Spring boot 无法模拟Mono对象
我一直在为Spring反应式应用程序编写单元测试。我正在尝试存根返回Mono的方法响应。我在网上试过样品。但它给出了空指针异常。如果我们不能用Mockito存根Mono类型,请建议我另一种方法 下面的存储库中提供了完整的代码Spring boot 无法模拟Mono对象,spring-boot,junit,mockito,spring-webflux,project-reactor,Spring Boot,Junit,Mockito,Spring Webflux,Project Reactor,我一直在为Spring反应式应用程序编写单元测试。我正在尝试存根返回Mono的方法响应。我在网上试过样品。但它给出了空指针异常。如果我们不能用Mockito存根Mono类型,请建议我另一种方法 下面的存储库中提供了完整的代码 我想,这里您想测试ExampleService.getName(),但我看到,ExampleService被模拟了。至少它被当作模拟处理,但我没有看到它的定义是模拟的。我也没有看到任何实际初始化的Mockito。您的测试似乎有几个问题。1.您正在自动连接ExampleSe
我想,这里您想测试
ExampleService.getName()
,但我看到,ExampleService
被模拟了。至少它被当作模拟处理,但我没有看到它的定义是模拟的。我也没有看到任何实际初始化的Mockito
。您的测试似乎有几个问题。1.您正在自动连接ExampleService
的bean,然后将其用作模拟bean。2.如果您在测试中自动连接一个bean,那么您在哪里为您的测试创建该bean?
public Mono<Person> getName() {
Mono<Person> nameMono = name();
return nameMono.flatMap(per -> Mono.just(per)).onErrorResume(error -> {
return Mono.error(new RuntimeException("Name not found"));
});
}
public Mono<Person> name() {
Person person = new Person();
person.setName("Narayana");
return Mono.just(person);
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private ExampleService exampleService;
@Test
public void getNameTest() {
Person person = new Person();
person.setName("Narayana");
Mono<Person> nameMono = Mono.just(person);
given(this.exampleService.name()).willReturn(nameMono);
StepVerifier.create(exampleService.getName()).expectNext(person).verifyComplete();
}
}
java.lang.NullPointerException
at reactor.test.DefaultStepVerifierBuilder$DefaultStepVerifier.verify(DefaultStepVerifierBuilder.java:801)
at reactor.test.DefaultStepVerifierBuilder$DefaultStepVerifier.verify(DefaultStepVerifierBuilder.java:772)
at reactor.test.DefaultStepVerifierBuilder.verifyComplete(DefaultStepVerifierBuilder.java:644)
at com.example.demo.DemoApplicationTests.getNameTest(DemoApplicationTests.java:29)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
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)