我无法在Spring Boot 1.4中运行简单测试。我按照官方网站上的教程testing-the-spring-mvc-slice操作,但未能使其正常工作。
每次都会出现以下错误:
从我的角度看,这篇文章和该网站上的帖子完全相同。
@WebMvcTest将会:
- 自动配置Spring MVC、Jackson、Gson、消息转换器等。 - 加载相关组件(@Controller、@RestController、@JsonComponent等)。 - 配置MockMVC。
那我为什么需要配置一个“超级”类呢?
每次都会出现以下错误:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
有任何想法、提示吗?
提前感谢。
编辑:
这是控制器。
@Controller
public class UserManagementController {
@GetMapping(value = "/gs/users/getUsers")
public @ResponseBody String getAllUsers() {
return "test";
}
}
这是一个测试
@RunWith(SpringRunner.class)
@WebMvcTest(UserManagementController.class)
public class UserManagementControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void showUserView() throws Exception {
this.mvc.perform(get("/gs/users/getUsers"))
.andExpect(status().isOk())
.andDo(print());
}
}
从我的角度看,这篇文章和该网站上的帖子完全相同。
@WebMvcTest将会:
- 自动配置Spring MVC、Jackson、Gson、消息转换器等。 - 加载相关组件(@Controller、@RestController、@JsonComponent等)。 - 配置MockMVC。
那我为什么需要配置一个“超级”类呢?