-1

i have a question regarding service layer testing in Spring framework. I have a class ProductService which has method:

public List<Product> getProducts() {
    return productDAO.getProducts();
}

Is there any way to test it properly? This method retrieve all products using dao layer, when i, for example fill a list with five products(which are in database right now) and test this method by asserting size of the list it seems useless because when i add one product it still passes, but it shouldn't - after adding one product it should return 6 not 5 products... If you have any usefull sites/yt videos about spring unit testing please let me know. Thanks for Your help in advance! Regards

thosenumbers
  • 55
  • 1
  • 10

1 Answers1

1

One technique you could use to test this particular code is to mock the productDAO using a framework. The mock object then provides specific results that you can test for in your unit tests.

Tague Griffith
  • 3,963
  • 2
  • 20
  • 24
  • thank you for your answer, im using mockito and hamcrest. Im using MockMvc and mockito for mocking dao. My question was how to properly cover this code like i wrote earlier when i mock productDao and hardcode that method return 5 products(for example) and after that i will put one more product into database it still passes but it shouldn't (there will be 6 products in database, and assert in unit test will expect 5 products. So maybe it's useless to cover this code with unit tests and i should write unit tests for DAO classes only? thanks for your answer! – thosenumbers Jul 01 '17 at 14:35