【问题标题】:Error while testing: found multiple declaration of @BootstrapWith for test class测试时出错:为测试类找到了多个@BootstrapWith 声明
【发布时间】: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);
        }
    }

【问题讨论】:

标签: rest spring-boot mockito junit4


【解决方案1】:

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

决定是否要测试:

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

在你的情况下,我会:

  • 删除@SpringBootTest
  • 在@WebMvcTest 中指定要测试的控制器

你也可以

  • 删除@WebMvTest
  • 添加 AutoConfigureWebMvc

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

【讨论】:

  • 啊,是这样的:)。好的谢谢。当我运行此测试时,我在 com.damian.todo_Final.TodoFinalApplicationTests.getAllTodosTest(TodoFinalApplicationTests.java:49) 处收到 java.lang.NullPointerException。我的测试代码有一些错误,或者我的 RestController 导致了这个问题?干杯
  • 请检查哪个变量为空,没有实际行号是不可能找到的。
  • 仅供参考:@RestClientTest 注释也将与 @SpringBootTest 冲突,因为它包含 @BootstrapWith 注释。在升级Spring Boot依赖版本的时候,我不得不去掉@SpringBootTest并添加@ConfigurationContext指定测试配置类来修正问题。
猜你喜欢
  • 2020-04-13
  • 1970-01-01
  • 2017-04-24
  • 2020-08-06
  • 2019-03-24
  • 2023-01-27
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
相关资源
最近更新 更多