Java APT generator for Hamcrest matcher library. It automatically generate a matcher builder class for Java Beans.
For example, for Java bean
@Value
@Builder
public class TestBean01 {
String stringValue;
}
it will generate the following matcher builder
public class TestBean01Matcher extends AbstractBeanMatcher<TestBean01> {
public static TestBean01Matcher create() {
return new TestBean01Matcher();
}
public static TestBean01Matcher testBean01Matcher() {
return create();
}
protected TestBean01Matcher add(String propertyName, final Matcher<?> matcher) {
addPropertyMatcher(propertyName, matcher);
return this;
}
public TestBean01Matcher withStringValue(Matcher<? super String> matcher) {
return add("stringValue", matcher);
}
public TestBean01Matcher withStringValue(String testValue) {
return withStringValue(equalTo(testValue));
}
}
The generated matcher builder you can use in your tests
@ParameterizedTest
@MethodSource
void testTestBean01(Matcher<TestBean01> matcher) {
TestBean01 bean = TestBean01.builder()
.stringValue("value01")
.build();
assertThat(bean, matcher);
}
private static Stream<Arguments> testTestBean01() {
return Stream.of(
Arguments.of(testBean01Matcher().withStringValue("value01")),
Arguments.of(testBean01Matcher().withStringValue(StringContains.containsStringIgnoringCase("lUe"))));
}
For each property generates general method accepted Matcher for this property:
withPropertyName(Matcher<PropertyType>)
- generic method allows to construct arbitrary matcher
withPropertyName(PropertyType)
- exact property value, underlines calls theMatchers.equalTo
method
withProperyNameSize(int)
- allows to check container size (except Iterable)hasPropertyName(ValueType)
- alows to check if container has specific entry.
withProperyNameSize(int)
- allows to check map sizehasPropertyName(KeyType, ValueType)
- alows to check if map contions specific key/value entry.
For each bean property the generator create a coresponded matcher builder class automatically.
For a property MyAnotherBean beanValue;
will be generated MyAnotherBeanMatcher
add the following project dependency to pom.xml
<dependency>
<groupId>org.jresearch.hamcrest.beanMatcher</groupId>
<artifactId>org.jresearch.hamcrest.beanMatcher.annotation</artifactId>
<version>${beanMatcher.version}</version>
<scope>test</scope>
</dependency>
and configure annotation processor
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.jresearch.hamcrest.beanMatcher</groupId>
<artifactId>org.jresearch.hamcrest.beanMatcher.processor</artifactId>
<version>${beanMatcher.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
After that add @BeanMatcher(TestBean01.class)
to your test package (to do it create package-info.java and add put the annotation into it)
@BeanMatcher(TestBean02.class)
package org.jresearch.hamcrest.beanmatcher.test;
import org.jresearch.hamcrest.beanmatcher.annotation.BeanMatcher;
See the test module for more information