1

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));
Nir
  • 1,608
  • 1
  • 17
  • 29

2 Answers2

4

This should work

 @Test
  public void test() throws .... {
    mockStatic(ItemParametersParser.class);
    doNothing().when(ItemParametersParser.class, "writeToFile", any(Item.class), any(Context.class),any(Builder.class));
  }

First argument is a class instance of the class under test, Second argument is the method name which you want to mock followed by arguments of the method.

Make sure you import org.powermock.api.mockito.PowerMockito.doNothing and not org.mockito.Mockito.doNothing

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • I've seen a similar solution before, however, I cannot pass arguments to "when". I get this error "The method when(T) in the type Stubber is not applicable for the arguments (Class, String, any(Item.class), any(Context.class), any(Builder.class))" – Nir Apr 17 '18 at 14:01
  • It works perfectly fine for me. I believe you are using `import static org.mockito.Mockito.doNothing;` . You should use `import static org.powermock.api.mockito.PowerMockito.doNothing;` – pvpkiran Apr 17 '18 at 14:03
  • hooray! the issue indeed was my imports. After changing the imports both solutions works. – Nir Apr 17 '18 at 14:38
3

Your when method should be on object:

ItemParametersParser itemParametersParser= mock(ItemParametersParser.class);    
doNothing().when(itemParametersParser).writeXMLToFile(any(Item.class),any(Context.class),any(Builder.class));
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • It doesn't work with this error: "ItemParametersParser cannot be resolved to a variable". also if I create an "object" of this class it doesn't work. – Nir Apr 17 '18 at 13:46
  • Try to add `@Mock ItemParametersParser ItemParametersParser;` – Ori Marko Apr 17 '18 at 13:50
  • It doesn't make any difference – Nir Apr 17 '18 at 14:02
  • The issue was my imports, as @pvpkiran mentioned. Your solution however still doesn't work for me. I get UnfinishedStubbingException. And also a warning that "ItemParametersParser should be accessed in a static way" – Nir Apr 17 '18 at 14:48