Imagine unit testing the following sample scenario. Let’s say I’ve mocked out the CustomerDAO in order to return a valid customer and customer orders. Testing this scenario is fairly easy. Except when I get to testing the boolean for whether or not a customer has orders. In some real world scenarios they will not have orders. So do I need to add some condition in my mock DAO that will return a customer with no orders, and then test that as well? Now imagine it is much more complicated and there are several pieces of the DTO that could contain various bits of information depending on the real results coming back from the database. Do I need to test all of those various conditions?
public class Manager {
public CustomerDTO getCustomerInformation() {
CustomerDAO customerDAO = new CustomerDAO();
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.setCustomer(customerDAO.getCustomer(1));
customerDTO.setCustomerOrders(customerDAO.getCustomerOrders(1));
if (!customerDTO.getCustomerOrders.isEmpty()) {
customerDTO.setHasCustomerOrders(true);
}
return customerDTO;
}
}
In short, I think yes you should test that when the DAO returns various things that the expected state exists on the DTO. Otherwise how can you have an confidence that the DTO will be an accurate representation of the data that was in the datastore?
You should create either a mock DAO per test with the required data for the test, or a mock DAO for each state that needs to be tested.
I’d probably have tests like:
CustomerHasOrders_WhenDaoReturnsNoOrders_ReturnsFalseCustomerHasOrders_WhenDaoReturnsOrders_ReturnsTrueGetCustomer_WhenDaoReturnsCustomer_CustomerIsSameGetCustomerOrders_WhenDaoReturnsOrders_OrdersAreTheSameGetCustomerOrders_WhenDaoReturnsNoOrders_OrdersAreEmptyand then tests for what happens if any of the calls to the Dao fail…
In this example it seems that the flag is redundant in the DTO, as it is just different representations of other data. You could implement that as an extension method on CustomerDTO, or some logic in the setCustomerOrders. If the DTO is to be sent over the wire and the functionality won’t neccessarily be there then you could exclude the property and the client could just do the check to see if there are any orders in the same way you are
customerDTO.getCustomerOrders.isEmpty()