Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Unable to create mock for Authenticator in Junit

Avatar

Level 5

My service class has a reference object of org.apache.sling.api.auth.Authenticator;

 

While I'm trying to implement a test class I'm getting the below error message for Authenticator.

 

org.apache.sling.testing.mock.osgi.ReferenceViolationException: Unable to inject mandatory reference 'authenticator' for class MyService

 

I have tried to create a mock object of Authenticator and registering it but the issue is still there. Can you please help me with this.

@Mock
Authenticator authenticator;

@BeforeEach
void setup(){
context.registerService(Authenticator.class, authenticator);
context.registerInjectActivateService(new MyServiceImpl(), "title", "My Service");
}

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @sravs , Can you try making the authenticator reference optional in your Service class.

@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL)
    private volatile Authenticator authenticator;


Thanks,

Krishna

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @sravs , Can you try making the authenticator reference optional in your Service class.

@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL)
    private volatile Authenticator authenticator;


Thanks,

Krishna