0

我在SpringBoot 1.4中的一個Controller Test上遇到Integration Test問題。 下面片段將顯示項目結構的清晰思路:SpringBoot1.4-無法找到@ SpringBootConfiguration,在運行IntegrationTest時出現測試錯誤時使用@ContextConfiguration/@SpringBootTest(class)

類ExchangeControllerIT:

public class ExchangeControllerIT extends AbstractSpringControllerIT { 

     // class under test 
     @Autowired 
     private ExchangeController exchangeController; 

     @Autowired 
     private OAuth2RestTemplate restTemplate; 

     @Test 
     public void shouldSuccessWhileExchange() throws Exception { 
     // given  
     controllerHas(mockExchangeServiceReturningStringResponse()); 
     // then  
     getMockMvc().perform(get(Uris.Exchange).accept(MediaType.TEXT_HTML) 
       .content(asString(ExchangeControllerIT.class, ""))) 
       .andExpect(status().isOk()) 
      .andExpect(content().contentType(MediaType.parseMediaType(MediaType.TEXT_HTML + ";charset=UTF-8")));   
     } 

     private void controllerHas(ExchangeService exchangeService) { 
      Reflections.setField(exchangeController, "exchangeService", exchangeService); 
     } 

     private static ExchangeService mockExchangeServiceReturningStringResponse() { 
     return new HandShakeService();   
     } 
} 

抽象類如下:

public abstract class AbstractSpringControllerIT extends AbstractSpringIT{ 

     protected MockMvc getMockMvc() { 
      return webAppContextSetup(getApplicationContext()).build(); 
     } 
    } 

AbstractSpringIT類:

@RunWith(SpringRunner.class) 
    @SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT) 
    public abstract class AbstractSpringIT { 

     @Autowired(required=true) 
     private GenericWebApplicationContext ctx; 
     protected final GenericWebApplicationContext getApplicationContext() { 
      return ctx; 
     } 
    } 

我是新來SpringBoot和測試,幫我找出原因和可能的解決方案

堆棧跟蹤上面提到的錯誤:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test 

at org.springframework.util.Assert.state(Assert.java:392) 
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:173) 
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112) 
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78) 
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120) 
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143) 
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) 
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:96) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
+0

您能否包含完整的堆棧跟蹤? – megalucio

+0

@megalucio我添加了stackTrace ..你能看看它是否提供任何提示 –

+0

你的測試和你的主要應用程序類包含什麼包? –

回答

-1

看起來即使您正在使用的@SpringBootTest註解你是不是包括它的類參數,您需要指定需要在上下文中加載的類,以便測試成功運行。

檢查你所需要的課程,包括他們那裏:

@SpringBootTest(classes=...) 

此外,雖然可能不是最好的解決辦法,如果你不介意重新加載整個春天背景爲您的測試,你可以只使用@SpringBootConfiguration註釋在您的測試類中而不是@SpringBootTest

+0

謝謝megalucio我在ExchangeControllerIT類上嘗試過,現在它給出**嵌套異常是org.springframework .context.ApplicationContextException:由於缺少EmbeddedServletContainerFactory bean,無法啓動EmbeddedWebApplicationContext **但是上面的哪個類應添加@SpringBootConfiguration註釋 –

+0

我認爲它將位於AbstractSpringControllerIT中。再次檢查我的答案,因爲我根據提供的堆棧跟蹤修改了我的上一個答案。 – megalucio

+0

我在AbstractSpringControllerIT上試過了@SpringBootConfiguration,但它沒有工作 –

相關問題