assertj/assertj-examples

Assertions generator doesn't generate for inner classes

baztian opened this issue · 3 comments

I did not manage to generate assertions for public static inner classes. I have a lot of these inner classes as they are generated jaxb.

assertj-examples has an example of assertion generation for static inner class, using Book.Title class:

public class Book {

  private Title title;

  public Book(String title) {
    this.title = new Title(title);
  }

  public Title getTitle() {
    return title;
  }

  public static class Title {
    private String title;
    public Title(String title) {
      super(); and
      this.title = title; ``
    }

    public String getTitle() {
      return title;
    }
  }
}

It generates assertions for both Book and Book.Title, here's Book.Title abstract assertion class:

public abstract class AbstractBookTitleAssert<S extends AbstractBookTitleAssert<S, A>, A extends Book.Title> 
                      extends AbstractAssert<S, A> {

  /**
   * Creates a new <code>{@link AbstractBookTitleAssert}</code> to make assertions on actual Book.Title.
   * @param actual the Book.Title we want to make assertions on.
   */
  protected AbstractBookTitleAssert(A actual, Class<S> selfType) {
    super(actual, selfType);
  }

  /**
   * Verifies that the actual Book.Title's title is equal to the given one.
   * @param title the given title to compare the actual Book.Title's title to.
   * @return this assertion object.
   * @throws AssertionError - if the actual Book.Title's title is not equal to the given one.
   */
  public S hasTitle(String title) {
    // check that actual Book.Title we want to make assertions on is not null.
    isNotNull();

    // overrides the default error message with a more explicit one
    String assertjErrorMessage = "\nExpected title of:\n  <%s>\nto be:\n  <%s>\nbut was:\n  <%s>";

    // null safe check
    String actualTitle = actual.getTitle();
    if (!Objects.areEqual(actualTitle, title)) {
      failWithMessage(assertjErrorMessage, actual, title, actualTitle);
    }

    // return the current assertion for method chaining
    return myself;
  }
}

and the corresponding concrete assertion class:

public class BookTitleAssert extends AbstractBookTitleAssert<BookTitleAssert, Book.Title> {

  /**
   * Creates a new <code>{@link BookTitleAssert}</code> to make assertions on actual Book.Title.
   * @param actual the Book.Title we want to make assertions on.
   */
  public BookTitleAssert(Book.Title actual) {
    super(actual, BookTitleAssert.class);
  }

  /**
   * An entry point for BookTitleAssert to follow AssertJ standard <code>assertThat()</code> statements.<br>
   * With a static import, one can write directly: <code>assertThat(myBook.Title)</code> and get specific assertion with code completion.
   * @param actual the Book.Title we want to make assertions on.
   * @return a new <code>{@link BookTitleAssert}</code>
   */
  public static BookTitleAssert assertThat(Book.Title actual) {
    return new BookTitleAssert(actual);
  }
}

How different your classes are ?
Can you provide an example ?

no feedback, closing the issue.

Maybe the problem arised with the use of the CLI launcher *.cli.AssertionGeneratorLauncher (e.g. in a build script) and its option -H for hierarchical generation. Unfortunately with this option no inner classes' assertions will be generated automatically. You have to explicitly provide these inner classes as arguments package.OuterClass$InnerClass then the assertions will be generated.