我无法为我的测试添加注释以@WebMvcTest使其正常工作。我还配置了spring-cloud-config-server和spring-cloud-starter,所有带有这个注解的Test类好像都失败了。我有以下控制器测试:
代码:
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = UserController.class)
@ActiveProfiles({"dev", "native"})
public class UserControllerTest {
@MockBean
private UserService userService;
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
public void changePassword() throws Exception {
given(userService.changePassword(any(), any(), any())).willReturn(true);
MvcResult result = mockMvc.perform(post(/user/changePassword)
.param("old", "Pa$$W0rd")
.param("new, "NewPa$$W0rd"))
.andExpect(status().isOk()).andReturn();
assertThat(result).isNotNull();
assertThat(result.getResponse().getContentAsString()).isEqualTo("true");
}
}
Run Code Online (Sandbox Code Playgroud)
相关控制器:
@RestController
@RequestMapping(/user)
@Log4j2
public class UserController {
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@Timed
@PostMapping(value = /changePassword)
public ResponseEntity changePassword(@RequestParam("old") String oldPassword,
@RequestParam("new") String newPassword) …Run Code Online (Sandbox Code Playgroud)