gpt4 book ai didi

java - 在 Junit 测试中异常 HashMap 返回 null 后

转载 作者:行者123 更新时间:2023-12-02 08:46:24 24 4
gpt4 key购买 nike

在我的项目中,在 DAO 类中,在删除方法中,作为删除响应,我返回了一个 HashMap。我使用过 JPA 和 Spring Boot。在 Try-Catch 中,成功删除后,将在映射中放置键“status”和值字符串“OK”。

众所周知,在JPA中,如果通过主键找不到实体,在删除时就会发生异常。发生异常后,在 HasMap 中将放置键“status”和值字符串“FAIL”。

这个 HashMap 将由 DAO 删除方法返回,我的目的是在 JUnit 中使用它进行断言。现在的问题是:它工作正常,可以成功删除,但是如果我尝试删除一个不存在的实体,那么键值分配在 Dao 方法中工作,但是当我尝试在 JUnit 测试中访问它时方法,显示为null。即使我在delete方法中打印了HasMap,它也显示了它的键值,但在Junit中,同时显示了null。

单元测试代码

@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"));
}

案例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;
}

输出

Profile Basic Delete Fail!
response (dao) : {status=FAIL}
Test Delete Fail!
response (test) : {}

案例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;
}

输出

Profile Basic Delete Fail!
response (dao) : {status=FAIL}
Test Delete Fail!
response (test) : {}

案例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;
}

输出:

Profile Basic Delete Fail!
response (dao) : {status=FAIL}
Test Delete Fail!
response (test) : {}

如果删除成功,消息如下:

    response (dao) : {status=OK}
//hibernate sql show
response (test) : {status=OK}

到目前为止,我可以理解,对于异常(exception),意味着当尝试删除空实体时,HasMap键值对分配是在Dao删除方法中,但正在返回到测试方法。

可能的原因是什么?

代码完整代码可以在GitHub中访问:Porject in GitHub这是一个开源项目,欢迎您做出贡献。

最佳答案

您正在使用嵌套的 JPA 方法(查找和删除)如果标识符为 null,find 方法将抛出异常。

关于java - 在 Junit 测试中异常 HashMap 返回 null 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61052011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com