Best Practices For Unit Testing in Java
Best Practices For Unit Testing in Java
In Jav
By Ramesh Fadatare (Java Guides)
a
Also, it avoids any possibility of running test code in the production environment.
givenEmployeeObject_whenSaveEmployee_thenReturnSavedEmploye
givenEmployeesList_whenFindAll_thenReturnListOfEmployee
givenEmployeeObject_whenUpdateEmployee_thenReturnUpdatedEmploye
Appropriate Assertions
Always use proper assertions to verify the expected vs. actual results. We
should use various methods available in the Assert class of JUnit or similar
frameworks like AssertJ.
Mock External Service
Although unit tests concentrate on speci c and smaller pieces of code, there is a
chance that the code is dependent on external services for some logic.
Therefore, we should mock the external services and merely test the logic and
execution of our code for varying scenarios.
We can use various frameworks like Mockito, EasyMock, and JMockit for mocking
external services.
Email Service
fi
s
fi
fi