尽管实际值和预期值相同,但JUnit给出了CompareFailure [英] JUnit gives ComparisonFailure although actual and expected are the same

查看:116
本文介绍了尽管实际值和预期值相同,但JUnit给出了CompareFailure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jsoup从网站解析一个字符串,并编写了以下测试以验证该解析

I am trying to parse a string from a website using Jsoup and wrote the following test to verify that the parsing

这是我的测试

@Test
public void extractBookData() throws Exception {
    String bookLink = ""; //some address
    Document doc = Jsoup.connect(bookLink).get().html();

    Book book = new Book();

    assertEquals("Literatür Yayıncılık", book.getPublisher(doc));
}

这是getPublisher(Element)方法:

public String getPublisher(Element element){
    String tableRowSelector = "tr:contains(Yayınevi)";
    String tableColumnSelector = "td";
    String tableRowData = "";
    element = element.select(tableRowSelector).last();
    if (element != null) {
          element = element.select(tableColumnSelector).last();
          if (element != null) {
                tableRowData = element.text().replaceAll(tableRow.getRowName() + " ?:", "").replaceAll(tableRow.getRowName() + " :?", "").replaceAll(" ?: ?", "").trim();
          }
    }
    return tableRowData;
}

问题在于,即使JUnit另有说明,实际的字符串和期望的字符串也会出现相同的情况.

The problem is that the actual and expected strings appears the same even though JUnit tells otherwise.

我愿意接受您的建议.

推荐答案

我之前也遇到过同样的问题,这是文本中的不间断空格(字符160),而不是空格(字符32).在我的情况下,文本来自html文本输入值,您的文本看起来也来自html.

I have had this same issue before, this is a non-breaking space (char 160) wich is in your text instead of a space (char 32). In my case the text came from an html text input value, yours looks like it hes also come from html.

我使用的解决方案就是用空格替换所有不间断的空格字符.

The solution I used was just too replace all non breaking space chars with a space.

这篇关于尽管实际值和预期值相同,但JUnit给出了CompareFailure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆