【发布时间】:2019-01-17 12:21:39
【问题描述】:
我正在尝试测试以下控制器:
@GetMapping("movies")
public Page<Title> getAllMovies(@PageableDefault(value=2) Pageable pageable){
return this.titleService.getTitleByType("Movie", pageable);
}
这是测试类:
@RunWith(SpringRunner.class)
@WebMvcTest(TitleController.class)
@EnableSpringDataWebSupport
public class TitleControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private TitleService titleService;
// Test controller method - getAllMovies
@Test
public void getAllMovies() throws Exception {
Title title = new Title();
title.setId((short)1);
title.setName("The Godfather");
title.setType("Movie");
List<Title> titles = new ArrayList<>();
titles.add(title);
Page<Title> page = new PageImpl<>(titles);
given(this.titleService.getTitleByType("Movie", PageRequest.of(0,2))).willReturn(page);
mockMvc.perform(MockMvcRequestBuilders.get("/movies"))
.andExpect(status().isOk());
}
}
当我运行测试它失败并给我以下消息:
java.lang.AssertionError: Status
Expected :200
Actual :500
当我测试 url http://localhost:8080/movies 时,它可以正常工作。
【问题讨论】:
标签: rest spring-boot junit spring-data-jpa mockito