java.lang.IllegalStateException:找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration

Posted

技术标签:

【中文标题】java.lang.IllegalStateException:找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration【英文标题】:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration with your test 【发布时间】:2019-03-28 11:23:55 【问题描述】:

我们应该在src下单独创建test文件夹还是test文件夹?

我的测试班位置是:server/test/com/cellpointmombile/mconsole/manualtrigger/ManualTriggerControllerTest.java

我正在为类ManualTriggerController编写Junit,它的路径是:server/src/main/java/com/cellpointmobile/mconsole/controller/ManualTriggerController.java

我的主类,即 Application.java 位于以下位置: server/src/main/java/com/cellpointmobile/mconsole/app/Application.java

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:70)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137)
    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:82)
    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 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:87)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:73)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

你能告诉我我做错了什么吗?

【问题讨论】:

欢迎来到 Stack Overflow!其他用户将您的问题标记为低质量和需要改进。我重新措辞/格式化您的输入,使其更容易阅读/理解。请查看我的更改以确保它们反映您的意图。但我认为你的问题仍然无法回答。 现在应该edit你的问题,添加缺失的细节(见minimal reproducible example)。如果您对我有其他问题或反馈,请随时给我留言。 你的一些类所在的信息在这里没有多大帮助。它也可能是您的测试代码的一些内容。我的建议:不要试图做这样的事情“试错”。开始阅读并练习使用教程,该教程为您提供了所有 对 Spring Boot 应用程序进行单元测试所需的步骤。了解这些东西是如何工作的,然后然后尝试在你自己的项目中使用它们。 【参考方案1】:

正如你所说, 我的测试班地点是:server/test/com/cellpointmombile/mconsole/manualtrigger/ManualTriggerControllerTest.java

应该是, server/src/test/com/cellpointmombile/mconsole/manualtrigger/ManualTriggerControllerTest.java

还要确保您的测试类ManualTriggerControllerTest 已使用@SpringBootTest 进行注释,或者您也可以使用@ContextConfiguration@SpringBootTest 更简单,因为您无需担心其他任何事情,因为它负责上下文加载、配置让您的测试运行。

您的ManualTriggerControllerTest 应如下所示,

@RunWith(SpringRunner.class)
@SpringBootTest
public class ManualTriggerControllerTest
/* your code */

我希望这会有所帮助!

【讨论】:

【参考方案2】:

按照已经使用的包,测试运行成功。

在 src/main/java 里面

package com.an.csv.mvc;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/myclass")
public class TestControllerEx 

 @GetMapping
  public ResponseEntity control()
    return new ResponseEntity("success",HttpStatus.OK);
  

Spring Boot 主类:

package com.an.csv;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class)
public class CSVBatchApplication 

  public static void main(String[] args) 
    SpringApplication.run(CSVBatchApplication.class, args);
  

在src/test/java下

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.an.csv.CSVBatchApplication;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CSVBatchApplication.class)
@AutoConfigureMockMvc
public class ControllerIntegrationTest 

  @Autowired private MockMvc mockMvc;

  @Test
  public void testController() throws Exception 
    mockMvc
        .perform(get("/myclass"))
        .andExpect(status().isOk());
  

【讨论】:

试过了,但面临以下问题:Caught exception while allowed TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@25af5db5] to prepare test instance [com.cellpointmobile.promotion.test. PromotionTest@14bae047] - @ConditionalOnClass 没有找到所需的类 'javax.jms.ConnectionFactory'、'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition)

以上是关于java.lang.IllegalStateException:找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration的主要内容,如果未能解决你的问题,请参考以下文章