equalsverifier

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


Solution

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