无法找到@SpringBootConfiguration

问题描述 投票:0回答:1

我想为此私有方法创建一个JUnit测试:

@Component
public class ReportingProcessor {

    @EventListener
    private void collectEnvironmentData(ContextRefreshedEvent event) {
    }
}

我尝试过:

@ContextConfiguration
@SpringBootTest
public class ReportingTest {

    @Autowired
    ReportingProcessor reportingProcessor;

    @Test
    public void reportingTest() throws Exception {

        ContextRefreshedEvent contextRefreshedEvent = PowerMockito.mock(ContextRefreshedEvent.class);
        Whitebox.invokeMethod(reportingProcessor, "collectEnvironmentData", contextRefreshedEvent);

    }
}

当我运行代码时,我得到:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

您知道我可以解决此问题吗?

java spring spring-boot spring-test
1个回答
0
投票

您应将@RunWith@SpringBootTest一起使用:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ReportingTest {

    @Autowired
    ReportingProcessor reportingProcessor;

    @Test
    public void reportingTest() throws Exception {

        ContextRefreshedEvent contextRefreshedEvent = PowerMockito.mock(ContextRefreshedEvent.class);
        Whitebox.invokeMethod(reportingProcessor, "collectEnvironmentData", contextRefreshedEvent);

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.