相关疑难解决方法(0)

@WebMvcTest 因 java.lang.IllegalStateException 失败:无法加载 ApplicationContext

我无法为我的测试添加注释以@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)

java junit spring spring-mvc spring-boot

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

标签 统计

java ×1

junit ×1

spring ×1

spring-boot ×1

spring-mvc ×1