我已经在我的工作项目中添加了AOP(面向方面的编程)方面。它确实可以工作,但是在尝试使用集成测试来测试其功能时不会调用它。
问题是,测试通过时不会调用方面。正常使用时,效果很好。
我试图创建一个应该为集成测试加载的自定义上下文,因为我认为这些测试可能不会在默认上下文中加载Aspect。
因为这不起作用,所以我也尝试手动代理该方面的bean,但这也不起作用。
这是我的综合测试课:
@ComponentScan(basePackages = { "package.aspects" })
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZirafyApp.class)
@ContextConfiguration(classes ={ IntegrationTestAOPConfiguration.class })
public class CellResourceIntTest {
private static CellTestHelper helper = new CellTestHelper();
@Autowired
private PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver;
@Autowired
private ExceptionTranslator exceptionTranslator;
@Autowired
private EntityManager em;
@Autowired
private BusinessFacade businessFacade;
@Autowired
private CellRepository cellRepository;
@Autowired
private AspectModule aspectModule;
private MockMvc restCellMockMvc;
private MappingJackson2HttpMessageConverter jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
private Cell cell;
private Cell parentCell;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
final CellResource cellResource = new CellResource(cellRepository, businessFacade);
this.restCellMockMvc = MockMvcBuilders.standaloneSetup(cellResource)
.setCustomArgumentResolvers(pageableHandlerMethodArgumentResolver)
.setControllerAdvice(exceptionTranslator)
.setConversionService(createFormattingConversionService())
.setMessageConverters(jacksonMessageConverter).build();
}
@Test
@Transactional
public void update_cellDtoWithEmptyName_returnsHttpError422AndCellInDbIsNotUpdated() throws Exception {
AspectJProxyFactory factory = new AspectJProxyFactory(cellRepository);
factory.addAspect(aspectModule);
CellRepository cellRepository = factory.getProxy();
CellDto cellDtoToUpdate = new CellDto.Builder().id(2).name(null).x(-10).active(true).parent(1).build();
Cell parentCell = helper.createCell(1L);
Cell cellToUpdate = helper.createCell(2L);
cellRepository.saveAndFlush(parentCell);
cellRepository.saveAndFlush(cellToUpdate);
restCellMockMvc.perform(put("/api/cells/update")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(cellDtoToUpdate)))
.andExpect(status().is(200));
Cell updatedCell = cellRepository.findOne(2L);
assertEquals(cellToUpdate.getX(), updatedCell.getX());
}
这里是集成测试的配置文件:
@Configuration
@EnableJpaRepositories(basePackages = {"package.repository"})
@ComponentScan("ch.post.pf.aspects")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class IntegrationTestAOPConfiguration {
@Autowired
private ExceptionTranslator exceptionTranslator;
@Autowired
private EntityManager em;
@Autowired
private CellConverter cellConverter;
@Autowired
private CellTreeService cellTreeService;
@Autowired
private CellService cellService;
@Autowired
private CellRepository cellRepository;
@Autowired
private BusinessFacade businessFacade;
@Autowired
private AspectModule aspectModule;
@Bean
public CellConverter returnCellConverter() {
return cellConverter;
}
@Bean
public AspectModule returnAspectModule() {
return null;//Aspects.aspectOf(AspectModule.class);
}
@Bean
public PageableHandlerMethodArgumentResolver returnPageableArgumentResolver() {
return new PageableHandlerMethodArgumentResolver();
}
@Bean
public ExceptionTranslator returnExceptionTranslator() {
return exceptionTranslator;
}
@Bean
@Primary
public EntityManager returnEntityManager() { return em; }
@Bean
public BusinessFacade returnBusinessFacade() {
return businessFacade;
}
@Bean
public CellTreeService returnCellTreeService() {
return cellTreeService;
}
@Bean
public CellService returnCellService() {
return cellService;
}
}
这是我的方面文件:
@Aspect
@Component
public class AspectModule {
private BusinessFacade businessFacade;
@Autowired
AspectModule(BusinessFacade businessFacade){
this.businessFacade = businessFacade;
}
@Pointcut("execution(* ch.post.pf.web.rest.CellResource.update(..))")
private void update() {}
@Around("update() && args(cell)")
public Object checkIsValidCell(ProceedingJoinPoint pjp, CellDto cell) {
System.out.println("Aspect was run");
final String message = canUpdate(cell);
if (message.equals("cell_valid")) {
try {
return pjp.proceed(); // Calls the usual update() function, if the cell is valid
} catch (Throwable e) {
System.out.println("Something went wrong with the aspects");
System.out.println(e.toString());
return null;
}
} else {
deleteIfCellWasEmpty(cell);
return ResponseUtil.unprocessableEntity(message);
}
}
}
该方面应该像现在这样继续工作,但它也应该在集成测试中工作,此刻在这些方面根本没有被调用。