2017-09-26 82 views
0

我正在試圖編寫一些測試用例的Spring-MVC應用程序。目前,我正在自動裝配我的服務層方法並用@ImportResouce註釋主類。我也試過@ContextConfiguration,但後來我得到一個NPE進行測試。我究竟做錯了什麼? JUNIT:4.1.2 - 與範圍測試POM目前 彈簧4.1.6 - 在POM目前Java,Spring,Junit:自動裝配測試失敗

代碼:

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ImportResource(value = {"classpath*:WEB-INF/spring/appServlet/servlet-context.xml", 
     "classpath*:WEB-INF/spring/root-context.xml", "classpath*:WEB-INF/spring/appServlet/security-applicationContext.xml"}) 
@Ignore 
public class TestingController { 

    @Autowired 
    protected PersonService personService; 

    @Autowired 
    protected GroupAccountService groupAccountService; 

    @Autowired 
    protected GroupCanvasService groupCanvasService; 

    @Autowired 
    protected GroupSectionService groupSectionService; 

    @Autowired 
    protected GroupNotesService groupNotesService; 

    @Autowired 
    @Qualifier("authenticationManager") 
    protected AuthenticationManager am; 

    @After 
    public void clear() { 
     SecurityContextHolder.clearContext(); 
    } 

    protected void login(String name, String password) { 
     Authentication auth = new UsernamePasswordAuthenticationToken(name, password); 
     SecurityContextHolder.getContext().setAuthentication(am.authenticate(auth)); 
    } 

} 

AllTests中:

public class AllTests extends TestingController { 

    public static final String MAIL = "[email protected]"; 
    private Person person; 
    private GroupAccount groupAccount; 
    private GroupCanvas groupCanvas; 
    private GroupSection groupSection; 
    private GroupNotes groupNotes; 


    @Test 
    public void runTests() { 
     this.person = TestUtils.createPerson(MAIL); 
     int personId = this.personService.addPerson(this.person); 
     assertTrue(this.personService.getPersonById(personId).getUsername().equals(MAIL)); 
     login(MAIL, "PASSWORD"); 
     Person loggedInUser = this.personService.getCurrentlyAuthenticatedUser(); 
} 

錯誤日誌:

Running tests.AllTests 
16:51:17.817 [main] INFO o.s.t.c.w.WebTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [tests.AllTests] 
16:51:17.827 [main] INFO o.s.t.c.w.WebTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 
16:51:17.843 [main] INFO o.s.t.c.w.WebTestContextBootstrapper - Using TestExecutionListeners: [or[email protected]4ddced80, org.springframewor[email protected]1534f01b, org.springfra[email protected]78e117e3, org.springframew[email protected]2ea227af, org.sp[email protected]4386f16] 
16:51:17.852 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [or[email protected]4ddced80] to prepare test instance [[email protected]] 
java.lang.IllegalStateException: Failed to load ApplicationContext 
     at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:170) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:110) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12] 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12] 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12] 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12] 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12] 
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12] 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) [surefire-junit4-2.17.jar:2.17] 
     at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) [surefire-junit4-2.17.jar:2.17] 
     at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) [surefire-junit4-2.17.jar:2.17] 
     at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200) [surefire-booter-2.17.jar:2.17] 
     at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) [surefire-booter-2.17.jar:2.17] 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [surefire-booter-2.17.jar:2.17] 
Caused by: java.lang.IllegalArgumentException: Cannot load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration or @ContextHierarchy. 
     at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
     at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:61) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE] 
     ... 25 common frames omitted 
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.358 sec <<< FAILURE! - in tests.AllTests 
runTests(tests.AllTests) Time elapsed: 0.008 sec <<< ERROR! 
java.lang.IllegalStateException: Failed to load ApplicationContext 
     at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94) 
     at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72) 
     at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:170) 
     at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:110) 
     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259) 
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) 
     at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) 
     at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200) 
     at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) 
Caused by: java.lang.IllegalArgumentException: Cannot load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration or @ContextHierarchy. 
     at org.springframework.util.Assert.notNull(Assert.java:112) 
     at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:61) 
     at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) 
     at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72) 
     at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:170) 
     at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:110) 
     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259) 
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) 
     at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) 
     at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200) 
     at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) 


Results : 

Tests in error: 
    AllTests.runTests » IllegalState Failed to load ApplicationContext 

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 

日誌:由於日誌超過30000個字符,上傳它到pastebin here

+1

的downvoter可以解釋爲什麼在這個問題downvoted。謝謝。 –

+0

爲什麼它無法加載應用程序上下文,查看原因或提供完整日誌? – Barath

+0

@Barath:附在主帖底部的日誌。 –

回答

0

爲什麼你的配置類註解@Ignore

這是因爲@Ignore也許。

+0

我已經添加了,指定這是用於測試的配置類。我嘗試刪除它並運行測試,仍然是相同的。謝謝 –

+0

並嘗試刪除'@WebAppConfiguration'。我害怕'TestingController'不是配置類。 –

+0

對不起,還是沒有運氣。你可以檢查附在主帖底部的日誌嗎?謝謝 –

0

而不是@ImportResource,請使用以下內容;

@TestPropertySource(locations = "classpath:./test.properties") 

@ContextConfiguration(initializers = AnyApplicationContextInitializer.class) 
+0

哪個文件是test.properties?我需要爲ContextConfiguration傳遞哪個類?謝謝。 –

+0

這只是一個例子。你可以在那裏傳遞你所有的屬性文件。 AnyApplicationContextInitializer是你自己的類。實現ApplicationContextInitializer 的東西但它是可選的,您可以將其保留爲空 – AlexGera

+0

當我刪除I​​mportResouce並添加ContextConfiguration時,出現autowired失敗錯誤。 TestPropertySource也沒有幫助。任何其他想法?謝謝 –