2014-10-31 138 views
0

應用程序:Spring MVC,JUnit,MockitoJunit Mockito在測試Spring MVC應用程序時異常

查詢:無法測試Spring MVC應用程序。獲取與「-servlet.xml」文件相關的錯誤未成功使用ContextConfiguration加載。

以下是源代碼: 要測試的文件存在於src/test/java中012her-servlet-test.xml文件存在於src/test/resource中。該文件包含所有必要的配置(DAO,豆類......)

偏方的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 for Configuration menu --> 
<util:list id="configMenuList" value-type="java.lang.String"> 
    <value>Cuisine</value> 
    <value>Person</value> 
    <!-- <value>Ingredients</value> --> 
</util:list> 

<!-- Handler for 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 to connect to 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 Factory 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> 

測試文件..

@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")); 
    } 
} 

一些相關JAR是:

 <!-- 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依賴關係樹

$ mvn dependency:tree 
[INFO] Scanning for 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] ------------------------------------------------------------------------ 

的例外是:

java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallableProcessingInterceptor 
    at org.springframework.test.web.servlet.MockMvcBuilderSupport.createMockMvc(MockMvcBuilderSupport.java:50) 
    at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.build(DefaultMockMvcBuilder.java:207) 
    at com.test.mvc.ConfigCuisineControllerTest.setup(ConfigCuisineControllerTest.java:44) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.request.async.CallableProcessingInterceptor 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 31 more 
+0

你有春天基於web *發佈的jar在類路徑? – 2014-10-31 04:42:49

+0

是的...... spring-web-3.1.1.RELEASE在類路徑中。 – Sandeep 2014-10-31 04:48:59

回答

0

接口CallableProcessingInterceptor春天的Web 3.2版的引入,所以你必須改變你的依賴版本3.2或更高版本。

從接口javadoc

public interface CallableProcessingInterceptor 
Intercepts concurrent request handling, where the concurrent result is obtained by executing a Callable on behalf of the application with an AsyncTaskExecutor. 
A CallableProcessingInterceptor is invoked before and after the invocation of the Callable task in the asynchronous thread, as well as on timeout from a container thread, or after completing for any reason including a timeout or network error. 

As a general rule exceptions raised by interceptor methods will cause async processing to resume by dispatching back to the container and using the Exception instance as the concurrent result. Such exceptions will then be processed through the HandlerExceptionResolver mechanism. 

The afterTimeout method can select a value to be used to resume processing. 
Since: 3.2 Author: Rossen Stoyanchev