使用 MockMvc 进行 Spring Boot Aspectj 测试

Sha*_*Sha 4 java aspectj spring-boot spring-boot-test

我有一个带有 Aspectj 的 Spring 启动代码。这段代码是用基本的 MVC 架构编写的。然后我只是尝试用 MockMVC 测试它。但是当我尝试对其进行测试时,Aspectj 并没有中断。Aspectj 有没有特殊的配置?

控制器:

@GetMapping("user/{userId}/todo-list")
public ResponseEntity<?> getWaitingItems(@RequestUser CurrentUser currentUser){
    ...handle it with service method.
}
Run Code Online (Sandbox Code Playgroud)

方面:

@Pointcut("execution(* *(.., @RequestUser (*), ..))")
void annotatedMethod()
{
}

@Before("annotatedMethod() && @annotation(requestUser)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, RequestUser requestUser)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

测试:

@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());
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

There is no need for a @SpringBootTest if you wanna do integration tests of specific controller (web layer) + your custom Aspect logic (AOP layer).

Try something like this

@WebMvcTest(controllers = {AnyController.class})
@Import({AopAutoConfiguration.class, ExceptionAspect.class})
public class ErrorControllerAdviceTest {
Run Code Online (Sandbox Code Playgroud)
  • AnyController.class: controller under test
  • AopAutoConfiguration.class: Spring Boot auto-configuration of AOP
  • ExceptionAspect.class: class containing AOP logic
@Aspect
@Component
public class ExceptionAspect {}
Run Code Online (Sandbox Code Playgroud)

Tested with Spring Boot 2.2.1.RELEASE and JUNIT5. I am unsure, if my solution is technically the same like @Deadpool answers


Dea*_*ool 2

Spring @WebMvcTest只会实例化Web层,并且不会加载完整的应用程序上下文

然而,在这个测试中,Spring Boot仅实例化Web层而不是整个上下文。

为了测试 Aspectj,您需要使用 @SpringBootTest 注释加载整个应用程序上下文

@SpringBootTest 注释告诉 Spring Boot 查找主配置类(例如带有 @SpringBootApplication 的配置类)并使用它来启动 Spring 应用程序上下文

@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());
         }
    }
Run Code Online (Sandbox Code Playgroud)