【发布时间】:2021-03-01 11:33:40
【问题描述】:
我有一个GreetingController
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public @ResponseBody String greeting() {
return "Hello, same to you";
}
}
和GreetingControllerTest
@WebMvcTest(GreetingController.class)
public class WebMockTest {
@Autowired
private MockMvc mockMvc;
@Test
public void greetingShouldReturnMessageFromService() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello, same to you")));
}
}
我在 intelliJ 中运行测试,希望它不会加载应用程序上下文,但它会从启动应用程序开始。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
{"thread":"main","level":"INFO","loggerName":..........
根据spring doc we can narrow the tests to only the web layer by using @WebMvcTest. 这是否意味着它仍然加载应用程序上下文?或者我没有正确理解它。
【问题讨论】:
-
它加载了一个最小的上下文,所以是的,它会加载一些东西。