Reputation: 853
In my project, in DAO Class, in delete method, as delete response i have returned a HashMap. I have used JPA and Spring Boot. In Try-Catch, after successful delete, in map as key "status" will be put and as value string "OK".
As we know, in JPA, if entity not found by primary key, in time of delete exception will occur. After exception, in HasMap as key "status" will be put and as value string "FAIL".
This HashMap will be returned by the DAO delete method and my intention is to use it in JUnit for Assertion. Now the problem is : it is working fine for successful delete, but if i am trying to delete an entity which doesn't exist, then the Key value assignment is working in Dao method, but when i am trying to access it in JUnit test method, it is showing null. Even i have printed the HasMap in delete method, it showing its key-value but in Junit, in the same time, showing null.
Unit Test Code
@Test
public void test3_delete() {
id = getID();
Map<String, String> response = new HashMap<>();
try {
response = dao_Profile_Basic_I.delete(id);
} catch (Exception e) {
System.out.println("Test Delete Fail!");
}
System.out.println("response (test) : " + response.toString());
assertEquals("OK",response.get("status"));
}
Case 1
@Override
public Map<String, String> delete(String id) {
System.out.println("DAO Delete: ");
Map<String, String> response = new HashMap<>();
String s = "";
try {
ProfileBasic profileBasic = entityManager.find(ProfileBasic.class, new Long(id));
entityManager.remove(profileBasic);
s = "OK";
} catch (Exception e) {
s = "FAIL";
System.out.println("Profile Basic Delete Fail!");
// e.printStackTrace();
}
response.put("status", s);
System.out.println("response (dao) : "+ response.toString());
return response;
}
Output
Profile Basic Delete Fail!
response (dao) : {status=FAIL}
Test Delete Fail!
response (test) : {}
Case 2
@Override
public Map<String, String> delete(String id) {
System.out.println("DAO Delete: ");
Map<String, String> response = new HashMap<>();
try {
ProfileBasic profileBasic = entityManager.find(ProfileBasic.class, new Long(id));
entityManager.remove(profileBasic);
response.put("status","OK");
} catch (Exception e) {
System.out.println("Profile Basic Delete Fail!");
response.put("status","FAIL");
// e.printStackTrace();
}
System.out.println("response (dao) : "+ response.toString());
return response;
}
Output
Profile Basic Delete Fail!
response (dao) : {status=FAIL}
Test Delete Fail!
response (test) : {}
Case 3
@Override
public Map<String, String> delete(String id) {
System.out.println("DAO Delete: ");
Map<String, String> response = new HashMap<>();
ProfileBasic profileBasic=null;
try {
profileBasic = entityManager.find(ProfileBasic.class, new Long(id));
response.put("status", "OK");
} catch (Exception e) {
System.out.println("Profile Basic Delete Fail!");
response.put("status", "FAIL");
// e.printStackTrace();
}
entityManager.remove(profileBasic);
System.out.println("response (dao) : " + response.toString());
return response;
}
Output:
Profile Basic Delete Fail!
response (dao) : {status=FAIL}
Test Delete Fail!
response (test) : {}
For a successful delete, the message is like below:
response (dao) : {status=OK}
//hibernate sql show
response (test) : {status=OK}
So far, i could understand, for exception, means when trying to delete a null entity, HasMap key-value pair assigning is in Dao delete method, but is being returned to test method.
What may be the possible reason?
The code full code can be accessed in GitHub : Porject in GitHub It is an open source project, you are welcome to contribute.
Upvotes: 0
Views: 314
Reputation: 11
You are using a nested JPA method (find and delete) The find method will throw an exception if the identifier is null.
Upvotes: 1