【问题标题】:Assert Equals having issue in Java断言 Equals 在 Java 中存在问题
【发布时间】:2014-07-16 07:25:56
【问题描述】:

我正在编写一个单元测试用例,其中我无法比较两个字符串,因为对象内部有</> 标签。

我的输出是

{"description":"<p>License forms information can be download as attachments<\/p>"}

我的预期输出是:

"{\"description\":\"<p>License forms information can be download as attachments</p>\"}";

我遇到问题的唯一地方是&lt;/p&gt; 标签。当我更改为 &lt;\/p&gt; 时,它会引发语法错误。任何建议将不胜感激。

我的代码

WebResource webResource = client.resource("http://myip:8080/Kalgudi/restService");
    ClientResponse response = webResource.type("application/xml").get(ClientResponse.class);        
    String output = response.getEntity(String.class);

    JSONObject json = new JSONObject(output);

    System.out.println("Json object is:"+json.getJSONObject("6824"));


    String expected = "{\"description\":\"<p>License forms information can be download as attachments</p>\"}";




    assertEquals(expected, json.getJSONObject("6824"));

【问题讨论】:

  • 嗯,也许测试用例代码是个好主意?
  • @cyborg,用我的代码更新。请检查一下。
  • 好的,所以无法回答这个问题,因为我们根本不知道您的 Web 服务正在做什么来生成带有不正确结束标记的 JSON。这大概是您需要担心的系统。

标签: java unit-testing


【解决方案1】:

我猜是因为没有足够的信息来说明您没有正确转义字符串,这在What are all the escape characters in Java? 中得到了简洁的回答。断言、相等或单元测试似乎不是问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多