Rishabh
Rishabh

Reputation: 3852

How to mock another method in the same class which is being tested?

I am writing JUnit Test case for a class which has two methods methodA,methodB. I would like to mock the call to the methodB from methodA in my test case I am using spy on the class which I am testing, but still the methodB gets executed.

here is the class

 public class SomeClass
{

     public Object methodA(Object object) 
    {
         object=methodB(object);

        return object;
    }



    public Object methodB(Object object) 
    {
        //do somthing
        return object;
    }
}

here is the test class

  @RunWith( org.powermock.modules.junit4.legacy.PowerMockRunner.class )
    @PrepareForTest(SomeClass.class)
    public class SomeClassTest { 

        private SomeClass var = null; 


        @Before
        public void setUp() {

            var=new SomeClass();

        }

        @After
        public void tearDown()
            {
            var= null;

        }

        @Test
        public void testMethodA_1()
            throws Exception {
            Object object =new Object();
        SomeClass spy_var=PowerMockito.spy(var);
        PowerMockito.when(spy_var.methodB(object)).thenReturn(object);

    Object result = var.methodA(object);        

        assertNotNull(result);

        }

    }

The method B still gets the called though I have mocked it PLease suggest me a solution with the proper way of mocking the methodB call from methodA of the same class.

Upvotes: 18

Views: 37073

Answers (4)

Adarsh Gupta
Adarsh Gupta

Reputation: 81

I had this very same challenge. Have a look at this solution which will work for newer testing suit versions.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.5.0</version>
</dependency>

Solution-

//when- then for MethodA test
SomeClass spy_var=Mockito.spy(var);
doReturn(X).when(spy_var).methodB(object)
Object result = spy_var.methodA(object);
//assertions on result

Upvotes: 2

Francislainy Campos
Francislainy Campos

Reputation: 4164

As per @Totoro's answer, this is just a summary here.

  1. Annotate the object for the class you are testing with the @Spy annotation.

So this:

@Spy
@InjectMocks
MyClassUnderTest myClassUnderTest;

instead of just

@InjectMocks
MyClassUnderTest myClassUnderTest;
  1. Use doReturn() instead of when.thenReturn() whenever asking for the mocked object from the class spyed on (class under test).

doReturn(X).when(myClassUnderTest).method(any())

Upvotes: 8

Totoro
Totoro

Reputation: 625

I ran into this yesterday, for spies is best to do:

doReturn(X).when(spy).method(any())

Upvotes: 11

Sam Holder
Sam Holder

Reputation: 32936

Taking this approach will result in brittle tests which will need to change if you refactor your class under test. I would highly recommend that you try to assert your expected test results by checking state of SomeClass rather than relying on mocks.

If you do indeed need to mock MethodB then this is an indication that maybe the behaviour in MethodB actually belongs in a separate class which you could then test the interaction of SomeClass with via mocks

if you do indeed need to do what you ask for then a PartialMock is what you want.

you probably want to create a partial mock of some class but indicate that calls to MethodA should call the actual method but then mock MethodB

You can see how to use them in the Mockito documentation

As stated in their documentation though Partial mocks are a code smell, though they have identified some explicit use cases.

Upvotes: 10

Related Questions