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

Popular posts from this blog

php - Magento - Deleted Base url key -

javascript - Tooltipster plugin not firing jquery function when button or any click even occur -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -