https://github.com/yandex-qatools/hamcrest-pojo-matcher-generator
Autogenerated java hamcrest matchers for pojo with help of AnnotationProcessor
https://github.com/yandex-qatools/hamcrest-pojo-matcher-generator
annotation-processor hamcrest matcher
Last synced: 3 months ago
JSON representation
Autogenerated java hamcrest matchers for pojo with help of AnnotationProcessor
- Host: GitHub
- URL: https://github.com/yandex-qatools/hamcrest-pojo-matcher-generator
- Owner: yandex-qatools
- License: apache-2.0
- Created: 2014-10-19T17:30:56.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-06-11T19:00:20.000Z (over 8 years ago)
- Last Synced: 2025-05-05T02:36:04.278Z (5 months ago)
- Topics: annotation-processor, hamcrest, matcher
- Language: Java
- Homepage:
- Size: 109 KB
- Stars: 33
- Watchers: 13
- Forks: 9
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Hamcrest Feature Matcher Generator for POJOs
[](https://github.com/yandex-qatools/hamcrest-pojo-matcher-generator/releases/latest)
[](https://maven-badges.herokuapp.com/maven-central/ru.yandex.qatools.processors/feature-matcher-generator)Inspired by lot of dummy work to create matchers for fields in auto-generated beans to write POJO-based tests.
Generates [Hamcrest](http://hamcrest.org/JavaHamcrest/)'s [Feature Matchers](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/FeatureMatcher.html)
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](https://github.com/joelittlejohn/jsonschema2pojo) + [Gson](https://code.google.com/p/google-gson/) or JAXB### Usage Guide
#### 1. Add dependency from Maven Central
```xmlru.yandex.qatools.processors
feature-matcher-generator
2.0.0
provided```
#### 2. Generate (or write) beans
```java
@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](https://code.google.com/p/google-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
```properties
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:```java
/**
* Matcher for {@link ru.yandex.qatools.beans.Owner#email}
*/
@org.hamcrest.Factory
public static org.hamcrest.Matcher withEmail(org.hamcrest.Matcher matcher) {
return new org.hamcrest.FeatureMatcher(matcher, "email", "email") {
@Override
protected java.lang.String featureValueOf(ru.yandex.qatools.beans.Owner actual) {
return actual.getEmail();
}
};
}
```#### 5. Use it in your tests!
```java
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`:
```xml
test
process-test-sources
generate
target/generated-test-sources/xjc
false
true
-enableIntrospection
-no-header
-Xxew
-Xxew:instantiate lazy
-Xfluent-api
-Xinheritance
-Xannotate
-Xvalue-constructor
-Xequals
-XhashCode
-XtoString
```2. If you want to add `toString`, `equals` etc methods, don't forget to add also dependency to test scope:
```xml
org.jvnet.jaxb2_commons
jaxb2-basics
test
```3. Add dependency to annotaion processor to `test`
4. 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
2. Right-click on Maven phase `"package"`
3. 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`