net*_*tta 16 java spring unit-testing 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));
}
}
Run Code Online (Sandbox Code Playgroud)
以及服务方法:
@Override
@Transactional
@EventJournal(type = EventType.CAMPAIGN_CREATE, owner = EventOwner.TERMINAL_USER)
public Long createCampaign(Campaign campaign) {
return companyDao.saveCompany(campaign);
}
Run Code Online (Sandbox Code Playgroud)
方面:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
测试执行时 - eventJournalService为null.我明白了NullPointerException
答案很简单:您想使用if()切入点表达式.
更新(问题也已更新):上面提供的最初链接应包含足够的信息,但对于它的价值,一个简短的解释和一个简单的例子:
一个if()切入点是一个static方面方法返回boolean.如果返回值为true,则表示任何组合切入点都myPointcut() && if()匹配,只要myPointcut()匹配即可.对于false整个组合切入点的返回值不匹配,有效地停用与切入点相关的任何建议.
那么你可以在静态if()切入点中做些什么呢?
TestMode.ACTIVE在单元测试或集成测试期间仅为true如果你想做更好的(并且更棘手)并且性能不那么重要,你也可以尝试动态确定自动连线方面成员变量是否等于null,并且只有在注入的对象实际存在时才激活你的切入点.这里唯一的问题是如何从静态方法确定成员变量.我不知道Spring AOP,但在简单的AspectJ中,有一个helper类,它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")
// ...
Run Code Online (Sandbox Code Playgroud)
小智 0
我只能猜测:第一件事是有一个单独的Spring applicationContext-test.xml,没有组件扫描;在 Maven 中,您可以添加一个阶段运行时,不包括用于测试的编织 jar。
| 归档时间: |
|
| 查看次数: |
5029 次 |
| 最近记录: |