All Projects → yandex-qatools → Hamcrest Pojo Matcher Generator

yandex-qatools / Hamcrest Pojo Matcher Generator

Licence: apache-2.0
Autogenerated java hamcrest matchers for pojo with help of AnnotationProcessor

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Hamcrest Pojo Matcher Generator

annotation-processor-sample
An annotation processor which implements "Builder pattern" for your java classes.
Stars: ✭ 22 (-29.03%)
Mutual labels:  annotation-processor
Parceler
📦 Android Parcelables made easy through code generation.
Stars: ✭ 3,589 (+11477.42%)
Mutual labels:  annotation-processor
Derive4j
Java 8 annotation processor and framework for deriving algebraic data types constructors, pattern-matching, folds, optics and typeclasses.
Stars: ✭ 511 (+1548.39%)
Mutual labels:  annotation-processor
Immutables
Annotation processor to create immutable objects and builders. Feels like Guava's immutable collections but for regular value objects. JSON, Jackson, Gson, JAX-RS integrations included
Stars: ✭ 3,031 (+9677.42%)
Mutual labels:  annotation-processor
Pojobuilder
A Java Code Generator for Pojo Builders
Stars: ✭ 326 (+951.61%)
Mutual labels:  annotation-processor
Mapstruct
An annotation processor for generating type-safe bean mappers
Stars: ✭ 4,710 (+15093.55%)
Mutual labels:  annotation-processor
therapi-runtime-javadoc
Read Javadoc comments at run time.
Stars: ✭ 50 (+61.29%)
Mutual labels:  annotation-processor
Showkase
🔦 Showkase is an annotation-processor based Android library that helps you organize, discover, search and visualize Jetpack Compose UI elements
Stars: ✭ 873 (+2716.13%)
Mutual labels:  annotation-processor
Preferenceroom
🚚 Android processing library for managing SharedPreferences persistence efficiently and structurally.
Stars: ✭ 341 (+1000%)
Mutual labels:  annotation-processor
Paperparcel
Auto-generate the fastest possible Parcelable implementations for Java and Kotlin
Stars: ✭ 494 (+1493.55%)
Mutual labels:  annotation-processor
Blade
Android library for boilerplate destruction
Stars: ✭ 278 (+796.77%)
Mutual labels:  annotation-processor
Jackdaw
Java Annotation Processor which allows to simplify development
Stars: ✭ 306 (+887.1%)
Mutual labels:  annotation-processor
Deeplinkdispatch
A simple, annotation-based library for making deep link handling better on Android
Stars: ✭ 4,108 (+13151.61%)
Mutual labels:  annotation-processor
Doma
DAO oriented database mapping framework for Java 8+
Stars: ✭ 257 (+729.03%)
Mutual labels:  annotation-processor
Kotshi
An annotation processor that generates Moshi adapters from immutable Kotlin data classes.
Stars: ✭ 656 (+2016.13%)
Mutual labels:  annotation-processor
Dexter
Manage multidexing using simple annotations and gradle tasks.
Stars: ✭ 41 (+32.26%)
Mutual labels:  annotation-processor
Android Api Securekeys
Store data in a simple and secure way
Stars: ✭ 372 (+1100%)
Mutual labels:  annotation-processor
Koloboke
Java Collections till the last breadcrumb of memory and performance
Stars: ✭ 909 (+2832.26%)
Mutual labels:  annotation-processor
Android State
A utility library for Android to save objects in a Bundle without any boilerplate.
Stars: ✭ 857 (+2664.52%)
Mutual labels:  annotation-processor
Onactivityresult
OnActivityResult annotation compiler for Android
Stars: ✭ 470 (+1416.13%)
Mutual labels:  annotation-processor

Hamcrest Feature Matcher Generator for POJOs

release Maven Central

Inspired by lot of dummy work to create matchers for fields in auto-generated beans to write POJO-based tests.

Generates Hamcrest's Feature Matchers for every field annotated with @GenerateMatcher annotation (ru.yandex.qatools.processors.matcher.gen.annotations.GenerateMatcher).

Useful for testing auto-generated unmarshalled beans with help of jsonschema2pojo + Gson or JAXB

Usage Guide

1. Add dependency from Maven Central

<dependency>
    <groupId>ru.yandex.qatools.processors</groupId>
    <artifactId>feature-matcher-generator</artifactId>
    <version>2.0.0</version>
    <!-- 'provided' scope because this is only needed during compilation -->
    <scope>provided</scope>
</dependency>

2. Generate (or write) beans

@GenerateMatcher // works with class level annotations as every field with same annotation
public class Owner {
    @GenerateMatcher
    private String email;
    @GenerateMatcher
    private String uid;
    @DoNotGenerateMatcher // excludes field from generation of matchers for class level @GenerateMatcher annotation
    private String name;

    public String getEmail() {
        return email;
    }

    public String getUid() {
        return uid;
    }
}

Beans MUST have getters with named get[Field]() to generate working matchers

3. Compile

Run mvn clean compile

Also:

  • Can use any other annotation to process if override system property matcher.gen.annotations.
  • Can use multiply annotations comma-separated.

For example we have @Expose annotation (com.google.gson.annotations.Expose) by Gson and want to generate matchers for fields with such annotation.

  • Run compilation with
mvn clean compile -Dmatcher.gen.annotations=ru.yandex.qatools.processors.matcher.gen.annotations.GenerateMatcher,com.google.gson.annotations.Expose
  • Or put in classpath matchers.gen.properties with content
 matcher.gen.annotations=ru.yandex.qatools.processors.matcher.gen.annotations.GenerateMatcher,com.google.gson.annotations.Expose

4. See generated matchers

You can find result in ${project.build.directory}/generated-sources/annotations/*.
For each class with such fields will be generated class *Matchers with public static methods looks like:

/**
 * Matcher for {@link ru.yandex.qatools.beans.Owner#email}
 */
@org.hamcrest.Factory
public static org.hamcrest.Matcher<ru.yandex.qatools.beans.Owner> withEmail(org.hamcrest.Matcher<java.lang.String> matcher) {
    return new org.hamcrest.FeatureMatcher<ru.yandex.qatools.beans.Owner, java.lang.String>(matcher, "email", "email") {
        @Override
        protected java.lang.String featureValueOf(ru.yandex.qatools.beans.Owner actual) {
            return actual.getEmail();
        }
    };
}

5. Use it in your tests!

assertThat(someOwner, withEmail(containsString("@"))); 

// OR combined: 

assertThat(someOwner, both(withEmail(containsString("@"))).and(withUid(is(uid))); 

How to produce matchers in test scope for JAXB generated classes

There are 2 ways:

  1. Generate as usually in separate module and just add as dependency in test scope
  2. Generate all classes as test sources.

Generating JAXB classes and matchers as test sources

  1. Add one more execution for maven-jaxb2-plugin:
<execution>
    <id>test</id>
    <phase>process-test-sources</phase>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <generateDirectory>target/generated-test-sources/xjc</generateDirectory>
        <addCompileSourceRoot>false</addCompileSourceRoot>
        <addTestCompileSourceRoot>true</addTestCompileSourceRoot>
        <args>
          <!--bunch of plugins that can be different from original configuration-->
          <arg>-enableIntrospection</arg>
          <arg>-no-header</arg>
          <arg>-Xxew</arg>
          <arg>-Xxew:instantiate lazy</arg>
          <arg>-Xfluent-api</arg>
          <arg>-Xinheritance</arg>
          <arg>-Xannotate</arg>
          <arg>-Xvalue-constructor</arg>
          <arg>-Xequals</arg> <!--in tests it will be useful to have to strings, equals...-->
          <arg>-XhashCode</arg>
          <arg>-XtoString</arg>
         </args>
      </configuration>
</execution>
  1. If you want to add toString, equals etc methods, don't forget to add also dependency to test scope:
<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics</artifactId>
    <scope>test</scope>
</dependency>
  1. Add dependency to annotaion processor to <scope>test</scope>
  2. Add matchers.gen.properties file to src/test/resources/ with content:
matcher.gen.annotations=javax.xml.bind.annotation.XmlElement

How to debug Annotation Processors

With remote debugger on maven build

  1. Run in shell export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n"
  2. Run mvn clean compile
  3. Connect with remote debugger to port 8000

With Intellij IDEA

  1. Open "Maven Projects" view
  2. Disclose "Yandex QATools Annotation Processors" element
  3. Right-click on Maven phase "package"
  4. Click on "Debug 'annotation-processors'"

It will create a new launcher configuration which can be used to debug project.

NOTE

Not supported: EmptyClass -> EmptyStaticNested -> StaticNestedWithFields

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].