Unable to create mock for Authenticator in Junit | Community
Skip to main content
sravs
New Participant
February 24, 2023
Solved

Unable to create mock for Authenticator in Junit

  • February 24, 2023
  • 1 reply
  • 634 views

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

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by krishna_sai

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

1 reply

krishna_sai
krishna_saiAccepted solution
New Participant
February 24, 2023

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