我开发了下一个目标类
class Person{
public Person(){}
public Person(String name) {
super();
this.name = name;
}
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
下一个控制器:
@Controller
private static class SampleController {
@RequestMapping(value="/path", method=RequestMethod.POST)
public String path(@Valid @ModelAttribute("person") Person person, BindingResult result, Model model) {
model.addAttribute("name",person.getName());
System.out.println(person.getName());
return "view";
}
}
和下一个测试:
public class ModelAssertionTests {
private MockMvc mockMvc;
@Before
public void setup() {
SampleController controller = new SampleController("a string value", 3, new Person("a name"));
this.mockMvc = standaloneSetup(controller)
.defaultRequest(get("/"))
.alwaysExpect(status().isOk())
.build();
}
@Test
public void testTest() throws Exception {
Person person = new Person("name");
mockMvc.perform(post("/path").sessionAttr("person", person));
}
}
开启调试模式
在这一行:
mockMvc.perform(post("/path").sessionAttr("person", person));
我看到
当我转到控制器方法时:
model.addAttribute("name",person.getName());
我看到
它的原因是什么?
如何解决?