How to Write the test case for DAO class with JUnit5 and Mockito in spring boot for the code
I am trying to write a test case with JUnit5 and Mockito. How to write the test cases for the below method
DAO class:
public Map<String, Object> addParticipantSubRole(ProductLineParticipantSubRoleDTO subRole, int userId, int buId,int plId) throws RTDataBaseException {
Map<String, Object> returnMap = new HashMap<>();
try {
StoredProcedureQuery query = this.getSession()
.createStoredProcedureCall("PKG_QA_PRODUCT_LINE_ADMIN.PROC_ADD_PARTICIP_SUB_ROLES")
.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN).setParameter(1, userId)
.registerStoredProcedureParameter(2, Integer.class, ParameterMode.IN).setParameter(2, plId)
.registerStoredProcedureParameter(3, String.class, ParameterMode.IN)
.setParameter(3, subRole.getParticipantSubRoleName())
.registerStoredProcedureParameter(4, String.class, ParameterMode.IN)
.setParameter(4, subRole.getParticipantSubRoleDesc())
.registerStoredProcedureParameter(5, Integer.class, ParameterMode.OUT)
.registerStoredProcedureParameter(6, Integer.class, ParameterMode.OUT)
.registerStoredProcedureParameter(7, String.class, ParameterMode.OUT)
.registerStoredProcedureParameter(8, String.class, ParameterMode.OUT);
int plParticipantSubRoleId = (Integer) query.getOutputParameterValue(5);
int returnId = (Integer) query.getOutputParameterValue(6);
String message = (String) query.getOutputParameterValue(7);
if (returnId == Constants.SUCCESS_INTEGER_VALUE) {
returnMap.put(RESULT, plParticipantSubRoleId);
returnMap.put(RETURN_ID, returnId);
returnMap.put(RETURN_MESSAGE, message);
} else {
returnMap.put(RESULT, subRole);
returnMap.put(RETURN_ID, returnId);
returnMap.put(RETURN_MESSAGE, message);
}
} catch (Exception exception) {
log.error(exception.toString());
throw new RTDataBaseException(userId, "Failed to add PL Sub Role in DB", userId, exception);
}
return returnMap;
}
It makes close to zero sense to do unit testing on the persistance layer. You get a lot of code with close to zero benefit.
Its better to go for an integration test which can look like this ...
@DataJpaTest
class MyDaoTest {
@Autowired private MyDao myDao;
@Test
void testMyDao(){
// Do test setup
myDay.addParticipantSubRole(/* input parameters */)
// Do asserts
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.