lundi 5 octobre 2015

How to tell a Mockito Mock annotation to return a mock object which is not null?

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?

Aucun commentaire:

Enregistrer un commentaire