Java – disables certain aspects during unit test runs
I have integration tests (load context) and unit tests running together My code uses spring compile time weaving
My problem is that my announced recommendations also run during my unit tests This kills the concept of unit tests, which is why I disable them
There are no declarations that can be placed on pointcuts, some methods I can call, some spring configurations, or Maven commands to disable these suggestions, like all * unittest java?
Thank you for your help
Example:
I have the following unit tests:
@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)); } }
And service mode:
@Override @Transactional @EventJournal(type = EventType.CAMPAIGN_CREATE,owner = EventOwner.TERMINAL_USER) public Long createCampaign(Campaign campaign) { return companyDao.saveCompany(campaign); }
aspect:
@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,null); } private void saveEvent(EventJournal eventJournal,EventStatus status,Long persistentId) { EventType type = eventJournal.type(); EventOwner owner = eventJournal.owner(); eventJournalService.saveEvent(type,owner,persistentId); } }
When the test is executed – eventjournalservice is empty So I see NullPointerException
Solution
The answer is simple: you want to use an if () pointcut expression
Update (after the question has also been updated): the initial link provided above should contain enough information, but for what is worth it, a short explanation and a simple example:
The if () pointcut is a static method that returns a Boolean value If the return value is true, it means the pointcut of any combination, such as mypointcut() & & as long as mypointcut() matches, if() matches If the return value is false and the whole composite pointcut does not match, any suggestions related to pointcuts are effectively disabled
So what can you do at the static if () pointcut?
>Evaluate something like testmode Static Boolean member of the active tool class, which is unique in unit or Integration Testing > evaluate environment variable, which is set only during Testing > evaluate java system properties set only during Testing > and more
If you want to be a good person (and tricky), performance is not important. You can also try to dynamically determine whether the member variable of the automatically connected aspect is equal to null, and activate the pointcut only when the injected object actually exists The only problem here is how to determine member variables from static methods I don't know about spring AOP, but in simple AspectJ, there are some helper classes, aspects, with several overloaded methods called aspectof (..) Assuming that your aspect is instantiated as single, you can do this:
@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") // ...