Hey everyone,

I have a scenario where I have @Cacheable annotation in the Service Layer. One method of this class is calling other one with the latter having both having @Cacheable annotation on it. Since I am using Spring AOP therefore whenever createSphereClientA calls createSphereClientB , the @Cacheable is ignored because there is no proxy . I read here https://stackoverflow.com/questions/16899604/spring-cache-cacheable-not-working-while-calling-from-another-method-of-the-s/48867068#48867068 and used @Resource as I'm using Springboot 2.2.5.

I don't want to use AspectJ because it's happening only for one class.

@Service
public class CacheableSphereClientFactoryImpl implements SphereClientFactory {

    /**
     * 1. Self-autowired reference to proxified bean of this class.
     */
    @Resource
    private SphereClientFactory self;

    @Autowired
    public CacheableSphereClientFactoryImpl(CacheablePolicyDao cacheablePolicyDao, UserCustomPolicyDao userCustomPolicyDao,
                                     UserAPI userAPI, PolicyMessages policyMessages,
                                    PolicyRepository policyRepository,
                                     Validator validator) {
        this.cacheablePolicyDao = cacheablePolicyDao;
        this.userCustomPolicyDao = userCustomPolicyDao;
        this.userAPI = userAPI;
        this.policyMessages = policyMessages;
        this.policyRepository = policyRepository;
        this.validator = validator;
    }

    @Override
    @Cacheable(sync = true)
    public SphereClient createSphereClientA(@Nonnull TenantConfig tenantConfig) {
        // 2. call cached method using self-bean
        return self.createSphereClient(tenantConfig.getSphereClientConfig());
    }

    @Override
    @Cacheable(sync = true)
    public SphereClient createSphereClientB(@Nonnull SphereClientConfig clientConfig) {
        return userAPI.createSphereClient(clientConfig);
    }
    ......... //Some more methods
}

The code is working perfectly fine!. Now I 'm struggling with writing JUnit for the createSphereClientA function. I am not able to Mock SphereClientFactory .

I'm using

       Mockito Version : 3.3.0
       PowerMock Version : 2.0.5

I tried various ways like using the below one & running with MockitoJUnitRunner

@Mock 
SphereClientFactory self

Or PowerMocking the object and running on PowerMockRunner but nothing works :(. Everytime I am getting NullPointerException because SphereClientFactory is actually never mocked with both the approaches.

Anyone has any clue how can I do this ? Also does the above solution will produce cyclic dependency?

Comment From: snicoll

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.