assertj / assertj/assertj-generator

Duplicated assertion method with target class implementing method from interface

Offen
#278 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
72
Forks
47
Ø Merge
1 T. 16 Std.
Gemergte PRs (30 T.)
2

Beschreibung

> Hi @scordio ,
>
> let me thank you first for your work. Because I was looking a long time that we can use records with AssertJ and can remove the templates.
>
> The new version works perfectly in our project. Unfortunately, I found a bug with M4/M5. But maybe isn't bug, because our clever programming isn't that clever ;). Should I open an issue for that?
>
> **Short summary of the bug**
> - We have an Interface X which has method called id()
> - We implement this Interface X in several classes. The typical implementation is
> - `String id = "someId"; public String id() { return id; }`
> - When AssertJ creates the *Assert classes it creates the method `hasId` twice, e.g.
> ```
> /**
> * Verifies that the actual Data's id is equal to the given one.
> * @param id the given id to compare the actual Data's id to.
> * @return this assertion object.
> * @throws AssertionError - if the actual Data's id is not equal to the given one.
> */
> public S hasId(String id) {
> // check that actual Data we want to make assertions on is not null.
> isNotNull();
>
> // overrides the default error message with a more explicit one
> String assertjErrorMessage = "\nExpecting id of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>";
>
> // null safe check
> String actualId = actual.getId();
> if (!Objects.deepEquals(actualId, id)) {
> failWithMessage(assertjErrorMessage, actual, id, actualId);
> }
>
> // return the current assertion for method chaining
> return myself;
> }
>
> /**
> * Verifies that the actual Data's id is equal to the given one.
> * @param id the given id to compare the actual Data's id to.
> * @return this assertion object.
> * @throws AssertionError - if the actual Data's id is not equal to the given one.
> */
> public S hasId(String id) {
> // check that actual Data we want to make assertions on is not null.
> isNotNull();
>
> // overrides the default error message with a more explicit one
> String assertjErrorMessage = "\nExpecting id of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>";
>
> // null safe check
> String actualId = actual.id();
> if (!Objects.deepEquals(actualId, id)) {
> failWithMessage(assertjErrorMessage, actual, id, actualId);
> }
>
> // return the current assertion for method chaining
> return myself;
> }
> ```
>
> **Simple workaround**
> Renaming the method `id()` to `getId()` solved the problem and all Assertions classes are created correctly.

_Originally posted by @schmuka0501 in https://github.com/assertj/assertj-assertions-generator-maven-plugin/issues/93#issuecomment-3183154411_

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Es wird keine Quelldatei oder kein Test genannt. Beginne damit, den Generatorpfad zu finden, der Interface- und Implementierungsmethoden in Assertion-Methoden umwandelt, und reproduziere dann den Fall mit einem Interface id() und einer implementierenden Klasse. Als erledigt gilt die Aufgabe, wenn die Generierung keine doppelten hasId-Methoden mehr erzeugt und das Verhalten durch einen Regressionstest abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.