如何使用Junit测试链调用方法?

时间:2018-10-24 02:34:31

标签: java unit-testing junit method-chaining

我是单元测试的新手。我有一堂课可以打电话 .setUserName(...)。setUserID(...)。toJson如何对此进行JUnit测试?

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;
  }

}

我需要测试每种设置方法吗?像

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

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

我不确定。

0 个答案:

没有答案