Federaspekt wird im Komponententest nicht ausgelöst
OK, wir reden über Spring (3.2.0) MVC
Wir haben einen Pointcut definiert, der "um" eine Annotation wie folgt ausgelöst wird:
@Around("@annotation(MyAnnotation)")
public void someFunction() {
}
Dann haben wir in einem Controller:
@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);
}
}
Dann haben wir einen Unit-Test, der so aussieht:
@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());
}
}
Der Test läuft gut, aber der um die Annotation (MyAnnotation) definierte Aspekt wird nicht ausgeführt. Dies funktioniert einwandfrei, wenn der Endpunkt durch a ausgelöst wirdecht request (z. B. beim Ausführen in einem Servlet-Container) wird beim Ausführen im Test nur nicht ausgelöst.
Ist dies ein besonderes "Feature" von MockMvc, das keine Aspekte auslöst?
Zu Ihrer Information, unsere applicationContext.xml ist konfiguriert mit:
<aop:aspectj-autoproxy/>
und wie gesagt, die aspekte funktionieren tatsächlich in der realität, nur nicht im test.
Weiß jemand, wie man diese Aspekte in Brand setzt?
Vielen Dank!