Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mock methods in same class

I am using Mockito to mock a method in the same class for which I am writing test. I have seen other answers on SO (Mocking method in the same class), but probably I am misunderstanding them, since I running into issues.

 class Temp() {      public boolean methodA(String param) {           try {               if(methodB(param))                    return true;               return false;          } catch (Exception e) {                e.printStackTrace();          }     }  } 

My Test method:

 @Test  public void testMethodA() {      Temp temp = new Temp();     Temp spyTemp = Mockito.spy(temp);      Mockito.doReturn(true).when(spyTemp).methodB(Mockito.any());      boolean status = temp.methodA("XYZ");      Assert.assertEquals(true, status);  } 

I however get the expection printed out because definition of methodB gets executed. My understanding is definition of methodB would get mocked by using spyTemp. However that does not appear to be the case.

Can someone please explain where I am going wrong?

like image 433
alwaysAStudent Avatar asked Aug 29 '16 05:08

alwaysAStudent


People also ask

Can we mock method of same class?

We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.

How do you mock another method in the same class which is being tested C#?

You can either mock the external calls that happen within the getTyreSpecification method or you can pull that method out into its own class, wrapped in an interface, and inject the interface into your Selecter class. That would allow you to mock it.

How do you mock a class in Java?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called. We don't need to do anything else to this method before we can use it.

What is difference between @mock and @injectmock?

@InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock annotations into this instance. @Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class.


2 Answers

The first issue is that you have to use spyTest object to expect something from Mockito. Here it is not the same as test. spyTemp is wrapped by the Mockito object temp.

Another issue is that you stub only methodB(), but you are trying to run methodA(). Yes in your implementation of methodA() you call methodB(), but you call this.methodB(), not spyTemp.methodB(). Here you have to understand that mocking would work only when you call it on the instance of temp. It's wrapped by a Mockito proxy which catches your call, and if you have overriden some method, it will call your new implementation instead of the original one. But since the original method is called, inside it you know nothing about Mockito proxy. So your "overriden" method would be called only when you run spyTemp.methodB()

This should work:

Mockito.doReturn(true).when(spyTemp).methodB(Mockito.any());  boolean status = spyTemp.methodA("XYZ"); 
like image 138
Konstantin Labun Avatar answered Oct 11 '22 12:10

Konstantin Labun


You created a spy and mocked methodB(). That is correct! But you called methodA() on the original object. To get the correct result call it on the spy

boolean status = spyTemp.methodA("XYZ"); 
like image 28
CoronA Avatar answered Oct 11 '22 12:10

CoronA