2016-03-18 3 views
1

Ich habe einen JUnit-Test, der so aussieht - es ist Teil einer größeren Anwendung.Wie kann ich andere JUnit-Tests stoppen, die meinen Spring Root Controller stehlen?

Wenn ich meinen Test isoliert ausführe - läuft es gut.

Wenn ich laufe es als Teil einer Reihe von anderen Tests - erhalte ich die Fehler

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.MyHandlerInterceptorTest ': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping com.MyHandlerInterceptorTest.requestMappingHandlerMapping; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Initialization of bean failed; nested exception is org.springframework.context.ApplicationContextException: Cannot reinitialize with different application context: current one is [Root WebApplicationContext: startup date [XXX 2016]; root of context hierarchy], passed-in one is [[email protected]60457f: startup date [XXX 2016]; root of context hierarchy] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376) 
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110) 
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) 
.... 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping com.MyHandlerInterceptorTest.requestMappingHandlerMapping; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Initialization of bean failed; nested exception is org.springframework.context.ApplicationContextException: Cannot reinitialize with different application context: current one is [Root WebApplicationContext: startup date [XXX 2016]; root of context hierarchy], passed-in one is [[email protected]60457f: startup date [Fri Mar 18 11:01:19 EST 2016]; root of context hierarchy] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) 

Was passiert zu sein scheint, ist, dass mein Test Zusammenhang mit anderen Tests gestohlen wird. Ich möchte wissen, wie ich feststellen kann, wann das passiert, und es stoppen oder zumindest umgehen.

Meine Frage ist: Wie kann ich andere JUnit-Tests stoppen, die meinen Spring Root Controller stehlen?

Antwort

0

Sie können diesen Kontext von anderen Testkontexten isolieren. Sie können es tun, indem Sie den Kontext über name attribute of @ContextConfiguration Benennung:

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, 
    classes = { MyTestConfig.class }, name = "UniqueName") 
+0

Brillianten Vorschlag. Eine viel knappere Art zu tun http://www.jroller.com/arondight/entry/mocking_a_webapplicationcontext_in_order. Leider laufen beide in Fehler mit http://stackoverflow.com/questions/10013288/another-unnamed-cachemanager-already-exists-in-the-same-vm-ehcache-2-5 Ich schreibe was auf Ich habe - im Grunde das http://stackoverflow.com/questions/7239786/how-to-invoke-the-same-maven-build-twice-in-one-call – hawkeye

0

Der Kredit für die aufschlussreiche Antwort auf @luboskrnac geht. Es hat nicht ganz geklappt - also schreibe ich auf, was ich getan habe.

Ich versuchte, den Web-Kontext in einem Nicht-Root-Klasse-loader wie so zu isolieren:

import org.springframework.beans.factory.support.DefaultListableBeanFactory; 
import org.springframework.mock.web.MockServletContext; 
import org.springframework.web.context.support.GenericWebApplicationContext; 

DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory(getApplicationContext().getBeanFactory()); 
GenericWebApplicationContext gwac = new GenericWebApplicationContext(dlbf); 
MockServletContext mockServletContext = new MockServletContext(); 
mockServletContext .setAttribute(GenericWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, gwac); 
gwac.setServletContext(msc); 
gwac.refresh(); 

Interessanterweise - das ist effektiv das gleiche wie das, was @luboskrnac oben vorgeschlagen - aber seine Lösung ist viel elegante .

Leider führte dies zu einem Fehler mit meinem ehcache (was sich herausstellt - möchte nur einmal in einer JVM Class-Loader geladen werden, unabhängig von Class-Loader oder Spring-Kontext Partitionierung).

SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/FileService] threw exception [com.sun.jersey.api.container.ContainerException: Unable to create resource class com.myapp.FileStoreAccessAction] with root cause 
net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 
2. Shutdown the earlier cacheManager before creating new one with same name. 
The source of the existing CacheManager is: InputStreamConfigurationSource [[email protected]] 

Also, was ich am Ende dabei wurde den Test als Teil einer separaten todsicheren Testausführung wie folgt ausgeführt werden:

<build> 
<plugins> 
    <plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <executions> 
      <execution> 
       <phase>test</phase> 
       <id>test-1</id> 
       <configuration> 
        ... 
       </configuration> 
       <goals><goal>test</goal></goals> 
      </execution> 
      <execution> 
       <phase>test</phase> 
       <id>test-2</id> 
       <configuration> 
        ... 
       </configuration> 
       <goals><goal>test</goal></goals> 
      </execution> 
     </executions> 
    </plugin> 
</plugins> 
... 
</build> 
Verwandte Themen