mardi 5 juillet 2016

Mock inherited method in Mockito Java

My class structure is as follows:

public class MyParentClass {

 void doSomethingParent(){
     System.out.println("something in parent");
 }
}

public class MyClass extends MyParentClass{


    protected String createDummyRequest(Holder myHolder){
        //...
        super.doSomethingParent();//I want to avoid this
        //...
        callingDB();
        return "processedOutput";
    }

    private void callingDB(){
        System.out.println("Calling to DB");
    }
}

Then my unit test:

public class UnitTest {


    public void testCreateDummyRequest(){
        //create my mock holder
        Holder mockHolder = new Holder();

        MyClass mockObj = Mockito.mock(MyClass.class);
        //mock doSomethingParent()
        //mock callingDB()

        //as mockObj is a fully mock, but I need to run my real method
        //Mockito.when(mockObj.createDummyRequest(mockHolder)).thenCallRealMethod();
        mockObj.createDummyRequest(mockHolder);
        //Problem: doSomethingParent() is getting called though I have mocked it
    }
}

How do I prevent the calling of the super.doSomethingParent() in my method? (method which I am writing my test)

Aucun commentaire:

Enregistrer un commentaire