测试时出错:发现测试类有多个 @BootstrapWith 声明

kak*_*x22 4 rest junit4 mockito spring-boot

我想第一次测试我的 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Les*_*iak 7

您在一个测试类上同时拥有 @SpringBootTest 和 @WebMvcTest。除其他类外,这两个类仅指定应在测试上下文中实例化哪些 bean。这些定义是相互冲突的,因此只允许使用一个。

决定是否要测试:

  • 整个应用程序上下文 - 使用@SpringBootTest
  • 仅控制器 - 使用@WebMvcTest

对于你的情况,我会:

  • 删除@SpringBootTest
  • 在@WebMvcTest中指定要测试的Controller

或者,您可以

  • 删除@WebMvTest
  • 添加 AutoConfigureWebMvc

@SpringBootTest 将所有 bean 引入上下文中,因此 @WebMvcTest 可能会导致更快的测试。