我想第一次测试我的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);
}
}
答案 0 :(得分:0)
在一个测试类上同时具有@SpringBootTest和@WebMvcTest。这两个类都仅指定应在测试上下文中实例化哪些bean。 定义冲突,因此只允许一个。
确定是否要测试:
就您而言,我会:
或者,您可以
@SpringBootTest将所有bean置于上下文中,因此@WebMvcTest可能会导致更快的测试。