如何对 mongotemplate 函数进行单元测试

How to unit test mongotemplate functions

我想为 mongotemplate 函数调用编写单元测试。我不知道怎么写,我只在网络上看到集成测试用例的实例。

public Class customerById(String id) {
        Query query = Query.query(Criteria.where("id").is(id));
        return template.findOne(query, Class.class);
    }

你可以这样试试,只要修改class名称

@Test
public void can_find_customer_by_customerId() {
    String id = someString(9);
    Customer Customer = mock(Customer.class);

    given(template.findOne(Query.query(Criteria.where("id").is(id)), Customer.class)).willReturn(Customer);

    Customer actual = factory.customerById(id);

    assertEquals(actual, Customer);
}