2014-10-06 72 views
1

說我有下面的測試案例play2 java窗體綁定 - 如何設置字段名稱映射到對象?

我希望能夠綁定駝峯參數:

anyData.put("my_id", "[email protected]"); 

我怎樣才能得到這個測試通過?

public class FormBindingExampleTest { 

    public static class FormBindingExampleModel { 
     public String myid; 
     public String email; 

     public String getMyid() { 
      return myid; 
     } 

     public void setMyid(String myid) { 
      this.myid = myid; 
     } 

     public String getEmail() { 
      return email; 
     } 

     public void setEmail(String email) { 
      this.email = email; 
     } 
    } 

    @Test 
    public void itShouldBindForm(){ 
     Form<FormBindingExampleModel> userForm = form(FormBindingExampleModel.class); 

     Map<String,String> anyData = new HashMap(); 
     anyData.put("my_id", "[email protected]"); 
     anyData.put("email", "secret"); 
     FormBindingExampleModel user = userForm.bind(anyData).get(); 

     System.out.println(user.myid); 

     assert(user.myid.equals("[email protected]")); 
    } 
} 

回答

0

使用表格的fill()方法以填充具有現有值的表格。

@Test 
public void itShouldBindForm(){ 
    Form<FormBindingExampleModel> userForm = form(FormBindingExampleModel.class); 

    FormBindingExampleModel formModel = new FormBindingExampleModel(); 

    formModel.setMyid("[email protected]"); 
    formModel.setEmail("secret"); 

    userForm.fill(formModel); 

    FormBindingExampleModel user = userForm.get(); 

    System.out.println(user.getMyid); 

    assert(user.getMyid.equals("[email protected]")); 
} 

可用的文檔here

相關問題