spring - Unit test on java code -


i have problems few lines of code remains reach 100 percent.

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

project uses spring framework , class org.springframework.validation.errors how can make unit test on fragment.

the second fragment can not test this.

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

i hope 1 :)

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 {             (int = 0; < model.getname().length(); i++) {                 int x = (int) model.getname().charat(i);                 if (x < 33 || x > 126) {                     errors.rejectvalue("name", "some error");                     break;                 }             }         } } 

why trying 100% coverage?

as @engineer mentioned what reasonable code coverage % unit tests (and why)?

it seems culprit insufficient branch-coverage. meaning possible paths through if condition (x < 33 || x > 126) not evaluated. impossible x never both greater 126 , less 33.

there 4 possible branches through if condition this.

true || true << both conditions never evaluate true @ same time.

false|| true

false||false

true || false

it possible cover 3 of 4 branches.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -