Java Bean Testing Scheme

Is there access to an infrastructure or library that, when provided by a JavaBean, "walks it through its steps", i.e. will check all getters and setters, confirm that the property matches getters and setters, etc.? p>

+4
source share
6 answers

Personally, I do not think that the most difficult part of testing. This can be done using reflection, but this is not what makes testing useful.

The hard part calculates all possible inputs, for a “happy journey” and error situations, making sure that exceptions are thrown when they were, etc.

Your Java Bean should implement equals and hashCode. I would be more worried about tests to verify an equal contract: zero equivalent, reflective, symmetric, transitive, not equal. This is not trivial.

Getters and setters are the least of your problems. When people talk about code coverage standards of 70% or better, they often say that getters and setters can be omitted.

+8
source

Although I agree that there are big problems to solve, there are cases for testing Java bean methods. Large teams of teams working on large code bases can run into problems. I have seen several cases of copy / paste errors causing getters or setters to work on the wrong property. Forgetting can lead to a hash code and equal methods becoming inconsistent. Finding errors in this simple code can be very annoying.

Bean Matches is a library that can help in this regard. It provides a series of Hamcrest combinations for seamlessly testing Java beans. For instance:

@Test public void testBean() { assertThat(MyBean.class, allOf( hasValidBeanConstructor(), hasValidGettersAndSetters(), hasValidBeanHashCode(), hasValidBeanEquals(), hasValidBeanToString() )); } 
+4
source

Take a look at reflection testing utilities:

http://code.google.com/p/rtu/

Although if you test the generated methods (based on the fields in the class), it may not be worth it.

+2
source

If you do not look like something more unusual, for example http://commons.apache.org/validator/ I would recommend writing your own. Personally, I don’t like clean data storage objects without any behavior - this is not a very good design. Therefore, if I am not forced to work with such objects (working with j2ee f. E.), I try to get rid of them.

+1
source

You can try http://oval.sourceforge.net/ Oval allows you to use annotations on your beans, and then execute the validation method. This is not fully consistent with JSR303. If you want to use something fully compatible, you should check out the Hibernate Validator.

Like previous states, definitely check apache validation.

+1
source

I think this library is the answer to your question: http://outsidemybox.github.com/testUtils/

it tests all initial values ​​of bean, setters, getters, hashCode (), equals () and toString (). All you have to do is define a default value map and default properties / values.

It can also test beans objects with additional constructors that do not use defaults.

0
source

Source: https://habr.com/ru/post/1316362/


All Articles