Skip to content

Generate the Truth extensions for value objects for Java 8+.

License

Notifications You must be signed in to change notification settings

t28hub/auto-truth

Repository files navigation

AutoTruth

GitHub Actions FOSSA Status Quality Gate Status Coverage

Generate the Truth extensions for value objects for Java 8+.

Usage

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 {}
}

Supported types

AutoTruth supports the following types:

Primitive types

  • boolean
  • byte
  • char
  • short
  • int
  • long
  • float
  • double

Array types

  • boolean[]
  • byte[]
  • char[]
  • short[]
  • int[]
  • long[]
  • float[]
  • double[]
  • Object[]

Java8 types

  • Optional
  • OptionalInt
  • OptionalLong
  • OptionalDouble
  • Stream
  • IntStream
  • LongStream

Other JDK types

  • Enum
  • Object
  • Class
  • String
  • Iterable
  • Map

Installing

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.

Gradle

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"
}

Maven

See GitHub Help.

License

FOSSA Status

About

Generate the Truth extensions for value objects for Java 8+.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors 4

  •  
  •  
  •  
  •