简体   繁体   English

如何更改构造函数参数?

[英]How to change constructor arguments?

I have a record that performs a verification in its constructor as such :我有一个在其构造函数中执行验证的记录:

public record Configuration(URI url) {
  public Configuration(URI url) {
    Validate.httpValid(url, "url");
  }
}

Where the httpValid method is : 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;
  }

This however fails the test i'm trying to create :然而,这未能通过我尝试创建的测试:

@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();
  }

This is because EqualsVerifier is trying to create an object with "x" as argument : InvalidPropertyValueException: The value x is not a valid httpValid as url这是因为 EqualsVerifier 试图创建一个以“x”为参数的对象: InvalidPropertyValueException: The value x is not a valid httpValid as url

You're very close.你很亲近。 You shouldn't provide the class that you're testing as a prefab value;你不应该提供你正在测试的类作为预制值; instead you need to provide the paramter that's causing trouble, like this:相反,您需要提供导致问题的参数,如下所示:

@Test
void Should_RespectEqualsContract() {
    EqualsVerifier
        .forClass(Configuration.class)
        .withPrefabValues(
            URI.class,
            URI.create("http://a.com"),
            URI.create("http://b.com"))
        .verify();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM