Rest 测试时出错:找到测试类的@BootstrapWith的多个声明
我想第一次测试我的CRUD REST控制器。我看了一些视频,并提出了这个想法,但我得到了错误。我正在使用JPA和mySql。ITodoService是与CRUD方法的简单接口。当我通过Postman测试它时,我的rest控制器正在工作,所以代码还可以。 如果你能给我一些反馈,有什么可能是错误的,我在哪里可以检查关于测试REST应用程序的好信息,因为我已经花了大约3个小时没有任何成功:)Rest 测试时出错:找到测试类的@BootstrapWith的多个声明,rest,spring-boot,mockito,junit4,Rest,Spring Boot,Mockito,Junit4,我想第一次测试我的CRUD REST控制器。我看了一些视频,并提出了这个想法,但我得到了错误。我正在使用JPA和mySql。ITodoService是与CRUD方法的简单接口。当我通过Postman测试它时,我的rest控制器正在工作,所以代码还可以。 如果你能给我一些反馈,有什么可能是错误的,我在哪里可以检查关于测试REST应用程序的好信息,因为我已经花了大约3个小时没有任何成功:) @SpringBootTest @RunWith(SpringRunner.class) @WebMvcTes
@SpringBootTest
@RunWith(SpringRunner.class)
@WebMvcTest
非应用程序测试的公共类{
@自动连线
私有MockMvc-MockMvc;
@蚕豆
私人ITodosService ITodosService;
@试验
public void getAllTodosTest()引发异常{
Mockito.when(iTodosService.findAll())。然后返回(
Collections.emptyList()
);
MvcResult MvcResult=mockMvc.perform(
MockMvcRequestBuilders.get(“/todos”)
.accept(MediaType.APPLICATION_JSON)
).andReturn();
System.out.println(mvcResult.getResponse());
验证(iTodosService.findAll());
}
}
错误消息:
java.lang.IllegalStateException:配置错误:找到测试类[com.damian.todo_Final.TodoFinalApplicationTests]的多个@BootstrapWith声明:[@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.springbootstrapWith),@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper)]
编辑:
这是整个CRUD REST测试的代码
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(classes=TodofInAlaApplication.class,webEnvironment=SpringBootTest.webEnvironment.DEFINED\u端口)
//@WebMvcTest
非应用程序测试的公共类{
@自动连线
私有TestRestTemplate;
@本地服务器端口
专用int端口;
私有字符串getRootUrl(){
返回“http://localhost:“+港口;
}
@试验
public void contextLoads(){
}
@试验
public void getAllTodos(){
HttpHeaders=新的HttpHeaders();
HttpEntity=新的HttpEntity(空,标题);
ResponseEntity response=restemplate.exchange(getRootUrl()+“/employees”,
HttpMethod.GET、实体、字符串.class);
assertNotNull(response.getBody());
}
@试验
public void createNewTodo(){
Todos todo=新Todos();
todo.setId(5);
todo.setTaskDate(“15.01.1990”);
todo.setTaskStatus(true);
todo.SetTaskDescription(“测试说明”);
ResponseEntity postResponse=restTemplate.postForEntity(getRootUrl()+“/todos”,todo,todos.class);
assertNotNull(postResponse);
assertNotNull(postResponse.getBody());
}
@试验
public void testUpdateTodo(){
int-id=1;
Todos todo=restTemplate.getForObject(getRootUrl()+“/Todos/”+id,Todos.class);
todo.setTaskDate(“15.01.1990”);
todo.setTaskStatus(true);
todo.SetTaskDescription(“更新”);
restemplate.put(getRootUrl()+“/todos/”+id,todo);
Todos updatedTodo=restTemplate.getForObject(getRootUrl()+“/Todos/”+id,Todos.class);
assertNotNull(updatedTodo);
}
@试验
public void testDeletedTodo(){
int-id=3;
Todos todo=restTemplate.getForObject(getRootUrl()+“/Todos/”+id,Todos.class);
assertNotNull(todo);
删除(getRootUrl()+“/todos/”+id);
试一试{
todo=restTemplate.getForObject(getRootUrl()+“/todos/”+id,todos.class);
}捕获(最终HttpClientError异常){
assertEquals(如getStatusCode(),HttpStatus.NOT_FOUND);
}
}
您在一个测试类上同时拥有@SpringBootTest和@WebMVCTTest。除其他外,这两个类仅指定应在测试上下文中实例化的bean。
定义相互冲突,因此只允许一个定义
决定是否要测试:
- 整个应用程序上下文-使用@SpringBootTest
- 仅限控制器-使用@WebMvcTest
- 删除@SpringBootTest
- 指定要在@WebMvcTest中测试的控制器
- 删除@WebMvTest
- 添加自动配置WebMVC
@SpringBootTest将所有bean放入上下文中,因此@WebMvcTest可能会导致更快的测试。在这种情况下,删除
WebMvcTest
,并替换为autoconfigurewebvc
您是对的:),谢谢啊,这是一个例子:)。好的,谢谢。当我运行这个测试时,我在com.damian.todo_Final.todofinal.todofinal.todofinaliapplicationtests.getAllTodosTest(todofinaliapplicationtests.java:49)得到java.lang.NullPointerException。我的测试代码有一些错误,或者是我的RestController导致了这个问题?请检查哪个变量为空,没有实际的行号是不可能找到的。仅供参考:@RestClientTest
注释也会与@SpringBootTest
冲突,因为它包含@BootstrapWith
注释。升级Spring引导依赖项版本时,我必须删除@SpringBootTest
,并添加@ConfigurationContext
,以指定测试配置类来纠正此问题。
@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);
}
}