the method when(t) in the type mockito is not applicable for the arguments (void)
Hi, Homework Helper. I got stuck with my testing code. It keeps giving me an error: 'the method when(t) in the type Mockito is not applicable for the arguments (void)'. What does this mean?
Hello! That error message typically comes up when you're trying to use the Mockito 'when()' method on a method that doesn't return a value, or a void method. The 'when()' method is used to stub a return value for certain method calls during testing, so it needs a non-void method.
I see. But what should I do if I need to do something like this with a void method?
If you need to stub a void method, you should use the 'doReturn()', 'doThrow()', or 'doAnswer()' methods from Mockito. These methods are designed to deal with void methods for testing, allowing you test your void methods in different scenarios.
That sounds great, but could you please provide an example of how I could use these methods?
Absolutely! Here's a basic example:
```java
doThrow(new RuntimeException()).when(yourObject).yourVoidMethod();
```
This will cause a RuntimeException to be thrown whenever 'yourVoidMethod()' of 'yourObject' is called during the test.