我有集成测试(加载上下文)和单元测试一起运行。我的代码使用spring进行aspectj编译时编织。
我的问题是我声明的建议也在我的一些单元测试期间运行。这扼杀了单元测试的概念,这就是我想禁用它们的原因。
我可以在切入点声明、我可以调用的一些方法、一些 spring 配置或 maven 命令中添加一些东西来禁用这些建议,例如所有 *UnitTest.java 吗?
感谢您的帮助。
示例:
我有以下单元测试:
@RunWith(MockitoJUnitRunner.class)
public class CompanyServiceImplTest {
@Test
public void createCampaignTest() throws Exception {
when(companyDaoMock.saveCompany(any(Campaign.class))).thenReturn(77L);
Long campaignId = companyService.createCampaign(campaignMock);
assertEquals(Long.valueOf(77L), Long.valueOf(campaignId));
}
}
及以下服务方式:
@Override
@Transactional
@EventJournal(type = EventType.CAMPAIGN_CREATE, owner = EventOwner.TERMINAL_USER)
public Long createCampaign(Campaign campaign) {
return companyDao.saveCompany(campaign);
}
方面:
@Aspect
public class EventJournalAspect {
@Autowired
private EventJournalService eventJournalService;
@Pointcut(value="execution(public * *(..))")
public void anyPublicMethod() {}
@Pointcut("within(com.terminal.service..*)")
private void inService() {}
@AfterReturning(pointcut = "anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", returning = "id")
public void process(Object id, EventJournal eventJournal, AbstractDomainEntity entity)
throws Throwable {
if (eventJournal.type() != EventType.CAMPAIGN_PAYMENT || id != null) {
saveEvent(eventJournal, EventStatus.SUCCESS, entity, (Long) id);
}
}
@AfterThrowing(pointcut = "anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", throwing="ex")
public void processException(EventJournal eventJournal, AbstractDomainEntity entity, Exception ex) throws Throwable {
saveEvent(eventJournal, EventStatus.FAILURE, entity, null);
}
private void saveEvent(EventJournal eventJournal, EventStatus status, AbstractDomainEntity entity, Long persistentId) {
EventType type = eventJournal.type();
EventOwner owner = eventJournal.owner();
eventJournalService.saveEvent(type, owner, EventStatus.SUCCESS, entity, persistentId);
}
}
当测试执行时 - eventJournalService
为空。因此我看到 NullPointerException
最佳答案
答案很简单:您想使用 if()
pointcut expression .
更新(在问题也被更新之后):上面最初提供的链接应该包含足够的信息,但是对于它的值(value),一个简短的解释和一个简单的例子:
if()
切入点是返回 boolean
的 static
方面方法。如果返回值为 true
,则表示只要 myPointcut()
匹配,任何像 myPointcut() && if()
这样的组合切入点都匹配.对于 false
的返回值,整个组合切入点不匹配,从而有效地停用了与切入点相关的任何建议。
那么你可以在静态 if()
切入点中做什么?
TestMode.ACTIVE
,这仅在单元测试或集成测试期间为真如果您想做一些更花哨(和更复杂)的事情并且性能不是那么重要,您还可以尝试动态确定自动连接的方面成员变量是否等于 null 并且仅在注入(inject)的对象为时才激活您的切入点实际存在。这里唯一的问题是如何从静态方法中确定成员变量。我不知道 Spring AOP,但是在普通的 AspectJ 中有一个辅助类 Aspects
和几个名为 aspectOf(..)
的重载方法。假设您的方面被实例化为单例,您可以执行以下操作:
@Pointcut("if()")
public static boolean isActive() {
return Aspects.aspectOf(PerformanceMonitorAspect.class).eventJournalService != null;
}
// ...
@AfterReturning(pointcut = "isActive() && anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", returning = "id")
// ...
@AfterThrowing(pointcut = "isActive() && anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", throwing="ex")
// ...
https://stackoverflow.com/questions/7433658/
相关文章:
java - 在没有 Spring 身份验证的情况下通过 web-socket 多次回复
java - Spring 启动 : How to setup test data with liq
java - Spring @Transaction 未启动事务
java - 如何在 Hibernate 中使用临时表批量删除记录?
json - 如何在 Spring Boot Actuator 的 Trace 中包含 JSON 响
java - 为什么 PropertyDescriptor 行为从 Java 1.6 更改为 1.7
java - 在哪里以及如何处理 spring+hibernate 异常?
java - 我可以在 View 层中找到 spring mvc Controller 的 URL