简体   繁体   中英

Dependency Injection to Aspect method in Spring Boot Test (JUnit)

My Aspect method work when i do not use my CronLogService but if I Inject this I have this error:

No qualifying bean of type 'com.app.service.CronLogService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

My Aspect method:

@Aspect
@Component
@RequiredArgsConstructor
@EnableAspectJAutoProxy
@Slf4j
public class CronLoggerAspect {

    private final CronLogService cronLogService;
    
    @Around("@annotation(CronLogger)")
    public Object trace(ProceedingJoinPoint joinPoint) throws Throwable {
        String name = MethodSignature.class.cast(joinPoint.getSignature()).getMethod().getAnnotation(CronLogger.class)
            .name();
        log.debug("start CronLogger for {}", name);
        ....
    }
}

My service:

@Service
@Transactional
@RequiredArgsConstructor
@Slf4j
public class CronLogServiceImpl implements CronLogService {

    private final CronLogRepository cronLogRepository;

    @Override
    public CronLog create(CronLog cronLog) {
        return cronLogRepository.save(cronLog);
    }
    ...
}

I have this error only on my JUnit test (my Springboot app work:!):

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {TaskService.class, CronLoggerAspect.class, CronLogService.class, CronLogRepository.class})
public class TaskTest {

    @Autowired
    private TaskService taskService;
    
    @Test
    void testCronLoggerSuccess() throws CronException {
        taskService.testCronLogger("test Success");
    }
}

My test service:

@Service
@RequiredArgsConstructor
@Slf4j
public class TaskService {

    @CronLogger(name = "unit test")
    public void testCronLogger(String param) throws CronException {
        log.info("testCronLogger for {}", param);
        ...
    }

}

My CronLogRepository

public interface CronLogRepository extends JpaRepository<CronLog, String> {

}

EDIT:

//@RequiredArgsConstructor
@Slf4j
public class CronLogServiceImpl implements CronLogService {

    @Autowired
    private CronLogRepository cronLogRepository;

test: No qualifying bean of type 'com.app.repository.CronLogRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

springboot: OK

@RequiredArgsConstructor
@Slf4j
public class CronLogServiceImpl implements CronLogService {

    private CronLogRepository cronLogRepository;

test: no spring error but cronLogRepository is null. so cronLogRepository.save return a java.lang.NullPointerException

springboot: java.lang.NullPointerException: null on CronLogServiceImpl.create

@SpringBootTest(classes = {TaskService.class, CronLoggerAspect.class, CronLogService.class, CronLogRepository.class})

Probably the error is in this line, try replacing it with just @SpringBootTest and if it works, then the error is in context configuration. You need to add, I think, CronLogServiceImpl to the list of classes to add to context.

Does this solve your problem? Let me know in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM