assertj-assertions-generator icon indicating copy to clipboard operation
assertj-assertions-generator copied to clipboard

Should assertions for properties of the superclass(es) be generated?

Open jcassee opened this issue 9 years ago • 3 comments

Consider this class:

import org.apache.http.client.HttpResponseException;

public class SomeErrorException extends HttpResponseException {
	private SomeError error;

	public SomeErrorException(int statusCode, String reason, SomeError error) {
		super(statusCode, reason);
		this.error = error;
	}

	public SomeError getError() {
		return error;
	}
}

It would be nice to be able to test SomeErrorException objects like this:

assertThat(exception).hasStatusCode(500)
                     .hasMessage("Internal Server Error")
                     .hasError(error)

However, hasStatusCode and hasMessage are not generated because they are defined in superclasses. If I include HttpResponseException in the generator configuration (in pom.xml), the hasStatusCode appear, but of course hasMessage is only present if I also include the whole hierarchy (four classes!) up to Throwable. Also, org.assertj.core.api.Assertions.assertThat(Throwable) already exists, so I cannot make my assertions a subclass (but problem with this idea are already discussed in the documentation).

Does it make sense to make it easier to include assertions for properties of superclasses?

jcassee avatar Nov 25 '16 11:11 jcassee

Maybe generating methods for properties of superclasses should even be the default?

PascalSchumacher avatar Nov 25 '16 18:11 PascalSchumacher

@jcassee in version 2.0.0, the generator by default generates assertions for inherited properties, which version are you using ?

joel-costigliola avatar May 20 '17 10:05 joel-costigliola

This is not the case when you have an interface extending an interface with default implementations, then assertions for the super class is not available in the subclass.

gwendo avatar Oct 02 '20 12:10 gwendo