Spring Boot Aspectj test with MockMvc

Sha

I have a Spring boot code with Aspectj. This code has written with basic MVC architecture. Then I just try to test it with MockMVC. But when I try to test it, Aspectj doesn't interrupted. Is there a special configuration about Aspectj?

Controller:

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

Aspect:

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

@Before("annotatedMethod() && @annotation(requestUser)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, RequestUser requestUser)
{
    ...
}

Test:

@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());
    }
}
Deadpool

Spring @WebMvcTest will only instantiate web layer and it will not load complete application context

However, in this test, Spring Boot instantiates only the web layer rather than the whole context.

In order to test Aspectj you need to load whole application context using @SpringBootTest annotation

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

So annotate the test using @SpringBootTest annotation

@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());
         }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Boot Aspectj test with MockMvc

From Java

Spring Boot MVC Test - MockMvc is always null

From Dev

Spring Boot MockMVC Test does not load Yaml file

From Dev

Spring Boot integration test responding with empty body - MockMvc

From Dev

Spring Boot - MockMVC Error creating bean

From Dev

Spring boot + Spring security with AspectJ not working

From Dev

Spring Test MockMvc perform request on external URL

From Dev

Using Spring mockMvc to test optional path variables

From Dev

AspectJ not working after Transaction configuration in Spring Boot

From Dev

Spring Security Unit Test - MockMvc perform test with custom user

From Dev

How to mock autowired dependencies in Spring Boot MockMvc Unit tests?

From Dev

how to use jmockit with spring's mockmvc to test controller

From Dev

Spring MVC Image upload Integration test using Mockmvc and JSON failure

From Dev

Spring mockMvc doesn't consider validation in my test

From Dev

Speed Up Spring MockMvc Integration Test with Embedded Cassandra

From Dev

How to use the right Spring Security configuration during a MockMvc test

From Dev

Spring MockMvc - How to test delete request of REST controller?

From Dev

spring-mvc: how to test Rx responses with mockMvc?

From Dev

Spring MockMvc: How to access Request Object while initializing test

From Dev

How to configure spring boot application to use aspectj transactions?

From Dev

Spring Boot - Can't get load time weaving with aspectj to work

From Java

MockMvc test not reach the Controller

From Dev

mockMvc - Test Error Message

From Dev

Spring boot test - wait for termination

From Dev

Cucumber Test a Spring Boot Application

From Dev

Integration Test with Spring Boot and Spock

From Dev

Spring Boot override interface in test

From Dev

Elasticsearch Spring boot integration test

From Dev

Spring OAuth and Boot Integration Test

Related Related

  1. 1

    Spring Boot Aspectj test with MockMvc

  2. 2

    Spring Boot MVC Test - MockMvc is always null

  3. 3

    Spring Boot MockMVC Test does not load Yaml file

  4. 4

    Spring Boot integration test responding with empty body - MockMvc

  5. 5

    Spring Boot - MockMVC Error creating bean

  6. 6

    Spring boot + Spring security with AspectJ not working

  7. 7

    Spring Test MockMvc perform request on external URL

  8. 8

    Using Spring mockMvc to test optional path variables

  9. 9

    AspectJ not working after Transaction configuration in Spring Boot

  10. 10

    Spring Security Unit Test - MockMvc perform test with custom user

  11. 11

    How to mock autowired dependencies in Spring Boot MockMvc Unit tests?

  12. 12

    how to use jmockit with spring's mockmvc to test controller

  13. 13

    Spring MVC Image upload Integration test using Mockmvc and JSON failure

  14. 14

    Spring mockMvc doesn't consider validation in my test

  15. 15

    Speed Up Spring MockMvc Integration Test with Embedded Cassandra

  16. 16

    How to use the right Spring Security configuration during a MockMvc test

  17. 17

    Spring MockMvc - How to test delete request of REST controller?

  18. 18

    spring-mvc: how to test Rx responses with mockMvc?

  19. 19

    Spring MockMvc: How to access Request Object while initializing test

  20. 20

    How to configure spring boot application to use aspectj transactions?

  21. 21

    Spring Boot - Can't get load time weaving with aspectj to work

  22. 22

    MockMvc test not reach the Controller

  23. 23

    mockMvc - Test Error Message

  24. 24

    Spring boot test - wait for termination

  25. 25

    Cucumber Test a Spring Boot Application

  26. 26

    Integration Test with Spring Boot and Spock

  27. 27

    Spring Boot override interface in test

  28. 28

    Elasticsearch Spring boot integration test

  29. 29

    Spring OAuth and Boot Integration Test

HotTag

Archive