assertj-assertions-generator icon indicating copy to clipboard operation
assertj-assertions-generator copied to clipboard

Support Java 8 Optional Feature

Open ghost opened this issue 10 years ago • 1 comments

Is it possible to support Java 8 Optional feature for the generator?

Sample model class:

public class Person {

    private String name;

    public Person(String name) {
        this.name = name;
    }

    public Optional<String> getName() {
        return name == null ? Optional.empty() : Optional.of(name);
    }
}

Generated assertion class:

public class PersonAssert extends AbstractAssert<PersonAssert, Person> {

  public PersonAssert hasName(java.util.Optional name) {
  }
}

Suggested assert class:

public class PersonAssert extends AbstractAssert<PersonAssert, Person> {

 // Generator might lookup generic type of the Optional and generate the method without Optional parameter
  public PersonAssert hasName(String name) {
  }

  public PersonAssert hasName(java.util.Optional name) {
  }
}

ghost avatar Jun 14 '15 19:06 ghost

It would be possible but not in the next version as the generator still relies on java 7 but ok for the java 8 version (3.0). In that future version, I would even not generate hasName(java.util.Optional name) but hasName(String name) and hasNoName() for the case the Optional in empty. We would end up with:

public class PersonAssert extends AbstractAssert<PersonAssert, Person> {

 // Generator might lookup generic type of the Optional and generate the method without Optional parameter
  public PersonAssert hasName(String name) {
     // check getName().getValue() is equal to name 
  }

  public PersonAssert hasNoName() {
     // check getName().isEmpty()
  }
}

joel-costigliola avatar Jun 14 '15 21:06 joel-costigliola