在 Junit 测试中,异常后 HashMap 返回 null。

huangapple go评论80阅读模式
英文:

After exception HashMap returning null in Junit Test

问题

以下为您提供的内容翻译:


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

正如我们所知,在JPA中,如果通过主键找不到实体,在删除时会引发异常。在异常之后,将在HasMap中放入键为"status"的键,值为字符串"FAIL"。

这个HashMap将由DAO删除方法返回,并且我的意图是在JUnit中使用它进行断言。现在的问题是:对于成功删除,它运行得很好,但是如果我尝试删除不存在的实体,那么键值赋值在Dao方法中是有效的,但是当我尝试在JUnit测试方法中访问它时,它显示为空。即使我在删除方法中打印了HasMap,它显示其键值,但在JUnit中,在同一时间,它显示为空。

单元测试代码

@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上访问:GitHub项目
这是一个开源项目,欢迎您进行贡献。

英文:

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&lt;String, String&gt; response = new HashMap&lt;&gt;();
        try {
            response = dao_Profile_Basic_I.delete(id);
        } catch (Exception e) {
            System.out.println(&quot;Test Delete Fail!&quot;);
        }
        System.out.println(&quot;response (test) : &quot; + response.toString());
        assertEquals(&quot;OK&quot;,response.get(&quot;status&quot;));
    }

Case 1

@Override
    public Map&lt;String, String&gt; delete(String id) {
        System.out.println(&quot;DAO Delete: &quot;);
        Map&lt;String, String&gt; response = new HashMap&lt;&gt;();
        String s = &quot;&quot;;
        try {
            ProfileBasic profileBasic = entityManager.find(ProfileBasic.class, new Long(id));
            entityManager.remove(profileBasic);
            s = &quot;OK&quot;;
        } catch (Exception e) {
            s = &quot;FAIL&quot;;
            System.out.println(&quot;Profile Basic Delete Fail!&quot;);
//            e.printStackTrace();
        }
        response.put(&quot;status&quot;, s);
        System.out.println(&quot;response (dao) : &quot;+ response.toString());
        return response;
    }

Output

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

Case 2

@Override
    public Map&lt;String, String&gt; delete(String id) {
        System.out.println(&quot;DAO Delete: &quot;);
        Map&lt;String, String&gt; response = new HashMap&lt;&gt;();
        try {
            ProfileBasic profileBasic = entityManager.find(ProfileBasic.class, new Long(id));
            entityManager.remove(profileBasic);
            response.put(&quot;status&quot;,&quot;OK&quot;);
        } catch (Exception e) {
            System.out.println(&quot;Profile Basic Delete Fail!&quot;);
            response.put(&quot;status&quot;,&quot;FAIL&quot;);
//            e.printStackTrace();
        }
        System.out.println(&quot;response (dao) : &quot;+ response.toString());
        return response;
    }

Output

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

Case 3

@Override
    public Map&lt;String, String&gt; delete(String id) {
        System.out.println(&quot;DAO Delete: &quot;);
        Map&lt;String, String&gt; response = new HashMap&lt;&gt;();
        ProfileBasic profileBasic=null;
        try {
            profileBasic = entityManager.find(ProfileBasic.class, new Long(id));
            response.put(&quot;status&quot;, &quot;OK&quot;);
        } catch (Exception e) {
            System.out.println(&quot;Profile Basic Delete Fail!&quot;);
            response.put(&quot;status&quot;, &quot;FAIL&quot;);
//            e.printStackTrace();
        }
        entityManager.remove(profileBasic);
        System.out.println(&quot;response (dao) : &quot; + 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.

答案1

得分: 1

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

英文:

You are using a nested JPA method (find and delete)
The find method will throw an exception if the identifier is null.

huangapple
  • 本文由 发表于 2020年4月6日 10:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61052011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定