Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to use Hamcrest matchers in production code?

Tags:

java

hamcrest

I'd like to use hamcrest as sugar framework to use in if statements, not in the unit tests with asserts, but in raw production code.

Something like

if ( isNotEmpty(name) ) return //....

or

if ( isEqual(name, "John")) return //...

Just like AssertThat but without throwing errors, just returning boolean. Is it possible?

like image 864
Vitamon Avatar asked Nov 30 '11 21:11

Vitamon


Video Answer


2 Answers

It's just Java, it's up to you what you choose to do with it. The Hamcrest homepage says:

Provides a library of matcher objects (also known as constraints or predicates) allowing 'match' rules to be defined declaratively, to be used in other frameworks. Typical scenarios include testing frameworks, mocking libraries and UI validation rules.

Note: Hamcrest it is not a testing library: it just happens that matchers are very useful for testing.

There is also a page on the other frameworks that use Hamcrest.

like image 194
skaffman Avatar answered Sep 19 '22 16:09

skaffman


There's the bool project that provides the following syntax:

if(the(name, is(equalTo("Alex")))) {
...
}
like image 45
Arend v. Reinersdorff Avatar answered Sep 18 '22 16:09

Arend v. Reinersdorff