小编kak*_*x22的帖子

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

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

rest junit4 mockito spring-boot

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

junit4 ×1

mockito ×1

rest ×1

spring-boot ×1