Saturday 15 September 2012

java - Junit Mockito Exception while testing Spring MVC application -



java - Junit Mockito Exception while testing Spring MVC application -

application: spring mvc, junit, mockito

query: unable test spring mvc application. getting error related "-servlet.xml" file not getting loaded contextconfiguration.

following source code: file tested exists in src/test/java -servlet-test.xml file exists in src/test/resource. file contain required configuration (dao, beans...)

recipe-servlet-test.xml

<mvc:annotation-driven /> <context:annotation-config /> <context:component-scan base-package="com.recipe" /> <!-- bean declarations --> <bean id="recipecontroller" class="com.recipe.mvc.recipecontroller" /> <!-- menu list configuration menu --> <util:list id="configmenulist" value-type="java.lang.string"> <value>cuisine</value> <value>person</value> <!-- <value>ingredients</value> --> </util:list> <!-- handler serving static resources --> <mvc:resources location="/resources" mapping="/resources/**" /> <!-- configure property files --> <context:property-placeholder location="classpath:spring/jdbc.properties, classpath:spring/business-config.properties" ignore-resource-not-found="true" local-override="false" ignore-unresolvable="false" properties-ref="defaultconfiguration" /> <util:properties id="defaultconfiguration"> <!-- <prop key="hitransactionevent.hitransactioneventid">1</prop> --> </util:properties> <!--configuration connect oracle --> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="driverclassname" value="${home.jdbc.driverclassname}" /> <property name="url" value="${home.jdbc.url}" /> <property name="username" value="${home.jdbc.username}" /> <property name="password" value="${home.jdbc.password}" /> <property name="initialsize" value="${home.jdbc.initialsize}" /> <property name="maxactive" value="${home.jdbc.maxactive}" /> </bean> <!-- hibernate session mill configuration --> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="packagestoscan" value="com.recipe" /> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">${home.jdbc.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- transaction's manager - using hibernate transaction manager --> <bean id="transactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory" /> </bean> <!-- enable annotation driven transactions --> <tx:annotation-driven transaction-manager="transactionmanager" /> <!-- define transactions --> <tx:advice transaction-manager="transactionmanager" id="txadvise"> <tx:attributes> <tx:method name="add*" propagation="required" read-only="false" /> <tx:method name="*" propagation="supports" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txadvise" pointcut="execution(* com.recipe.service.recipeservices.*(..))" /> </aop:config>

test file..

@runwith(springjunit4classrunner.class) @contextconfiguration(locations={"classpath:recipe-servlet-test.xml"}) public class configcuisinecontrollertest { @mock private configservices configservices; @injectmocks private configcuisinecontroller configcuisinecontroller; private mockmvc mockmvc; @before public void setup(){ mockitoannotations.initmocks(this); mockmvc = mockmvcbuilders.standalonesetup(configcuisinecontroller).build(); } @test public void testgetcuisine() throws exception{ list<cuisine> cuisines = aslist(new cuisine(), new cuisine(), new cuisine()); when(configservices.getcuisine()).thenreturn(cuisines); mockmvc.perform(get("/config/cuisine")) .andexpect(status().isok()) .andexpect(view().name("config/cuisines")); } }

few relevant jars are:

<!-- testing jars --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>3.2.4.release</version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test-mvc</artifactid> <version>1.0.0.m1</version> <scope>test</scope> </dependency> <dependency> <groupid>org.mockito</groupid> <artifactid>mockito-core</artifactid> <version>1.9.5</version> <scope>test</scope> </dependency> <!-- start spring framework --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency>

mvn dependency tree

$ mvn dependency:tree [info] scanning projects... [info] [info] ------------------------------------------------------------------------ [info] building recipe maven webapp 0.0.1-snapshot [info] ------------------------------------------------------------------------ [info] [info] --- maven-dependency-plugin:2.8:tree (default-cli) @ recipe --- [info] recipe:recipe:war:0.0.1-snapshot [info] +- javax.servlet:servlet-api:jar:2.5:compile [info] +- junit:junit:jar:4.11:test [info] | \- org.hamcrest:hamcrest-core:jar:1.3:test [info] +- org.springframework:spring-test:jar:3.2.4.release:test [info] +- org.springframework:spring-test-mvc:jar:1.0.0.m1:test [info] | +- org.springframework:spring-context:jar:3.1.1.release:compile [info] | \- org.hamcrest:hamcrest-library:jar:1.2.1:test [info] +- org.mockito:mockito-core:jar:1.9.5:test [info] | \- org.objenesis:objenesis:jar:1.0:test [info] +- org.springframework:spring-core:jar:3.2.4.release:compile [info] | \- commons-logging:commons-logging:jar:1.1.1:compile [info] +- org.springframework:spring-web:jar:3.2.4.release:compile [info] | +- org.springframework:spring-aop:jar:3.2.4.release:compile [info] | \- org.springframework:spring-beans:jar:3.2.4.release:compile [info] +- org.springframework:spring-webmvc:jar:3.2.4.release:compile [info] | \- org.springframework:spring-expression:jar:3.2.4.release:compile [info] +- javax.annotation:jsr250-api:jar:1.0:compile [info] +- org.springframework:spring-asm:jar:3.1.1.release:compile [info] +- aopalliance:aopalliance:jar:1.0:compile [info] +- org.aspectj:aspectjrt:jar:1.6.11:compile [info] +- org.aspectj:aspectjweaver:jar:1.6.11:compile [info] +- jstl:jstl:jar:1.2:compile [info] +- org.apache.tiles:tiles-template:jar:2.2.2:compile [info] | \- org.apache.tiles:tiles-api:jar:2.2.2:compile [info] +- org.apache.tiles:tiles-servlet:jar:2.2.2:compile [info] | \- org.apache.tiles:tiles-core:jar:2.2.2:compile [info] | +- commons-digester:commons-digester:jar:2.0:compile [info] | | \- commons-beanutils:commons-beanutils:jar:1.8.0:compile [info] | \- org.slf4j:jcl-over-slf4j:jar:1.5.8:compile [info] +- org.apache.tiles:tiles-jsp:jar:2.2.2:compile [info] +- com.oracle:ojdbc14:jar:10.2.0.3.0:compile [info] +- org.springframework:spring-jdbc:jar:3.2.4.release:compile [info] | \- org.springframework:spring-tx:jar:3.2.4.release:compile [info] +- javassist:javassist:jar:3.12.1.ga:compile [info] +- org.hibernate:hibernate-core:jar:3.6.3.final:compile [info] | +- antlr:antlr:jar:2.7.6:compile [info] | +- commons-collections:commons-collections:jar:3.1:compile [info] | +- dom4j:dom4j:jar:1.6.1:compile [info] | +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.final:compile [info] | +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.final:compile [info] | +- javax.transaction:jta:jar:1.1:compile [info] | \- org.slf4j:slf4j-api:jar:1.6.1:compile [info] +- org.springframework:spring-orm:jar:3.2.4.release:compile [info] +- org.hibernate:hibernate-validator:jar:4.2.0.final:compile [info] | \- javax.validation:validation-api:jar:1.0.0.ga:compile [info] +- commons-dbcp:commons-dbcp:jar:1.4:compile [info] | \- commons-pool:commons-pool:jar:1.5.4:compile [info] \- log4j:log4j:jar:1.2.17:compile [info] ------------------------------------------------------------------------ [info] build success [info] ------------------------------------------------------------------------ [info] total time: 1.984s [info] finished at: fri oct 31 17:19:11 est 2014 [info] final memory: 9m/23m [info] ------------------------------------------------------------------------

exception :

java.lang.noclassdeffounderror: org/springframework/web/context/request/async/callableprocessinginterceptor @ org.springframework.test.web.servlet.mockmvcbuildersupport.createmockmvc(mockmvcbuildersupport.java:50) @ org.springframework.test.web.servlet.setup.defaultmockmvcbuilder.build(defaultmockmvcbuilder.java:207) @ com.test.mvc.configcuisinecontrollertest.setup(configcuisinecontrollertest.java:44) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:47) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:44) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:24) @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:74) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:83) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:231) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:88) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:63) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:236) @ org.junit.runners.parentrunner.access$000(parentrunner.java:53) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:229) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71) @ org.junit.runners.parentrunner.run(parentrunner.java:309) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:174) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) caused by: java.lang.classnotfoundexception: org.springframework.web.context.request.async.callableprocessinginterceptor @ java.net.urlclassloader$1.run(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ sun.misc.launcher$appclassloader.loadclass(unknown source) @ java.lang.classloader.loadclass(unknown source) ... 31 more

the interface callableprocessinginterceptor introduced in spring-web version 3.2 have alter dependency version 3.2 or newer.

from interface javadoc:

public interface callableprocessinginterceptor intercepts concurrent request handling, concurrent result obtained executing callable on behalf of application asynctaskexecutor. callableprocessinginterceptor invoked before , after invocation of callable task in asynchronous thread, on timeout container thread, or after completing reason including timeout or network error. general rule exceptions raised interceptor methods cause async processing resume dispatching container , using exception instance concurrent result. such exceptions processed through handlerexceptionresolver mechanism. aftertimeout method can select value used resume processing. since: 3.2 author: rossen stoyanchev

java spring-mvc junit

No comments:

Post a Comment