Mohammed
Mohammed

Reputation: 757

PowerMockito whenNew thenReturn is not working for transitive classes

I have following scenario

Class A {
  someMethod() {
    B b = new B();
    b.someMethod();
  }
}

Class B {
  String someMethod() {
    C c = new C();
    return c.someMethod();
  }
}

Class C {
  String someMethod() {
    D d = new D();
    return d.getId()
  }
}

Class D {
  getId() {
    return "id123";
  }
}

what I am doing in my UT

@RunWith(PowerMockRunner.class)
@PrepareForTest({A.class, B.class, C.class})
MyTest {
  public void basicTest() {
    D mockDClass = mock(D.class);
    when(mockDClass.getId()).thenReturn("mockedId");
    PowerMockito.whenNew(D.class).withAnyArguments().thenReturn(mockClass);
  }
}

In my test, it should return mockedId but control goes to the actual method.

Any idea what I might be doing wrong? Does whenNew works when I do new in the class I test? And not for the case I described.

----------------------UPDATE--------------------- Wrote a simple module to test the above

public class A {
  public String someMethod() {
    B b = new B();
    return b.someMethod();
  }
}

public class B {
  public String someMethod() {
    C c = new C();
    return c.someMethod();
  }
}

public class C {
  public String someMethod() {
    D d = new D();
    return d.getId();
  }
}

public class D {
  public String getId() {
    return "id123";
  }
}

Test:

import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;

@RunWith(PowerMockRunner.class)
@PrepareForTest({A.class, B.class, C.class})
public class ATest extends TestCase {
  @Test
  public void mockTest() {
    D mockD = mock(D.class);
    when(mockD.getId()).thenReturn("this is from mock");
    PowerMockito.whenNew(D.class).withAnyArguments().thenReturn(mockD);
    A a = new A();
    System.out.println(a.someMethod());
  }
}

And the output of this test looks like this: enter image description here

This small test works so I am doing something wrong in the actual test.

Upvotes: 2

Views: 396

Answers (1)

Wafo
Wafo

Reputation: 54

whenNew should works every time the object you mocks get instantiated. You should actually replace the line 'when(D.getId()).thenReturn("mockedId")' with 'when(mockDClass.getId).thenReturn("mockedId")'. The following code snippets should works:

C test:

@RunWith(PowerMockRunner.class)
@PrepareForTest({C.class})
public class CTest {

  @Test
  public void someMethodTest() throws Exception {
    D mockDClass = mock(D.class);
    when(mockDClass.getId()).thenReturn("mockedId");
    PowerMockito.whenNew(D.class).withAnyArguments().thenReturn(mockDClass);
    C c = new C();

    String response = c.someMethod();
    verifyNew(D.class).withNoArguments();
    assertEquals("mockedId", response);
  }
}

D test:

@RunWith(PowerMockRunner.class)
public class DTest {

  @Test
  public void testId() throws Exception {
    D mockDClass = mock(D.class);
    when(mockDClass.getId()).thenReturn("mockedId");
    PowerMockito.whenNew(D.class).withAnyArguments().thenReturn(mockDClass);
    D d = new D();

    verifyNew(D.class).withNoArguments();
    String response = d.getId();
    assertEquals("mockedId", response);
  }
}

Upvotes: 1

Related Questions