【发布时间】:2019-05-23 09:56:33
【问题描述】:
使用 Mocking 和单元测试助手更喜欢 Spring 测试:
模拟服务替换多个依赖项
@Controller
@RequestMapping("/people")
public class PeopleController {
@Autowired
protected PersonService personService;
@GetMapping
public ModelAndView people(Model model) {
for (Person person: personService.getAllPeople()) {
model.addAttribute(person.getName(), person.getAge());
}
return new ModelAndView("people.jsp", model.asMap());
}
}
私有 MockMvc mockMvc:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PeopleControllerTest {
@Autowired
PersonService personService;
private MockMvc mockMvc;
@Configuration
static class Config {
// Other beans
@Bean
public PersonService getPersonService() {
return mock(PersonService.class);
}
}
@Test
public void testPeople() throws Exception {
// When
ResultActions actions = mockMvc.perform(get("/people"));
}
}
我想运行mockMvc时出错
java.lang.NullPointerException
【问题讨论】:
-
mockMvc是什么? -
@Ravi 服务器端 Spring MVC 测试的主要入口点
-
你能分享这门课吗?
标签: java spring-mvc spring-boot junit mockito