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,但它没有工作 –

相关问题