Example usage for org.springframework.data.mongodb.core MongoOperations findById

List of usage examples for org.springframework.data.mongodb.core MongoOperations findById

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoOperations findById.

Prototype

@Nullable
<T> T findById(Object id, Class<T> entityClass, String collectionName);

Source Link

Document

Returns the document with the given id from the given collection mapped onto the given target class.

Usage

From source file:com.comcast.video.dawg.service.house.MongoHouseServiceTest.java

@SuppressWarnings("unchecked")
@Test//from  w w w . j a v a2 s .co  m
public void testGetById() {
    MongoHouseService service = new MongoHouseService();
    MongoOperations operations = EasyMock.createMock(MongoOperations.class);
    EasyMock.expect(operations.findById((Object) EasyMock.anyObject(), (Class) EasyMock.anyObject(),
            (String) EasyMock.anyObject())).andReturn(null).anyTimes();
    EasyMock.replay(operations);
    ReflectionTestUtils.setField(service, "template", operations);
    Assert.assertNull(service.getById("id"), "expected null but got" + service.getById("id"));
}

From source file:com.comcast.video.dawg.service.house.MongoHouseServiceTest.java

@SuppressWarnings("unchecked")
@Test//from   www .  j  ava  2  s  .  co  m
public void testUpsertStb() {
    MongoHouseService service = new MongoHouseService();
    MongoOperations operations = EasyMock.createMock(MongoOperations.class);
    EasyMock.expect(operations.collectionExists((Class) EasyMock.anyObject())).andReturn(false);
    EasyMock.expect(operations.createCollection((Class) EasyMock.anyObject())).andReturn(null);

    operations.save((PersistableDevice) EasyMock.anyObject(), (String) EasyMock.anyObject());
    EasyMock.expectLastCall();
    EasyMock.expect(operations.findById((Object) EasyMock.anyObject(), (Class) EasyMock.anyObject(),
            (String) EasyMock.anyObject())).andReturn(null);
    EasyMock.replay(operations);
    ReflectionTestUtils.setField(service, "template", operations);

    ParkService mockParkService = EasyMock.createMock(ParkService.class);
    EasyMock.expect(mockParkService.getModelById((String) EasyMock.anyObject())).andReturn(null);
    EasyMock.replay(mockParkService);
    ReflectionTestUtils.setField(service, "parkService", mockParkService);

    DawgDevice[] stb = new DawgDevice[1];
    stb[0] = new PersistableDevice();

    try {
        service.upsertStb(stb);
    } catch (Exception e) {
        Assert.fail(e.getMessage(), e);
    }
}