Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate assertions for Java records #212

Open
maciejwalkowiak opened this issue May 12, 2023 · 2 comments
Open

Generate assertions for Java records #212

maciejwalkowiak opened this issue May 12, 2023 · 2 comments

Comments

@maciejwalkowiak
Copy link

No description provided.

@Berlizov
Copy link

Records are supported, but through a hack:

import org.assertj.assertions.generator.GenerateAssertion;

@GenerateAssertion
public record Item(String name, Integer value) {}

If you add the @GenerateAssertion, it generates a full Assert class:

@javax.annotation.Generated(value="assertj-assertions-generator")
public abstract class AbstractItemAssert<S extends AbstractItemAssert<S, A>, A extends Item> extends AbstractObjectAssert<S, A> {
  protected AbstractItemAssert(A actual, Class<S> selfType) {...}
  public S hasHashCode(int hashCode) {...}
  public S hasName(String name) {...}
  public S hasToString(String toString) {...}
  public S hasValue(Integer value) {...}
}

But there are three disadvantages:

  • need to add the annotation to each record, or to a class that not uses old-school getter notation (getFoo);
  • not everyone wants to add a new dependency (org.assertj:assertj-assertions-generator) to a project just for one annotation;
  • this approach generates garbage hasHashCode and hasToString methods. But of course, all standard AbstractAssert's methods such as asString remain.

@scordio
Copy link
Member

scordio commented May 12, 2024

We are currently focusing on the next release for AssertJ. As soon as that is done, we'll revamp the generator projects and address these additional topics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants