Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java unit-test framework that auto-tests getters and setters? [closed]

There is a well-known debate in Java (and other communities, I'm sure) whether or not trivial getter/setter methods should be tested. Usually, this is with respect to code coverage. Let's agree that this is an open debate, and not try to answer it here.

There have been several blog posts on using Java reflection to auto-test such methods.

Does any framework (e.g. jUnit) provide such a feature? e.g. An annotation that says "this test T should auto-test all the getters/setters on class C, because I assert that they are standard".

It seems to me that it would add value, and if it were configurable, the 'debate' would be left as an option to the user.

like image 585
Michael Easter Avatar asked Sep 20 '08 16:09

Michael Easter


People also ask

Do you unit test getters and setters?

Unit tests are there to test the behaviour of your code, in an expressive and meaningful way, and getters/setters are only a means to an end. If you tests use the getters/setters to achieve their goal of testing the "real" functionality, then that's good enough.

What is automated unit testing in Java?

Automated is generally the preferred method as it saves the developer a lot of time and effort. To do unit testing, your developer needs to write clear test code to test a specific function against an expected outcome. The tests will then run and any errors or failures to produce the expected outcome will be logged.


1 Answers

I created the OpenPojo project for solving this exact problem.

The project allows you to validate:

  • Enforce Pojo coding standard (i.e. All fields private, or no native variables, ...etc)
  • Enforce Pojo behaviour (i.e. setter does JUST setting, no transformation, etc)
  • Validate Pojo Identity (i.e. Use annotation based equality & hashcode generation)

See Tutorial

like image 194
Osman Shoukry Avatar answered Oct 09 '22 09:10

Osman Shoukry