mardi 8 décembre 2015

How to bypass a actual function call for unit testing play framework and mockito library?

I am writing unit tests for model class and its using MongoDB with playframe work. I want to skip the actual call to datastore and test the rest of the functionality.

//Transfer Object Class.
Class public MyModel {
    //Attributes for model.
    private int id;
    private String name;
    private String phone;

}

//Access Object Class.
Class public MyModelAccess {
    // getDs is a mongo library function which returns datastore instance.
    private DatastoreImpl datastore = getDs();
    public static boolean insert(MyModel data) {
        try {
            datastore.save(data);
            return true;
        } catch(Exception e) {
              printException(e.ErrorMessage());
        }
        return false;
    }
}


//Testing class.
public class TestMyModelAccess {
    MongoClient mongoTest = mock(MongoClient.class);
    Morphia morphiaTest = new Morphia();

    DatastoreImpl datastore = spy(new DatastoreImpl(morphiaTest, mongoClientTest, "TestDB"));
    doReturn(null).when(datastore).save(any(MyModelAccess.class));

    MyModelAccess obj = new MyModelAccess();
    // Add some dummy data to object.
    boolean response = MyModelAccess.insert(obj);

    assertEquals(response, true);
    verify(datastore).save(isA(MyModel.class));
}

I write this way by reffering Mockito but its creating an entry in the actual mongo and verify test is getting failed saying that DatastoreImpl.save(isA(MyModel)) not invoked.Although i am getting response as true.

I just want to skip the actual call to datastore.save and just want to check that it was called with the given type of argument.

Aucun commentaire:

Enregistrer un commentaire