The method in the service layer that is being tested below...:
@Service
public class Service{
public Flux<String> methodBeingTested () {
Flux<String> fluxOfTypeString = Flux.just("test");
return fluxOfTypeString;
}
}
The JUnit Test:
@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
@InjectMocks
Service service;
@Mock
Utility Util;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
Flux<String> fluxOfTypeString = Flux.just("test");
when(service.methodToTest()).thenReturn(fluxOfTypeString);
}
In Debug mode I get the following error:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
FluxJust cannot be returned by toString()
toString() should return String
And when not in debug mode I get the following error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
Any ideas on what a possible workaround for this is? any help would be much appreciated.
You don't need Mockito
at all to test methodToTest()
, since no third party classes (like Utility
, which you mock in your ServiceTest
) are called:
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
class MyTest {
private Service service = new Service();
@Test
void name() {
Flux<String> actual = service.methodToTest();
StepVerifier.create(actual)
.expectNext("test")
.verifyComplete();
}
}
More information about testing reactive streams can be found in this article: https://www.baeldung.com/reactive-streams-step-verifier-test-publisher