title: Spring Aspect not triggered in unit test
body: <p>OK, we're talking Spring (3.2.0) MVC</p>
  
  <p>We have an pointcut defined to be triggered "around" an annotation like so:</p>
  
  <pre><code>@Around("@annotation(MyAnnotation)")
  public void someFunction() {
  
  }
  </code></pre>
  
  <p>Then in a controller we have:</p>
  
  <pre><code>@Controller
  @Component
  @RequestMapping("/somepath")
  public class MyController {
  
      @Autowired
      private MyService service;
  
      ...
  
      @MyAnnotation
      @RequestMapping(value = "/myendpoint", method = RequestMethod.POST, produces = "application/json")
      @ResponseBody
      public Object myEndpoint(@RequestBody MyRequestObject requestObject, HttpServletRequest request, HttpServletResponse response) {
          ...
          return service.doSomething(requestObject);
      }         
  }
  </code></pre>
  
  <p>Then we have a unit test that looks like this:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  @ContextConfiguration(locations = {"../path/to/applicationContext.xml"})
  @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
  public class MyControllerTest {
  
      private MockMvc mockMvc;
  
      @InjectMocks
      private MyController controller;
  
      @Mock
      private MyService myService;    
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
          this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
      }
  
  
      @Test
      public void myTest() {
          MyRequest request = new MyRequest();
          MyResponse response = new MyResponse();
          String expectedValue = "foobar";
  
          Mockito.when(myService.doSomething((MyRequest) Mockito.any())).thenReturn(response);
  
          MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/myendpoint");
  
          String request = IOUtils.toString(context.getResource("classpath:/request.json").getURI());
  
          builder.content(request);
          builder.contentType(MediaType.APPLICATION_JSON);
  
          mockMvc.perform(builder)
                  .andExpect(MockMvcResultMatchers.status().isOk())
                  .andExpect(MockMvcResultMatchers.jsonPath("$.someKey").value(expectedValue));
  
          Mockito.verify(myService, Mockito.times(1)).doSomething((MyRequest) Mockito.any());
      }
  }
  </code></pre>
  
  <p>The test runs fine, but the aspect defined around the annotation (MyAnnotation) does not execute.  This executes just fine when the endpoint is triggered by a <em>real</em> request (e.g. when running in a servlet container) but just doesn't fire when running in the test.</p>
  
  <p>Is this a particular "feature" of MockMvc that it doesn't trigger aspects?</p>
  
  <p>FYI our applicationContext.xml is configured with:</p>
  
  <pre><code>&lt;aop:aspectj-autoproxy/&gt;
  </code></pre>
  
  <p>and as I mentioned the aspects do actually work in reality, just not in the test.</p>
  
  <p>Anyone know how to get these aspects to fire?</p>
  
  <p>Thanks!</p>