2

I am trying to unit test a method, which has different branches depending upon the value of an object that is created inside it. The below code demonstrates it.

public class AClass {

public void method2() {
  //Some code goes here
}
public void method1(BClass bObject) {
    C_Class cObject = bObject.someMethodThatReturnsC();
    if(cObject != null) {
        method2();
        method2();
    }
}}

Below is the TestClass:

public class AClassTest {
@InjectMocks
AClass AClassSpy;

@Mock
BClass b_objectMock;

@Mock
C_Class c_objectMock;

@BeforeMethod
public void beforeMethod() {
  AClassSpy = spy(new AClass());      
  MockitoAnnotations.initMocks(this);
}

public void method1Test () {
    doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC());
    AClassSpy.method1(b_objectMock);
    verify(AClassSpy, times(2).method2();
}
}

However, it always FAILS, since the c_objectMock is always null. What do I do to tell Mockito to not to return a null object?

Caesar
  • 1,092
  • 12
  • 19

2 Answers2

2

It works good, just use @Before annotation from junit, not @BeforeMethod, and mark your test method like @Test and remove second bracket from
doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC())<-this one;
and add bracket at verify:
verify(AClassSpy, times(2)<-here.method2();
And just take care of your code!

This should work:

public class AClassTest {

  @InjectMocks
  private AClass AClassSpy;

  @Mock
  private BClass b_objectMock;

  @Mock
  private C_Class c_objectMock;

  @Before
  public void beforeMethod() {
      AClassSpy = spy(new AClass());
      MockitoAnnotations.initMocks(this);
  }

  @Test
  public void method1Test() {
      doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC();
      AClassSpy.method1(b_objectMock);
      verify(AClassSpy, times(2)).method2();
  }

}

Instead of before method you can use annotation @RunWith. It looks clearly:

@RunWith(MockitoJUnitRunner.class)
public class AClassTest {

  @Spy
  @InjectMocks
  private AClass AClassSpy;

  @Mock
  private BClass b_objectMock;

  @Mock
  private C_Class c_objectMock;

  @Test
  public void method1Test() {
      doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC();
      AClassSpy.method1(b_objectMock);
      verify(AClassSpy, times(2)).method2();
  }

}
Vova Yatsyk
  • 3,245
  • 3
  • 20
  • 34
1

You are having this behaviour because you are not mocking property the call to someMethodThatReturnsC.

It should be:

doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC();
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 1
    when(...) thenReturn(...) makes a real method call just before it is mocked. [link](http://stackoverflow.com/questions/20353846/mockito-difference-between-doreturn-and-when) , and I don't want to use it because it since someMethodThatReturnsC() can throw some Exception. – Caesar Oct 05 '15 at 07:27
  • @Caesar I just tested and the test passes correctly with this modification. – Tunaki Oct 05 '15 at 07:38
  • @Tunaki it also passes without making modifications (except fixing parentheses) – zubergu Oct 05 '15 at 07:49
  • 1
    @zubergu The OP has edited his post to fix his error with my answer. – Tunaki Oct 05 '15 at 07:51