I tried all the answers regarding similar questions but I couldn't make it work (like This)
I have this class (simplified)
public class ItemParametersParser
{ ...
public static void writeToFile(Item i, Context c, Builder b) throws HandlerException
}
I am trying to mock the writeToFile method with no success
@RunWith(PowerMockRunner.class)
@PrepareForTest({ItemParametersParser.class})
public class MyTest {
@Test
public void test() throws ... {
mockStatic(ItemParametersParser.class);
doNothing().when(ItemParametersParser.class);
ItemParametersParser.writeXMLToFile(null,null,null);
...
}
This result with going to the original method I tried also mock the specific method using:
doNothing().when(ItemParametersParser.writeXMLToFile(any(Item.class),any(Context.class),any(Builder.class)));
but I get this error for doNothing.when(...
The method when(T) in the type Stubber is not applicable for the arguments (void)
or this error if I use PowerMokito.doNothing().when(...
The method when(Class) in the type PowerMockitoStubber is not applicable for the arguments (void)
using doAnswer also didn't work
Edit: After fixing the import issue, as mentioned by @pvpkiran, it works now! I could verify it using doAnswer.
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
System.out.println("called with arguments: " + Arrays.toString(args));
return null;
}
}).when(ItemParametersParser.class, "writeToFile", any(Item.class), any(Context.class),any(Builder.class));