Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Kotlin class assertion generation problem

Open
#193 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java, kotlin
Domain
tooling

Research direction

Start with the Kotlin reproducer in Klass.kt and JavaClass.java, then inspect the generator path that produces AbstractKlassAssert. Confirm that the generated assertion methods are emitted only once and that the resulting Java class compiles.

Written by the indexing model from the issue text.

Description

There is a small problem:

Generator is creating assertion methods twice in Abstract***Assert classes for code where Kotlin class with lateinit collection field is generified with complex java class. Generated code cannot compile

Example to reproduce the problem:

Klass.kt:

class Klass {
  lateinit var collection: Collection<JavaClass>
}

JavaClass.java:

public class JavaClass {}

Result:

/**
 * Abstract base class for {@link Klass} specific assertions - Generated by CustomAssertionGenerator.
 */
@javax.annotation.Generated(value="assertj-assertions-generator")
public abstract class AbstractKlassAssert<S extends AbstractKlassAssert<S, A>, A extends Klass> extends AbstractObjectAssert<S, A> {

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

  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.getCollection(), collection);

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

  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements in Collection.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.getCollection(), collection.toArray());

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

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.getCollection(), collection);

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

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements in Collection and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.getCollection(), collection.toArray());

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

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.getCollection(), collection);

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

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements in Collection.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.getCollection(), collection.toArray());

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

  /**
   * Verifies that the actual Klass has no collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection is not empty.
   */
  public S hasNoCollection() {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // we override the default error message with a more explicit one
    String assertjErrorMessage = "\nExpecting :\n  <%s>\nnot to have collection but had :\n  <%s>";

    // check
    if (actual.getCollection().iterator().hasNext()) {
      failWithMessage(assertjErrorMessage, actual, actual.getCollection());
    }

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


  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.collection, collection);

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

  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements in Collection.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.collection, collection.toArray());

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

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.collection, collection);

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

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements in Collection and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.collection, collection.toArray());

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

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.collection, collection);

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

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements in Collection.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.collection, collection.toArray());

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

  /**
   * Verifies that the actual Klass has no collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection is not empty.
   */
  public S hasNoCollection() {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // we override the default error message with a more explicit one
    String assertjErrorMessage = "\nExpecting :\n  <%s>\nnot to have collection but had :\n  <%s>";

    // check
    if (actual.collection.iterator().hasNext()) {
      failWithMessage(assertjErrorMessage, actual, actual.collection);
    }

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


}
Dominant language
Java
Stars
72
Forks
47
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from assertj/assertj-generator

All issues in assertj/assertj-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.