jeudi 29 janvier 2015

Mocking method from protected abstract method

Here's my problem in detail.


Setup:



  • I have class A that has a private member variable of class B.

  • A method(method1) in class A calls a non-static method(method2) in class B.

  • Class B actually inherits method2 from a protected abstract class C and does not override it.


Problem:



  • I'm writing a test for class A.

  • In the test I'm mocking the call to method2.


Sample Code:



B b = Mockito.mock(B.class);
A a = new A(b);
Mockito.when(b.method2()).thenReturn(MY_LIST);



  • Now when I call method1(which in turn calls method2), I get a

    NullPointerException.


Sample Code:



a.method1();



  • I'm assuming that this call is completely independent of the implementation of method2 since I'm mocking it. Is that wrong ? If not, what am I doing wrong ?


PS: class C is protected and Class A is in a different package from class B and C.


Aucun commentaire:

Enregistrer un commentaire