0

I am new to unit test. I have a class which can make a chain call like .setUserName(...).setUserID(...).toJson How can I make the JUnit test about this?

public class Event{

private String userName;
private int userID;
private String userAddress;

public Event setUserName(String userName){
   this.userName = userName;
   return this;
}

public Event setUserID(String userID){
   this.userID = userID;
   return this;
}

public Event setUserAddress(String userAddress){
   this.userAddress = userAddress;
   return this;
}

   public JSONObject toJson(){
   JSONObject json = new JSONObject();

     if(null != userName)
     json.put("userName", userName);
     if(0 != userID)
     json.put("userID", userID);
     if(null != userAddress)
     json.put("userAddress", userAddress);
     return json;
  }

}

Do I need to test each of the set methods? something like

 @Test
 public void testSetUserName() {
 Event event = new Event();

 Assert.IsNotNull(event.setUserName("somename")); 
 }

I am not sure about that.

HenlenLee
  • 435
  • 1
  • 11
  • 30
  • Actually, you could even test if `event` is returned as result. And, of course, if the method is doing what it is supposed to do (i.e. setting the user name field). – Henry Oct 24 '18 at 03:33
  • The only public API is the toJSON() method. If you aren't going to expose any getters for the properties, then just test the JSON output is correct. That will already test that the properties are set. – Paul Samsotha Oct 24 '18 at 04:47
  • [It is impossible to answer your question because you do not provide a specification of what your code ought to do](https://stackoverflow.com/a/53757321/545127). – Raedwald Dec 13 '18 at 08:49

0 Answers0