我有一个在其构造函数中执行验证的记录,如下所示:
public record Configuration(URI url) {
public Configuration(URI url) {
Validate.httpValid(url, "url");
}
}
方法在哪里httpValid
:
public static URI httpValid(final URI value, final String property) {
try {
value.toURL();
} catch (IllegalArgumentException | MalformedURLException e) {
throw new InvalidPropertyValueException(property, "httpValid", value, Map.of());
}
return value;
}
但是,这未能通过我尝试创建的测试:
@Test
void Should_RespectEqualsContract() {
EqualsVerifier
.forClass(Configuration.class)
.withPrefabValues(
Configuration.class,
new Configuration(URI.create("http://a.com")),
new Configuration(URI.create("http://b.com")))
.verify();
}
这是因为 EqualsVerifier 试图创建一个以“x”为参数的对象:
InvalidPropertyValueException: The value x is not a valid httpValid as url