【问题标题】:After exception HashMap returning null in Junit Test在 Junit Test 中异常 HashMap 返回 null 之后
【发布时间】:2020-04-06 02:12:35
【问题描述】:

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

我们知道,在 JPA 中,如果实体没有被主键找到,那么在删除时就会发生异常。异常后,在 HasMap 中作为键“状态”将被放入,作为值字符串“失败”。

这个 HashMap 将由 DAO 删除方法返回,我的意图是在 JUnit 中使用它进行断言。现在的问题是:成功删除它工作正常,但如果我试图删除一个不存在的实体,那么键值分配在 Dao 方法中工作,但是当我试图在 JUnit 测试中访问它时方法,它显示为空。即使我在 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}

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

可能的原因是什么?

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

【问题讨论】:

    标签: java spring junit


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 2013-09-14
      • 2014-04-25
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2011-05-23
      相关资源
      最近更新 更多