Java 弹簧反应测试用例

Java 弹簧反应测试用例,java,spring,spring-boot,spring-test,Java,Spring,Spring Boot,Spring Test,我一直在尝试为Spring反应式模块编写一个单元测试用例。我在那个模块中遇到了错误 以下是我的测试用例代码: import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigur

我一直在尝试为Spring反应式模块编写一个单元测试用例。我在那个模块中遇到了错误

以下是我的测试用例代码:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;


@RunWith(SpringRunner.class)
@WebFluxTest(CustomController.class)
@AutoConfigureWebTestClient
public class CustomControllerTest {

    @Autowired
    private WebTestClient testClient;

    @Test
    public void broadcastVoltageConsumption() {
        System.out.println("CONTEXT LOADING >>>>>>>>>");
        System.out.println(testClient);
    }
}
我遇到以下错误:

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:73)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:240)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:153)
    at org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTestContextBootstrapper.processMergedContextConfiguration(WebFluxTestContextBootstrapper.java:35)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:395)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:97)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
    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:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

无法找出此处的错误所在,是否有相关帮助?

这两个测试类(SpringWebFluxDemoApplicationTests和CustomControllerTest)都在同一个包中?否,它们在不同的软件包中SpringWebFluxDemo应用程序测试在org.opensource.springWebflxDemo.main中,CustomControllerTest在org.opensource.springWebflxDemo.controllerI中。我相信SpringWebFluxDemo应用程序和CustomController的配置有所不同(已经在SpringWebFluxDemoApplication类中的CustomController中缺少某些内容)。这两个都是我在代码中显示的。这就是我不明白为什么它不工作的原因。我相信Spring测试没有看到主类,无法启动应用程序。你能试着从“org.opensource.springwebflxudemo.main”到父“org.opensource.springwebflxudemo”包?(或者,至少是springwebfluxdemo应用程序类)
@RunWith(SpringRunner.class)
@WebFluxTest
public class SpringWebFluxDemoApplicationTests {

    @Test
    public void contextLoads() {
        System.out.println("CONTEXT LOAD ########## ");
    }

}