简体   繁体   中英

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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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