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

时间:2019-06-20 03:27:48

标签: unit-testing junit mongotemplate

我想为mongotemplate函数调用编写单元测试。我不确定如何写,我只在Web上看到过集成测试用例的实例。

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

1 个答案:

答案 0 :(得分:1)

您可以尝试类似的操作,只需修改类名

@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);
}