Unit Testing – Do I Need a Unit Test for Each Class in My Project?

Architecturejavajunitproject-structureunit testing

I have recently tried to to implement unit tests on a Java Web Application my project is built on MVC design architecture and uses Spring & JPA Hibernate and JSF here is a package tree

-src
  -entity
  -dao
    -impl
  -service
    -impl
  -rest
    -impl
  -web
  -utils
  -config
  -common
     -enums
     -exceptions

So do I need to write a unit test for the following classes :

utils/DateUtils.java
entity/User.java
web/RegisterUserBeanAction.java
dao/UserDao.java
commons/enums/UserTypeEnum.java

If yes so I wonder how testing a class like User.java just has some properties with setters and getters would be

If no so what the rule then ?

Best Answer

Business rules are the most important thing to test. Those should be in behavior objects (objects that DO things) not in value objects (getters and setters).

Testing should never be done blindly or by rote. It should help you refactor and go fast. If it's not doing that something is wrong.

There are things that don't have a strong need to be tested. Like GUI's that have no logic. Now that doesn't mean you can't test them. But testing a value object in a strongly typed language is duplicating what the type system already does for you.

Related Topic