【问题标题】:Scala test case with URL带有 URL 的 Scala 测试用例
【发布时间】:2020-09-01 12:48:53
【问题描述】:

我有一个包含 URL 字段的案例类:


    import java.net.URL

    case class A(a: URL)

我正在编写一个测试用例,它有以下断言。 result.url 是从case类中获取的。

result.url should equal(new java.net.URL("https://hostpath/"))

result.url 打印我:

https://hostpath/

我的测试用例失败了,说它们不相等。

【问题讨论】:

    标签: scala scala-collections


    【解决方案1】:

    当您使用 new 关键字时,它会一起创建一个新对象,并且它会失败,具体取决于底层 URL 的 equals 实现

    result.url.toString should equal(new java.net.URL("https://hostpath/").toString)
    

    如果你看一下 URL 的 equals 方法实现,它看起来像这样,并不像比较文本 URL 部分那么简单。

    
    protected boolean equals(URL u1, URL u2) {
            String ref1 = u1.getRef();
            String ref2 = u2.getRef();
            return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
                   sameFile(u1, u2);
        }
    
    protected boolean equals(URL u1, URL u2) {
            String ref1 = u1.getRef();
            String ref2 = u2.getRef();
            return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
                   sameFile(u1, u2);
        }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多