0

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

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)
4

1 回答 1

-1

看起来即使您使用的是@SpringBootTest注释,但您没有包含它的 classes 参数,您需要在其中指定您需要在上下文中加载的类,以便您的测试成功运行。

检查您需要哪些课程并将其包括在内:

@SpringBootTest(classes=...)

此外,虽然可能不是最好的解决方案,但如果您不介意为您的测试重新加载整个弹簧上下文,您可以只@SpringBootConfiguration在测试类中使用注释而不是@SpringBootTest

于 2017-03-06T11:23:51.453 回答