testing - junit test case with null object -
i want write person class 3 parameter constructor , if user give null string name , surname person, want return null object because want use junit assertnull function show object not created null string.
public person(string names,string surnames,int ages) { if(!names.equals(null) && !surnames.equals(null)) { name = names; surname = surnames; } else { return; } if(ages > 0) age = ages; else return; }
and test
@test public void createperson() { string ad = null; string soyad = null; int age = 10; person p = new person(ad, soyad, age); assertnull("object not created!", p ); }
how can null pointer exception?
you nullpointerexception because calling equals method on null object. if want check whether string not null, need use somestring != null
.
what need is:
if (names != null && surnames != null)
but not solve problem not possible return null constructor. thing ist throw nullpointerexception—and code does. there must other way solve problem have.
Comments
Post a Comment