Duplicated assertion method with target class implementing method from interface

オープン
#278 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
java
領域
tooling

調査の方向性

ソースファイルもテストも指定されていません。まず、インターフェースと実装のメソッドをアサーションメソッドに変換するジェネレーターの経路を特定し、次にインターフェース id() とそれを実装するクラスでこのケースを再現してください。生成によって重複した hasId メソッドが生成されなくなり、その動作が回帰テストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Java
スター
72
フォーク
47
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

assertj/assertj-generator のほかの issue

assertj/assertj-generator の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。