I sat down to write a matcher today and decided to take a quick look at the jmock documentation to refresh my memory on the process, and noticed a reference to the org.hamcrest.Factory annotation. The documentation for the annotation states.
Marks a Hamcrest static factory method so tools recognise them. A factory method is an equivalent to a named constructor.
Do any tools actually use this annotation?
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.
Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.
As explained in the Hamcrest tutorial, the Factory
annotation is used by a Hamcrest code generator, org.hamcrest.generator.config.XmlConfigurator
. It generates a Java source file that contains all factories from a configured set of classes, so that you can statically import all of them by using a single static import.
I have not used this feature yet, because I manually collect my self-written matchers in a factory class as soon as I write the matcher, and on usage I statically import each factory method by itself (using the Eclipse "Favorites" feature for auto-import).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With