Issue
We have a methodA which returns:
return bObj.methodB()
.flatMap(x -> cObj.methodC(x));
methodB returns Mono<String>
and methodC returns Mono<Void>
.
When using Mockito, methodB we are able to mock, but methodC we are not.
when(bObj.methodB()).thenReturn(Mono.just("x"));
when(cObj.methodC(eq("x"))).thenReturn(Mono.empty());
aObj.methodA();
When this runs, we get the following exception:
java.lang.NullPointerException: The mapper returned a null Publisher
When running the application, this works fine, but when running with Mockito, it doesn't. I assume it's something to do with the flatMap, but can't figure it out. Referencing this question it seems like it should work, but doesn't. Any ideas?
Solution
Turns out that if anybody gets an issue like this, be sure Mockito.when
is defined correctly. If it doesn't match, then it returns null. Hence the exception.
On a side note, I had left some information out of my question to make it a little less complicated, but turns out that the example I had described worked perfectly. Reworking through it shown the actual code I had didn't match the when clause.
Answered By - Brandon
Answer Checked By - Timothy Miller (JavaFixing Admin)