gpt4 book ai didi

java - Spring WebFlux测试抛出 "IllegalStateException: Failed to load ApplicationContext"异常

转载 作者:行者123 更新时间:2023-12-01 19:33:49 25 4
gpt4 key购买 nike

我创建了一个简单的 Spring WebFlux REST api,我想编写测试用例来对其进行端到端测试(无模拟)。

但它抛出错误消息。

这是我的类(class)。

休息 Controller 类

@RestController
@RequestMapping("/users")
public class UsersController {
private final UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@RequestMapping(path = "list")
public Mono<ResponseEntity> getusersList() {
return userService.getusers();
}

休息 Controller 测试类


@RunWith(SpringRunner.class)
@WebFluxTest
public class UserControllerTestIT {
@Mock
private UserService userService;

@Autowired
private WebTestClient webTestClient;

@Test
public void testApplicationsList() {
this.webTestClient
.get()
.uri("/users/list")
.exchange();
}
}

但我收到以下错误消息。如何解决这个问题?

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
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)

最佳答案

如果您使用 SpringRunner.class 运行测试,则需要使用 @MockBean 而不是 @Mock

请引用spring boot documentation

@RunWith(SpringRunner.class)
@WebFluxTest
public class UserControllerTestIT {
@MockBean
private UserService userService;

@Autowired
private WebTestClient webTestClient;

@Test
public void testApplicationsList() {
this.webTestClient
.get()
.uri("/users/list")
.exchange();
}
}

此外,@WebFluxTest@MockBean@Import 结合使用,以创建 @Controller 所需的任何协作者 bean 类。

关于java - Spring WebFlux测试抛出 "IllegalStateException: Failed to load ApplicationContext"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58552416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com