Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito NotaMockException

I am facing an issue with Mockito junit testing. I am new to it and am a bit confused with the problem I am facing. Any help on this would be appreciated.

class Activity{

    public void firstMethod(){

      String str = secondMethod();
   }

    public String secondMethod(){
      String str = null;

      /*  some Code */

      return str;
   }
}

Getting exception :

*org.mockito.exceptions.misusing.NotAMockException: 
 Argument passed to when() is not a mock!*

in the below code

class ActivityTest(){

  Activity act;

  @Before
  public void setup(){
     act = new Activity();
  }

  @Test
  public void testFirstMethod(){

      Mockito.doReturn(Mockito.anyString()).when(act).secondMethod();
      act.firstMethod();
      verify(act).secondMethod();
  }
} 

I am aware that activity is not a mock but I am not sure for a way around this as secondMethod() is a method in the same class. I need to write rule for secondMethod() as I have already done its Unit Testing. The definition of secondMethod() consists has external dependencies. Should I be mocking the external dependencies present in secondMethod() and writing rules for them rather than rule for secondMethod()?

I found this post: Mockito Spy'ing on the object being unit tested However separating the secondMethod() into a different class does not make sense. My method is related to this class. Creating a different class for testing does not seem right to me. Even mocking the actual class using spy() is not the most correct way as already explained in the post.

I don't think I should be creating a mock of the Activity class as that is the class I am testing. I would really appreciate help and insights into this.

like image 661
alwaysAStudent Avatar asked Apr 13 '15 17:04

alwaysAStudent


People also ask

How does Mockito spy work?

A Mockito spy is a partial mock. We can mock a part of the object by stubbing few methods, while real method invocations will be used for the other. By saying so, we can conclude that calling a method on a spy will invoke the actual method, unless we explicitly stub the method, and therefore the term partial mock.

How do I verify a void in Mockito?

Using the verify() Method Mockito provides us with a verify() method that lets us verify whether the mock void method is being called or not. It lets us check the number of methods invocations. So, if the method invocation returns to be zero, we would know that our mock method is not being called.

How do you call a real method in Mockito?

Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().


1 Answers

As you noted, act is not a mock, and therefore you cannot record behavior on it. You could use Mockito.spy to, well, spy (or partially mock) the act object so that you only record the behavior of secondMethod and execute the actual code for firstMethod.

Note, however, that matchers can't be used in doReturn calls regardles of how you're mocking or spying your object. A return value must be a concrete object.

class ActivityTest() {

  Activity act;

  @Before
  public void setup(){
     act = Mockito.spy(new Activity()); // Here!
  }

  @Test
  public void testFirstMethod(){

      Mockito.doReturn("someString").when(act).secondMethod();
      act.firstMethod();
      verify(act).secondMethod();
  }
} 

A slightly more elegant syntax allows you to use annotations instead of explicitly calling Mockito.spy, but it's a matter of taste really:

@RunWith(MockitoJUnitRunner.class)
class ActivityTest() {

  @Spy
  Activity act = new Activity();

  @Test
  public void testFirstMethod(){

      Mockito.doReturn("someString").when(act).secondMethod();
      act.firstMethod();
      verify(act).secondMethod();
  }
} 
like image 115
Mureinik Avatar answered Oct 20 '22 16:10

Mureinik