Generate the Truth extensions for value objects for Java 8+.
Add the @AutoTruth
annotation to a custom subject class and specify a value object class.
This example uses Employee.java.
@AutoSubject(Employee.class)
class EmployeeSubject {
}
Then AutoTruth generates the AutoEmployeeSubject
that is declared the following methods:
hasUsername(String)
hasId(long)
hasName(String)
hasLocation(Location)
isCeo()
isNotCeo()
You can extends the AutoEmployeeSubject
as follows, if the generated methods are not enough.
@AutoSubject(Employee.class)
public class EmployeeSubject extends AutoEmployeeSubject {
private final Employee actual;
EmployeeSubject(FailureMetadata failureMetadata, Employee actual) {
super(failureMetadata, actual);
this.actual = actual;
}
public static EmployeeSubject assertThat(Employee actual) {
return Truth.assertAbout(EmployeeSubject::new).that(actual);
}
public LocationSubject location() {
final Location actual = this.actual.location();
return check("location()").about(LocationSubject::new).that(actual);
}
@AutoSubject(Employee.Location.class)
public static class LocationSubject {}
}
AutoTruth supports the following types:
boolean
byte
char
short
int
long
float
double
boolean[]
byte[]
char[]
short[]
int[]
long[]
float[]
double[]
Object[]
Optional
OptionalInt
OptionalLong
OptionalDouble
Stream
IntStream
LongStream
Enum
Object
Class
String
Iterable
Map
The AutoTruth packages are available on the GitHub Packages.
You need an access token to install packages in GitHub Packages.
Instructions for creating an access token is described in GitHub Help.
repositories {
mavenCentral()
jcenter()
maven {
url 'https://maven.pkg.github.com/t28hub/auto-truth'
credentials {
username = 'YOUR_GITHUB_USERNAME'
password = 'YOUR_GITHUB_TOKEN'
}
}
}
dependencies {
testImplementation "com.google.truth:truth:$LATEST_TRUTH_VERSION"
testImplementation "com.google.truth.extensions:truth-java8-extension:$LATEST_TRUTH_VERSION"
testImplementation "io.t28.auto:auto-truth-annotations:$LATEST_VERSION"
testAnnotationProcessor "io.t28.auto:auto-truth-processor:$LATEST_VERSION"
}
See GitHub Help.