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 · 8 comments
Open

Generate assertions for Java records #212

maciejwalkowiak opened this issue May 12, 2023 · 8 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.

@timo-a
Copy link

timo-a commented Jul 22, 2024

Hi @Berlizov,

I cannot make your hack work in my example project (using the gradle plugin for the generator), am I missing something?
The job summary shows the generated file: https://github.com/timo-a/microservice-example/actions/runs/10048164798
I'm just running generateAssertJ to sidestep the javax annotations that would cause a build to fail.
Locally, I'm running $ ./gradlew :generateAssertJ, PRs against the branch demo/assertj/records-workaround-with-annotation will trigger a new run.

Update: I tried it with the maven plugin as well, to no avail (I hope I'm overlooking something). As before in gradle: The job summary shows the generated file: https://github.com/timo-a/debug-assertj-maven-plugin/actions/runs/10125183976, repository to reproduce, PRs against demo/assertj/records-workaround-with-annotation will trigger a rerun.
Locally the commands are mvn compile; mvn assertj:generate-assertions.

@timo-a
Copy link

timo-a commented Jul 27, 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.

I don't mean to create any pressure @scordio, I appreciate what is there already, I'm busy myself and cannot promise to contribute anything either. However, seeing as there has been a new release since your comment, and there is a PR for this issue already, is there anything outside contributors can do to speed up the resolution of this particular issue? Maybe tackle a number of higher priority issues?

While not as important as the core, I think that in the scope of custom assertion generation this is a key issue for projects with Java 17+ as a lot of the types to generate custom assertions for can be records.

@scordio
Copy link
Member

scordio commented Jul 27, 2024

@timo-a yes, definitely. Such issues are not forgotten 🙂

We couldn't spare time for the generator in the last months but I plan to jump on it in August.

@scordio
Copy link
Member

scordio commented Jul 28, 2024

Team decision: We will start the development of the next major version with Java 17 as a baseline to better aid records and jakarta support.

@scordio scordio pinned this issue Jul 28, 2024
@timo-a
Copy link

timo-a commented Sep 12, 2024

Gentle check-in, is there something outside contributors can help to get this issue resolved? Does the java 17 baseline for instance imply any steps to be taken, like updating the workflow?

@scordio
Copy link
Member

scordio commented Sep 15, 2024

Thanks for the offer, @timo-a! We're already working on it.

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

4 participants