I am writing unit tests for one of our reactive methods. This method depends on another service. I have mocked this service. While running the test, i am getting the below error:
java.lang.AssertionError: expectation "expectError(Class)" failed (expected: onError(MyException); actual: onComplete())
.
Here is what I have tried:
Method(ReactiveService.class):
@Autowired
private Service serice;
public Mono<MyObject> save(List<MyObject> arg1, String arg2) {
return SomeUtil.user()
.map(user -> service.save(arg1, arg2, user))
.subscribeOn(scheduler)
.doOnError(e -> {
throw new MyException(MyObject.class, "save object", e);
});
}
Test:
@Mock
Service service;
@InjectMocks
ReactiveService reactiveService;
@Test
public void unit_test(){
when(service.save(any(), any(), any())).thenThrow(new RuntimeException());
Mono<MyObject> result = reactiveService.save(arg1, arg2);
StepVerifier.create(result)
.expectError(MyException.class)
.verify();
}
From the error I understood that, no signal is received from the method. But I am not getting what I have missed here.