-3

I use Spring boot and in my aspect class I have two fields which have @Autowired annotation. When I run code then it works fine, but when I run test for this class then these two autowired fields are null.

Can someone tell what I need to do to fix that? I'm sorry for lack of code but today I don't have access to this.


example (from another SO question):

Unit test:

@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));
    }
}

Service method:

@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, 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);
    }

}

When test executes - eventJournalService is null. Thus I see NullPointerException

Denis Abakumov
  • 355
  • 3
  • 11
193michalll
  • 21
  • 2
  • 5
  • Please share your code? – Kishor Velayutham Feb 18 '18 at 14:55
  • I can't share whole code but tommorow I could sent code from this class. In Generally I added two autowired field to this aspect class and it's work but not when i run test for this class. This fields are null then. I don't even know how i can fix that in theory. – 193michalll Feb 18 '18 at 15:42
  • 2
    You have already received 4 downvotes and 3 close votes, only 2 shy of the question being closed altogether. So please edit your question to be an [MCVE](http://stackoverflow.com/help/mcve). Extract a small, executeable sample from your code base, reproducing the problem, including configuration. This is how StackOverflow works. It is about concrete programming problems and their solutions. People around here tend to be programmers like you, not mediums. With the kind of info you provide only a medium could find out what might be your problem. Sorry, man. – kriegaex Feb 19 '18 at 00:46

1 Answers1

1

well, having in mind the fact that your question is not clear at all, here are some options that you could potentially miss in your test code:

  1. You are not mocking them or mocking them but not injecting
  2. You are not using injections in your test code at all

P.S. please read and understand more about dependency injection (@Autowired) of Spring Boot applications

BigGinDaHouse
  • 1,282
  • 9
  • 19