mercredi 2 décembre 2015

Mockito: verify the captured object's method is called

I am using Mockito to write a simple unit test. I have a smiple abstract class which implements Runnable:

public abstract class MyRunnable implements Runnable {
  @Override
  public void run() {
    doTask();
  }
  public abstract void doTask();
}

Then, a function under test uses MyRunnable:

public class MyService {
  public void something() {
     executor.execute(new MyRunnable() {
        @Override
        doTask() {
          …
        }
     });
  }
}

My test case, I want to test doTask() has run :

@Test
public void testSomething() {
   …
   ArgumentCaptor<MyRunnable> myCaptor = ArgumentCaptor.forClass(MyRunnable.class);
   verify(mockMyService).something(myCaptor.capture());

   // get what has been captured
   MyRunnable myRunnable = myCaptor.getValue();
   //verify doTask() has run , but got ERROR.
   verify(myRunnable).doTask();
}

My test case throw the following error:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type  and is not a mock!

The error complains that verify() only accept mocked object. Then, how can I verify/test that the captured MyRunnable object has run doTask() with Mockito?

Aucun commentaire:

Enregistrer un commentaire