0

I have some problems with a few lines of code which remains to reach 100 percent.

if (object== null) {
            errors.reject("some error");
        } 

Project uses Spring Framework and its class org.springframework.validation.Errors How can i make a unit test on this fragment.

The second fragment that i can not test is this.

for (int i = 0; i < model.getName().length(); i++) {
                int x = (int) model.getName().charAt(i);
                if (x < 33 || x > 126) {
                    errors.rejectValue("name", "some error");
                    break;
                }
            }

I hope that some one will help :)

EDIT:

public void validate() {
if (object== null) {
            errors.reject("some error");
        } 

        if (model.getName().equals("")) {
            errors.rejectValue("name", "some error");
        } else if (model.getName().length() < 6) {
            errors.rejectValue("name", "some error");
        } else if (model.getName().length() > 30) {
            errors.rejectValue("name", "some error");
        } else {
            for (int i = 0; i < model.getName().length(); i++) {
                int x = (int) model.getName().charAt(i);
                if (x < 33 || x > 126) {
                    errors.rejectValue("name", "some error");
                    break;
                }
            }
        }
}
rockStar
  • 9
  • 1

1 Answers1

0

Why are you trying to get 100% coverage?

As @engineer mentioned What is a reasonable code coverage % for unit tests (and why)?

It seems that the most likely culprit is insufficient branch-coverage. Meaning all the possible paths through your if condition (x < 33 || x > 126) are not evaluated. This is impossible as x will never be both greater than 126 and less than 33.

There are 4 Possible Branches through an if condition like this.

true || true << Both conditions will never evaluate to true at the same time.

false|| true

false||false

true || false

It is only possible to cover 3 of 4 branches.

Community
  • 1
  • 1
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
  • It's an *or* condition, not an *and* condition. It's certainly possible for `x` to be either less than 33 *or* more than 126. – Erwin Bolwidt Jan 07 '16 at 14:35
  • 1
    @ErwinBolwidt But not all 4 possibilities can be covered. – Manu Jan 07 '16 at 14:36
  • 2
    @rockStar , see http://stackoverflow.com/questions/90002/what-is-a-reasonable-code-coverage-for-unit-tests-and-why ... – ROMANIA_engineer Jan 07 '16 at 14:37
  • @Manu *which* 4 possibilities? I only see two, `x < 33` and `x > 126` and it's certainly - easily - possible to cover these – Erwin Bolwidt Jan 07 '16 at 14:37
  • @BlakeYarbrough There are only two *branches*, the branch where `x < 33 || x > 126` is `true` and the branch where it is `false`. The branches are created by the `if` statement, not by the components of the logical expression. You are listing the truth table of a boolean expression, but branches are something different. By your reasoning, almost no logical or expression would be possible to test because the left and right sides normally do *not* overlap – Erwin Bolwidt Jan 08 '16 at 00:59