Spring 使用 MockMvc 启动 Aspectj 测试
Spring Boot Aspectj test with MockMvc
我有一个 Spring 使用 Aspectj 的启动代码。这段代码是用基本的 MVC 架构编写的。然后我只是尝试用 MockMVC 测试它。但是当我尝试测试它时,Aspectj 不会中断。 Aspectj有没有特殊配置?
控制器:
@GetMapping("user/{userId}/todo-list")
public ResponseEntity<?> getWaitingItems(@RequestUser CurrentUser currentUser){
...handle it with service method.
}
看点:
@Pointcut("execution(* *(.., @RequestUser (*), ..))")
void annotatedMethod()
{
}
@Before("annotatedMethod() && @annotation(requestUser)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, RequestUser requestUser)
{
...
}
测试:
@WebMvcTest(value = {Controller.class, Aspect.class})
@ActiveProfiles("test")
@ContextConfiguration(classes = {Controller.class, Aspect.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class ControllerTest
{
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private Controller controller;
@MockBean
private Service service;
@Before
public void setUp()
{
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void getWaitingItems() throws Exception
{
mockMvc.perform(get("/user/{userId}/todo-list", 1L))
.andExpect(status().isOk());
}
}
Spring @WebMvcTest只会实例化web层,不会加载完整的应用上下文
However, in this test, Spring Boot instantiates only the web layer rather than the whole context.
为了测试 Aspectj,您需要使用 @SpringBootTest 注释加载整个应用程序上下文
The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context
因此使用 @SpringBootTest
注释
注释测试
@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private Controller controller;
@Before
public void setUp() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void getWaitingItems() throws Exception {
mockMvc.perform(get("/user/{userId}/todo-list", 1L))
.andExpect(status().isOk());
}
}
如果您想对特定控制器(Web 层)+ 自定义方面逻辑(AOP 层)进行集成测试,则不需要 @SpringBootTest
。
尝试这样的事情
@WebMvcTest(controllers = {AnyController.class})
@Import({AopAutoConfiguration.class, ExceptionAspect.class})
public class ErrorControllerAdviceTest {
- AnyController.class: 待测控制器
- AopAutoConfiguration.class: Spring AOP 的引导自动配置
- ExceptionAspect.class: class 包含 AOP 逻辑
@Aspect
@Component
public class ExceptionAspect {}
使用 Spring Boot 2.2.1.RELEASE 和 JUNIT5 进行测试。
我不确定,如果我的解决方案在技术上与@Deadpool answers
相同
我有一个 Spring 使用 Aspectj 的启动代码。这段代码是用基本的 MVC 架构编写的。然后我只是尝试用 MockMVC 测试它。但是当我尝试测试它时,Aspectj 不会中断。 Aspectj有没有特殊配置?
控制器:
@GetMapping("user/{userId}/todo-list")
public ResponseEntity<?> getWaitingItems(@RequestUser CurrentUser currentUser){
...handle it with service method.
}
看点:
@Pointcut("execution(* *(.., @RequestUser (*), ..))")
void annotatedMethod()
{
}
@Before("annotatedMethod() && @annotation(requestUser)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, RequestUser requestUser)
{
...
}
测试:
@WebMvcTest(value = {Controller.class, Aspect.class})
@ActiveProfiles("test")
@ContextConfiguration(classes = {Controller.class, Aspect.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class ControllerTest
{
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private Controller controller;
@MockBean
private Service service;
@Before
public void setUp()
{
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void getWaitingItems() throws Exception
{
mockMvc.perform(get("/user/{userId}/todo-list", 1L))
.andExpect(status().isOk());
}
}
Spring @WebMvcTest只会实例化web层,不会加载完整的应用上下文
However, in this test, Spring Boot instantiates only the web layer rather than the whole context.
为了测试 Aspectj,您需要使用 @SpringBootTest 注释加载整个应用程序上下文
The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context
因此使用 @SpringBootTest
注释
@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private Controller controller;
@Before
public void setUp() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void getWaitingItems() throws Exception {
mockMvc.perform(get("/user/{userId}/todo-list", 1L))
.andExpect(status().isOk());
}
}
如果您想对特定控制器(Web 层)+ 自定义方面逻辑(AOP 层)进行集成测试,则不需要 @SpringBootTest
。
尝试这样的事情
@WebMvcTest(controllers = {AnyController.class})
@Import({AopAutoConfiguration.class, ExceptionAspect.class})
public class ErrorControllerAdviceTest {
- AnyController.class: 待测控制器
- AopAutoConfiguration.class: Spring AOP 的引导自动配置
- ExceptionAspect.class: class 包含 AOP 逻辑
@Aspect
@Component
public class ExceptionAspect {}
使用 Spring Boot 2.2.1.RELEASE 和 JUNIT5 进行测试。 我不确定,如果我的解决方案在技术上与@Deadpool answers
相同