测试 returns 响应实体的控制器
Testing the controller which returns response entity
我关注接受 post 请求和 returns 响应实体的控制器
@RestController
public class UserController {
@Autowired
private UserService UserService;
@RequestMapping(value = "/all", method = POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ResponseUser>> navigationTree(@RequestBody(required=false) UserDataRequest request) {
return UserService.sendEntries(request);
}
}
这是我为它写的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {
private MockMvc mockMvc;
@MockBean
private UserService UserServiceMock;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
@Test
public void returnTopLevel() throws Exception {
String expectedJson = "[{\"id\":\"11\",\"name\":\"11\"},{\"id\":\"22\",\"name\":\"22\"}]";
MvcResult result = this.mockMvc.perform(post("/all")
.contentType(MediaType.APPLICATION_JSON)
.content("")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
String actualJson = result.getResponse().getContentAsString();
// System.out.println(result);
//assertThat(Objects.equals(expectedJson, actualJson)).isTrue();
verify(UserServiceMock,times(1)).sendEntries(null);
}
}
我想将响应正文中收到的字符串与预期字符串进行比较。
- 我尝试了字符串比较,但它不起作用
错误:
assertThat(Objects.equals(expectedJson, actualJson)).isTrue();
而 actualjson 是空的。
还有哪些方式?
在执行 this.mockMvc.perform
之前,您需要将 UserService
模拟为 return。像这样:
when(UserServiceMock.sendEntries(null)).thenReturn(expectedResponceEntity);
所以只需构建预期的响应,然后将其模拟 UserService
到 return。
我关注接受 post 请求和 returns 响应实体的控制器
@RestController
public class UserController {
@Autowired
private UserService UserService;
@RequestMapping(value = "/all", method = POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ResponseUser>> navigationTree(@RequestBody(required=false) UserDataRequest request) {
return UserService.sendEntries(request);
}
}
这是我为它写的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {
private MockMvc mockMvc;
@MockBean
private UserService UserServiceMock;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
@Test
public void returnTopLevel() throws Exception {
String expectedJson = "[{\"id\":\"11\",\"name\":\"11\"},{\"id\":\"22\",\"name\":\"22\"}]";
MvcResult result = this.mockMvc.perform(post("/all")
.contentType(MediaType.APPLICATION_JSON)
.content("")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
String actualJson = result.getResponse().getContentAsString();
// System.out.println(result);
//assertThat(Objects.equals(expectedJson, actualJson)).isTrue();
verify(UserServiceMock,times(1)).sendEntries(null);
}
}
我想将响应正文中收到的字符串与预期字符串进行比较。
- 我尝试了字符串比较,但它不起作用
错误:
assertThat(Objects.equals(expectedJson, actualJson)).isTrue();
而 actualjson 是空的。
还有哪些方式?
在执行 this.mockMvc.perform
之前,您需要将 UserService
模拟为 return。像这样:
when(UserServiceMock.sendEntries(null)).thenReturn(expectedResponceEntity);
所以只需构建预期的响应,然后将其模拟 UserService
到 return。