Writing Test Cases
1. Writing Test Cases for POJO classes
Most of the time we need to write test cases for POJO classes. So that Test case Coverage can shoot up. We need to identify the package where we have pojo classes and count the number of POJO classes present on that package. Add 1 into the total count which will include the JUnit test class for itself. For example if there are 4 POJO classes for which you wish to write test cases, write the "EXPECTED_CLASS_COUNT = 5;" into your test case.
Here is the example:--
Add dependency in POM.XML:-
<dependency>
<groupId>com.openpojo</groupId>
<artifactId>openpojo</artifactId>
<version>0.8.4</version>
</dependency>
2. Write Test case for a micro-service Controller. Say the contoller looks like the following one:-
The JUnit test cases for the above contoller is:-
3. Write a JUnit test case for a service layer class:-
JUnit case for the above service class is:-
4. Write JUnit test case for DAO Class. Say the DAO Class is :-
JUnit test case for above DAO class ( in Hibernate) needs to be develiped in such a way so that it should only the functionality of DAO and should not hit actual DB anyway:
Comments