public class BaseAssertionGenerator extends Object implements AssertionGenerator, AssertionsEntryPointGenerator
Constructor and Description |
---|
BaseAssertionGenerator()
Creates a new
with default templates directory. |
BaseAssertionGenerator(String templatesDirectory)
Creates a new
with the templates from the given directory. |
Modifier and Type | Method and Description |
---|---|
String |
generateAssertionsEntryPointClassContentFor(Set<ClassDescription> classDescriptionSet,
AssertionsEntryPointType assertionsEntryPointType,
String entryPointClassPackage)
Returns the assertions entry point class content for the given
ClassDescription set. |
File |
generateAssertionsEntryPointClassFor(Set<ClassDescription> classDescriptionSet,
AssertionsEntryPointType assertionsEntryPointType,
String entryPointClassPackage)
Same as
AssertionsEntryPointGenerator.generateAssertionsEntryPointClassContentFor(Set, AssertionsEntryPointType, String) but in addition
create the corresponding java class file. |
protected void |
generateAssertionsForDeclaredFieldsOf(StringBuilder contentBuilder,
ClassDescription classDescription) |
protected void |
generateAssertionsForDeclaredGettersOf(StringBuilder contentBuilder,
ClassDescription classDescription) |
protected void |
generateAssertionsForFields(StringBuilder assertionsForPublicFields,
Set<FieldDescription> fields,
ClassDescription classDescription) |
protected void |
generateAssertionsForFieldsOf(StringBuilder contentBuilder,
ClassDescription classDescription) |
protected void |
generateAssertionsForGetters(StringBuilder assertionsForGetters,
Set<GetterDescription> getters,
ClassDescription classDescription) |
protected void |
generateAssertionsForGettersOf(StringBuilder contentBuilder,
ClassDescription classDescription) |
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<?>> classes)
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.
|
void |
setDirectoryWhereAssertionFilesAreGenerated(File targetBaseDirectory) |
void |
setGenerateAssertionsForAllFields(boolean generateAssertionsForAllFields) |
void |
setGeneratedAssertionsPackage(String generatedAssertionsPackage) |
public BaseAssertionGenerator() throws IOException
BaseAssertionGenerator
with default templates directory.IOException
- if some template file could not be found or readpublic BaseAssertionGenerator(String templatesDirectory) throws IOException
BaseAssertionGenerator
with the templates from the given directory.templatesDirectory
- path where to find templatesIOException
- if some template file could not be found or readpublic void setDirectoryWhereAssertionFilesAreGenerated(File targetBaseDirectory)
public void setGenerateAssertionsForAllFields(boolean generateAssertionsForAllFields)
public void setGeneratedAssertionsPackage(String generatedAssertionsPackage)
public File generateCustomAssertionFor(ClassDescription classDescription) throws IOException
AssertionGenerator
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; }
generateCustomAssertionFor
in interface AssertionGenerator
classDescription
- the ClassDescription
used to generate the assertions class.IOException
- if something went wrong when creating the assertion file.public File[] generateHierarchicalCustomAssertionFor(ClassDescription classDescription, Set<com.google.common.reflect.TypeToken<?>> allClasses) throws IOException
AssertionGenerator
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>
.generateHierarchicalCustomAssertionFor
in interface AssertionGenerator
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.public String[] generateHierarchicalCustomAssertionContentFor(ClassDescription classDescription, Set<com.google.common.reflect.TypeToken<?>> classes)
AssertionGenerator
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>
.generateHierarchicalCustomAssertionContentFor
in interface AssertionGenerator
classDescription
- the ClassDescription
used to generate the assertions
class.classes
- set of all classes that we are currently generating assertions
for. Used to find superclass assertions.public String generateCustomAssertionContentFor(ClassDescription classDescription)
AssertionGenerator
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; }
generateCustomAssertionContentFor
in interface AssertionGenerator
classDescription
- the ClassDescription
used to generate the assertions class.public String generateAssertionsEntryPointClassContentFor(Set<ClassDescription> classDescriptionSet, AssertionsEntryPointType assertionsEntryPointType, String entryPointClassPackage)
AssertionsEntryPointGenerator
ClassDescription
set.
The idea is to generate an equivalent of assertj-core Assertions class to give easy access to all generated
assertions. With AssertionsEntryPointType
parameter one can generate standard, BDD or Soft assertions entry
point class (default is AssertionsEntryPointType.STANDARD
).
You can define the package of the generated entry point class, if null the common base package of the given classes, will be used, i.e if some classe are in a.b.c and others in a.b.c.d, then entry point class will be in a.b.c.
generateAssertionsEntryPointClassContentFor
in interface AssertionsEntryPointGenerator
classDescriptionSet
- the set of ClassDescription we want to generate an entry point for.assertionsEntryPointType
- the valueType of entry point class to generateentryPointClassPackage
- the package of the generated entry point classpublic File generateAssertionsEntryPointClassFor(Set<ClassDescription> classDescriptionSet, AssertionsEntryPointType assertionsEntryPointType, String entryPointClassPackage) throws IOException
AssertionsEntryPointGenerator
AssertionsEntryPointGenerator.generateAssertionsEntryPointClassContentFor(Set, AssertionsEntryPointType, String)
but in addition
create the corresponding java class file.generateAssertionsEntryPointClassFor
in interface AssertionsEntryPointGenerator
classDescriptionSet
- class descriptionsassertionsEntryPointType
- entry point typeentryPointClassPackage
- entry point class package nameIOException
- if entry point file can't be created.protected void generateAssertionsForGettersOf(StringBuilder contentBuilder, ClassDescription classDescription)
protected void generateAssertionsForDeclaredGettersOf(StringBuilder contentBuilder, ClassDescription classDescription)
protected void generateAssertionsForGetters(StringBuilder assertionsForGetters, Set<GetterDescription> getters, ClassDescription classDescription)
protected void generateAssertionsForFieldsOf(StringBuilder contentBuilder, ClassDescription classDescription)
protected void generateAssertionsForDeclaredFieldsOf(StringBuilder contentBuilder, ClassDescription classDescription)
protected void generateAssertionsForFields(StringBuilder assertionsForPublicFields, Set<FieldDescription> fields, ClassDescription classDescription)
public void register(Template template)
AssertionGenerator
register
in interface AssertionGenerator
template
- templateCopyright © 2021 AssertJ. All rights reserved.