Is there a way to automate junit bean property tests?

Let's face it, writing bean property tests is arguably the worst use of time. But they must be made.

For instance. If you are testing a property Stringto propNamecall, for example:

testProperty(target, "propName", String.class);
testProperty(target, "propName", String.class, "expected initial");

It will be verified that:

  • There are methods
  • getand set.
  • When using the expected value, the test getcalls assertEqualsfor the given value.
  • ( get, is) / setbehave as expected.

I could start writing these implementations, but I want to know if there is anything to facilitate this. Other optional attributes can be used to verify that the nullJSR-303 bean is enabled or used to validate the field.

+5
source share
4 answers

There are quite a few existing libraries / code snippets that make this easier. When doing a quick search, I found several potential possibilities:

I saw someone take the first example I gave (beautiful and simple, because it is just one class) and modify it to better suit their needs.

+6
source

I can think about using reflection if you want to do it manually
or refer to this answer on a regular bet:

junit testing method for getters and setters

0
source

create another bean so that it looks like your test bean. and then use apache-commons, festAssert, hamcrest or any other structure that can perform a comparison using reflection. do not write it yourself, I'm sure it is already done

0
source

Update javabean tester above

There is an active active fork project for the above (but dead) code / javabean -tester, which seems pretty nice ...

0
source

All Articles