However, as I started using them and learning their power, I have seen that I can really use mocking in ways that enhance my unit tests. Here is a concrete example. I am using Spring Data JPA. So, I need to mock the save of my entity when it gets created in the function that I am unit testing. However, once I am mocking I can easily also validate the parameters sent to this function that did the save. Using Mockito, we use the ArgumentCaptor, to do this validation as follows:
This now means my unit test has validated that all parameters passed to the function were passed to the database.MyRepository myRepository = mock(MyRepository.class); ArgumentCaptor<EntityClass> argument = ArgumentCaptor.forClass(EntityClass.class); //... invoke function to unit Test that will internally save Entity verify(myRepository).saveAndFlush(argument.capture()); EntityClass entity = argument.getValue(); assert entity.getName().equals(“name”); assert entity.getAddress().equals("address");
Now we see some real added value in the mock, where we can be confident that our copy of the data to the entity we are persisting has not missed any of the parameters.
Another nice feature in mocking is checking how many times a function is invoked. Modifying the the code above:
MyRepository myRepository = mock(MyRepository.class); ArgumentCaptor<EntityClass> argument = ArgumentCaptor.forClass(EntityClass.class); //... invoke function to unit Test that will internally save Entity verify(myRepository,times(1)).saveAndFlush(argument.capture()); EntityClass entity = argument.getValue(); assert entity.getName().equals(“name”); assert entity.getAddress().equals("address");
Happy Mocking!
No comments:
Post a Comment