Search code examples
unit-testingjunitmongotemplate

How to unit test mongotemplate functions


I want to write unit test for mongotemplate function calls. I am not sure how to write that, I have seen instances of integration test case only on web.

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

Solution

  • You can try something like this, just modify class name

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