public interface AssertionGenerator
Modifier and Type | Method and Description |
---|---|
String |
generateCustomAssertionContentFor(ClassDescription classDescription)
Builds and returns the custom assertion java file content for the given
ClassDescription . |
File |
generateCustomAssertionFor(ClassDescription classDescription)
Builds and returns the custom assertion java file for the given
ClassDescription . |
String[] |
generateHierarchicalCustomAssertionContentFor(ClassDescription classDescription,
Set<com.google.common.reflect.TypeToken<?>> allClasses)
Generates hierarchical assertion classes for the class represented by the
given classDescription.
|
File[] |
generateHierarchicalCustomAssertionFor(ClassDescription classDescription,
Set<com.google.common.reflect.TypeToken<?>> allClasses)
Generates hierarchical assertion classes for the class represented by the given classDescription.
|
void |
register(Template template)
Registers a template in the internal TemplateRegistry so that customers can override default templates.
|
File generateCustomAssertionFor(ClassDescription classDescription) throws IOException
ClassDescription
.
Let's say we have the ClassDescription
corresponding to :
public class Race { private final String name; private final boolean immortal; public Race(String name, boolean immortal) { this.name = name; this.immortal = immortal; } public String getName() { return name; } public boolean isImmortal() { return immortal; } }We will generate assertions specific to
Race
in RaceAssert
class, like :
import static java.lang.String.format; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.Assertions; public class RaceAssert extends AbstractObjectAssert<RaceAssert, Race> { public RaceAssert(Race actual) { super(actual, RaceAssert.class); } public static RaceAssert assertThat(Race actual) { return new RaceAssert(actual); } public RaceAssert hasName(String name) { // check that actual Race we want to make assertions on is not null. isNotNull(); // we overrides the default error message with a more explicit one String assertjErrorMessage = format("Expected Race's name to be <%s> but was <%s>", name, actual.getName()); // check if (!actual.getName().equals(name)) { throw new AssertionError(assertjErrorMessage); } // return the current assertion for method chaining return this; } public RaceAssert isImmortal() { // check that actual Race we want to make assertions on is not null. isNotNull(); // we overrides the default error message with a more explicit one String assertjErrorMessage = format("Expected actual Race to be immortal but was not.", actual); // check if (!actual.isImmortal()) throw new AssertionError(assertjErrorMessage); // return the current assertion for method chaining return this; }
classDescription
- the ClassDescription
used to generate the assertions class.IOException
- if something went wrong when creating the assertion file.File[] generateHierarchicalCustomAssertionFor(ClassDescription classDescription, Set<com.google.common.reflect.TypeToken<?>> allClasses) throws IOException
public abstract class AbstractRaceAssert<S extends AbstractRaceAssert, T extends Race>
public final class RaceAssert extends AbstractRaceAssert<RaceAssert, Race>
classDescription
has a supertype with a known assertion class, then the generated abstract
assertion class will inherit from the superclass' abstract assertion class. Otherwise, it will inherit from
AbstractObjectAssert<S, T>
.classDescription
- the ClassDescription
used to generate the assertions class.allClasses
- set of all classes that we are currently generating assertions for. Used to find superclass
assertions.IOException
- if something went wrong when creating the assertion files.String generateCustomAssertionContentFor(ClassDescription classDescription) throws IOException
ClassDescription
.
Let's say we have the ClassDescription
corresponding to :
public class Race { private final String name; private final boolean immortal; public Race(String name, boolean immortal) { this.name = name; this.immortal = immortal; } public String getName() { return name; } public boolean isImmortal() { return immortal; } }We will generate assertions specific to
Race
in RaceAssert
class, like :
import static java.lang.String.format; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.Assertions; public class RaceAssert extends AbstractObjectAssert<RaceAssert, Race> { public RaceAssert(Race actual) { super(actual, RaceAssert.class); } public static RaceAssert assertThat(Race actual) { return new RaceAssert(actual); } public RaceAssert hasName(String name) { // check that actual Race we want to make assertions on is not null. isNotNull(); // we overrides the default error message with a more explicit one String assertjErrorMessage = format("Expected Race's name to be <%s> but was <%s>", name, actual.getName()); // check if (!actual.getName().equals(name)) { throw new AssertionError(assertjErrorMessage); } // return the current assertion for method chaining return this; } public RaceAssert isImmortal() { // check that actual Race we want to make assertions on is not null. isNotNull(); // we overrides the default error message with a more explicit one String assertjErrorMessage = format("Expected actual Race to be immortal but was not.", actual); // check if (!actual.isImmortal()) throw new AssertionError(assertjErrorMessage); // return the current assertion for method chaining return this; }
classDescription
- the ClassDescription
used to generate the assertions class.RuntimeException
- if something went wrong when creating the assertion content.IOException
- I/O exceptionString[] generateHierarchicalCustomAssertionContentFor(ClassDescription classDescription, Set<com.google.common.reflect.TypeToken<?>> allClasses)
public abstract class AbstractRaceAssert<S extends AbstractRaceAssert, T extends Race>
public final class RaceAssert extends AbstractRaceAssert<RaceAssert, Race>
classDescription
has a supertype with a known assertion class, then the generated abstract
assertion class will inherit from the superclass' abstract assertion class. Otherwise, it will inherit from
AbstractObjectAssert<S, T>
.classDescription
- the ClassDescription
used to generate the assertions
class.allClasses
- set of all classes that we are currently generating assertions
for. Used to find superclass assertions.RuntimeException
- if something went wrong when creating the assertion content.void register(Template template)
template
- templateNullPointerException
- if template is nullNullPointerException
- if template.getContent is nullCopyright © 2021 AssertJ. All rights reserved.