Java – is it acceptable to use the hamcrest matcher in non test code

I tried to find a clear answer to using hamcrest matchers in non - Test code I have done some research and have some comparative References:

>Hamcrest of Wikipedia:

Solution

So far, I have used hamcrest. In non - Test code many times When I want to test different conditions on the same object, I use it to get a report on which conditions failed for what reason Then the single condition is represented as different objects, which also have some other good effects For example, checking the application configuration can produce the results of what operations support data

The reasons why I particularly use hamcrest for such tasks are:

>Its purpose is to do this (the test conditions are not only completed in the test code) > it will not bring additional dependencies > as we all know, many people have used it for test code > it has an easy-to-use and small API > it is easy to expand and supports composition very well

Finally, it comes down to choosing the right tool For example, bean validation can be used to perform relatively similar work Thoughtful decisions need to be made, which also includes the requirements of the development process and environment

Using matchers is also a good way to use the tell, do not ask principle You can pass a matcher to a method to indicate the return value you want to return If the object in question does not match the matcher, an exception with a good error description can be thrown immediately

In addition, when comparing the use of matchers with Java 8 predicates, the advantage of matchers is that they can provide descriptions, but the disadvantage is that they are not functional interfaces themselves

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>