【发布时间】:2019-10-16 04:41:59
【问题描述】:
我想第一次测试我的 CRUD REST 控制器。我看过一些视频并提出了这个想法,但我遇到了错误。我正在将 JPA 与 mySql 一起使用。 ITodoService 是带有 CRUD 方法的简单接口。当我通过 Postman 测试时,我的休息控制器正在工作,所以那里的代码没问题。 如果您能给我一些反馈,可能出了什么问题,我在哪里可以检查有关测试 REST 应用程序的良好信息,因为我花了大约 3 小时没有任何成功:)
@SpringBootTest
@RunWith(SpringRunner.class)
@WebMvcTest
public class TodoFinalApplicationTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private ITodosService iTodosService;
@Test
public void getAllTodosTest() throws Exception {
Mockito.when(iTodosService.findAll()).thenReturn(
Collections.emptyList()
);
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/todos")
.accept(MediaType.APPLICATION_JSON)
).andReturn();
System.out.println(mvcResult.getResponse());
Mockito.verify(iTodosService.findAll());
}
}
Error message:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.damian.todo_Final.TodoFinalApplicationTests]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper)]
EDIT:
This is code for whole CRUD REST Test
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(classes = TodoFinalApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
// @WebMvcTest
public class TodoFinalApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort
private int port;
private String getRootUrl() {
return "http://localhost:" + port;
}
@Test
public void contextLoads() {
}
@Test
public void getAllTodos() {
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(getRootUrl() + "/employees",
HttpMethod.GET, entity, String.class);
assertNotNull(response.getBody());
}
@Test
public void createNewTodo() {
Todos todo = new Todos();
todo.setId(5);
todo.setTaskDate("15.01.1990");
todo.setTaskStatus(true);
todo.setTaskDescritpion("Description for testing");
ResponseEntity<Todos> postResponse = restTemplate.postForEntity(getRootUrl() + "/todos", todo, Todos.class);
assertNotNull(postResponse);
assertNotNull(postResponse.getBody());
}
@Test
public void testUpdateTodo() {
int id = 1;
Todos todo = restTemplate.getForObject(getRootUrl() + "/todos/" + id, Todos.class);
todo.setTaskDate("15.01.1990");
todo.setTaskStatus(true);
todo.setTaskDescritpion("Updating");
restTemplate.put(getRootUrl() + "/todos/" + id, todo);
Todos updatedTodo = restTemplate.getForObject(getRootUrl() + "/todos/" + id, Todos.class);
assertNotNull(updatedTodo);
}
@Test
public void testDeletedTodo() {
int id = 3;
Todos todo = restTemplate.getForObject(getRootUrl() + "/todos/" + id, Todos.class);
assertNotNull(todo);
restTemplate.delete(getRootUrl() + "/todos/" + id);
try {
todo = restTemplate.getForObject(getRootUrl() + "/todos/" + id, Todos.class);
} catch (final HttpClientErrorException e) {
assertEquals(e.getStatusCode(), HttpStatus.NOT_FOUND);
}
}
【问题讨论】:
-
在这种情况下,删除
WebMvcTest,并替换为AutoconfigureWebMvc -
你是对的 :),谢谢
标签: rest spring-boot mockito junit4