【问题标题】:SpringBoot1.4-Unable to find@SpringBootConfiguration,use @ContextConfiguration/ @SpringBootTest(class) in test error when I run IntegrationTestSpringBoot1.4-Unable to find@SpringBootConfiguration,use @ContextConfiguration/ @SpringBootTest(class) in test error when I run IntegrationTest
【发布时间】:2017-07-26 02:24:44
【问题描述】:

我在 SpringBoot 1.4 中的控制器测试之一上面临集成测试问题。 下面sn-ps会清晰地展示项目结构:

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

【问题讨论】:

  • 你能包含完整的堆栈跟踪吗?
  • @megalucio 我添加了 stackTrace.. 你能看看它是否给出任何提示
  • 你的测试和你的主要应用程序类在哪些包中?
  • @AndyWilkinson 我在 com.org.abc 下有主类和其他类,并且有 resources/com.org.abc 和相同的测试路径测试目录下的类。我在主目录和测试目录中的 resources/com.org.abc 下都有 application.properties 和 applicationContext.xml主目录和测试目录下com.org.abc/subfolder下的Appcontext和TestAppContext(导入applicationContext.xml)

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


【解决方案1】:

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

检查您需要哪些类并将它们包括在内:

@SpringBootTest(classes=...)

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

【讨论】:

  • 感谢 megalucio 我在 ExchangeControllerIT 类上尝试了同样的方法,现在它给出了 嵌套异常是 org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean。 但是我应该在上面的哪个类上添加@SpringBootConfiguration 注释
  • 我认为它将在 AbstractSpringControllerIT 中。还要再次检查我的答案,因为我根据提供的堆栈跟踪修改了我之前的答案。
  • 我在 AbstractSpringControllerIT 上尝试了@SpringBootConfiguration,但没有成功
  • 对不起,我的意思是用 AbstractSpringIT 代替其他注释。
  • 我在 AbstractSpringIT 类上添加了 @SpringBootTest(classes=) 注释。虽然我得到了构建成功,但是有异常警告 Exception: java.lang.IllegalStateException throw from the UncaughtExceptionHandler in thread "Thread-5 这是什么意思??
猜你喜欢
  • 2022-12-26
  • 2022-12-02
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2023-01-06
  • 2022-12-26
  • 2015-06-14
相关资源
最近更新 更多