URL 的 Scala 测试用例
Scala test case with URL
我有一个案例 class 有一个 URL 字段:
import java.net.URL
case class A(a: URL)
我正在写一个测试用例,它有以下断言。
result.url 是从案例 class.
中得到的
result.url should equal(new java.net.URL("https://hostpath/"))
result.url 打印我:
我的测试用例失败,说它们不相等。
当您使用 new 关键字时,它会一起创建一个新对象,并且它会失败,具体取决于基础 URLs 的 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);
}
我有一个案例 class 有一个 URL 字段:
import java.net.URL
case class A(a: URL)
我正在写一个测试用例,它有以下断言。 result.url 是从案例 class.
中得到的result.url should equal(new java.net.URL("https://hostpath/"))
result.url 打印我:
我的测试用例失败,说它们不相等。
当您使用 new 关键字时,它会一起创建一个新对象,并且它会失败,具体取决于基础 URLs 的 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);
}