SpringBoot1.4-无法找到@SpringBootConfiguration,在运行IntegrationTest

时间:2017-03-06 09:26:14

标签: spring-mvc gradle spring-boot applicationcontext spring-boot-test

我在SpringBoot 1.4中的一个控制器测试中面临集成测试的问题。 下面的代码片段将清楚地展示项目结构:

类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和Tests的新手,帮助我找出原因和可能的解决方案

StackTrace上面提到的错误:

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)

1 个答案:

答案 0 :(得分:-1)

看起来即使您使用@SpringBootTest注释,也不包括其classes参数,您需要在上下文中指定需要加载的类才能使测试成功运行。

检查您需要哪些课程并将其包含在那里:

@SpringBootTest(classes=...)

此外,尽管可能不是最佳解决方案,但如果您不介意为测试重新加载整个弹簧上下文,则可以在测试类中使用@SpringBootConfiguration注释而不是@SpringBootTest < / p>