Java Junit-Spring-测试集合
您能建议谁使用JUnit4和SpringMVC测试集合吗 控制器Java Junit-Spring-测试集合,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,您能建议谁使用JUnit4和SpringMVC测试集合吗 控制器 @Controller public class PersonController { @Autowired private PersonService personService; @RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET) public String getPerson(Model model) {
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET)
public String getPerson(Model model) {
model.addAttribute("personData", personService.getPerson);
return "personPage";
}
}
可以通过以下方式测试Person类:
public class TestPersonController {
@Mock
private PersonService personService;
@InjectMocks
private PersonController personController;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(personController).build();
}
@Test
public void testGetPerson() throws Exception {
when(personService.getPerson(1L)).thenReturn(new Person(1L, "StackOverflow"));
mockMvc.perform(get("/person/{id}", 1L))
.andExpect(status().isOk())
.andExpect(view().name("personPage"))
.andExpect(model().attribute("personData",
allOf(hasProperty("id", is(1L)),
hasProperty("name", is("StackOverflow")))));
}
}
但是我不知道如何测试perService.getPerson是否返回列表 您可以使用与现在相同的方法对其进行测试,但要使用相应的
匹配器
实例来检查集合
元素
假设您有一个处理程序方法,如
@RequestMapping(value = { "/persons",}, method = RequestMethod.GET)
public String getPersons(Model model) {
model.addAttribute("personList", personService.getPersons());
return "personsPage";
}
您只需更改mock以返回其他内容
when(personService.getPersons()).thenReturn( // get some Person objects in a List
Arrays.asList(new Person(1L, "StackOverflow"), new Person(1L,
"StackOverflow")));
并使用匹配器执行新请求,该匹配器对列表中返回的元素进行相应的检查。比如说
mockMvc.perform(get("/persos"))
.andExpect(status().isOk())
.andExpect(view().name("personsPage"))
.andExpect(
model().attribute(
"personList",
Matchers.everyItem(AllOf.allOf(
HasPropertyWithValue.hasProperty("id", Is.is(1L)),
HasPropertyWithValue.hasProperty("name", Is.is("StackOverflow"))))));
您可以使用与现在相同的方法对其进行测试,但要使用相应的Matcher
实例来检查Collection
元素
假设您有一个处理程序方法,如
@RequestMapping(value = { "/persons",}, method = RequestMethod.GET)
public String getPersons(Model model) {
model.addAttribute("personList", personService.getPersons());
return "personsPage";
}
您只需更改mock以返回其他内容
when(personService.getPersons()).thenReturn( // get some Person objects in a List
Arrays.asList(new Person(1L, "StackOverflow"), new Person(1L,
"StackOverflow")));
并使用匹配器执行新请求,该匹配器对列表中返回的元素进行相应的检查。比如说
mockMvc.perform(get("/persos"))
.andExpect(status().isOk())
.andExpect(view().name("personsPage"))
.andExpect(
model().attribute(
"personList",
Matchers.everyItem(AllOf.allOf(
HasPropertyWithValue.hasProperty("id", Is.is(1L)),
HasPropertyWithValue.hasProperty("name", Is.is("StackOverflow"))))));
让我们看看你的控制器。另外,你在测试列表
返回的对象时尝试了什么?@SotiriosDelimanolis-我添加了控制器代码。很抱歉,我不知道如何执行此操作,我尝试对集合运行相同的Junit代码,但(如预期)失败。让我们看看您的控制器。另外,您尝试了什么来测试列表
返回的对象?@SotiriosDelimanolis-我添加了控制器代码。很抱歉,我不知道怎么做,我尝试用集合运行相同的Junit代码,但(如预期的)失败了。