Mocking JobManager in junit unit TEst | Community
Skip to main content
Level 2
October 25, 2021
Solved

Mocking JobManager in junit unit TEst

  • October 25, 2021
  • 4 replies
  • 4419 views

Hi everybody, 

 

Any help will be appreciated.

 

i am trying to create a unit test for  myCustomService. This service has a reference to a JobManager. I didn't find any precise documentation to do that. 

Here what i do :

 

@ExtendsWith({AemContextExtension.class,MockitoExtension.class})

class myCustomServiceTest

.

.

 @Mock

 private JobManager jobManager;

 

.private MyCustomService myCustomService = new MyCustomService () 

.

AemContext aemContext = new AemContext(); 

 

.

.

aemContext.registerService(JobManager);   

 

aemContext.registerInjectActivateService(myCustomService); Line XX7

 

 

And i got a :

org.apache.sling.testing.mock.osgi.ReferenceViolationException: Unable to inject mandatory reference 'jobManager' ... no matching services were found  at Line xx7.

 

 

PS : when i try to use : 

AemContext aemContext = new AemContext(ResourceResolverType.JCR_OAK);

I have the following message :

java.lang.RuntimeException: Unable to initialize JCR_OAK resource resolver factory: Unable to instantiate resourcer resolver: org.apache.sling.testing.mock.sling.oak.OakMockResourceResolverAdapter. Make sure this maven dependency is included: org.apache.sling:org.apache.sling.testing.sling-mock-oak

Maybe could someone help me to understand : how do i have to mock a reference used in the service i want to test.

 

Thanks in advance.

 

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 lukasz-m

Hi @dody-2, could you please try below option

@Mock
private JobManager jobManager;
//...
aemContext.registerService(JobManager.class, jobManager);

4 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
October 25, 2021
Dody-2Author
Level 2
October 25, 2021

Hi @siva_sogalapalli ,

 

Thanks for taking some time for me. I already saw that ticket but doesn't feel it's the same isssue.

 

Thanks.

SantoshSai
Community Advisor
Community Advisor
October 25, 2021

Hi @dody-2 

 

May be some luck with changing annotation to?

@InjectMocks

Regards,

Santosh

Santosh Sai
Dody-2Author
Level 2
October 26, 2021

Hello @santoshsai .


Thanks for your help. I tried to change the @Mock with @InjectMock on the jobManager but it doesn't work too. :

Cannot instantiate @InjectMocks field named 'jobManager'! Cause: the type 'JobManager' is an interface.
You haven't provided the instance at field declaration so I tried to construct the instance.

 

Regards.

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
October 26, 2021

Hi @dody-2, could you please try below option

@Mock
private JobManager jobManager;
//...
aemContext.registerService(JobManager.class, jobManager);
Dody-2Author
Level 2
October 26, 2021

Hi @lukasz-m ,

 

Thanks for your help. I tried that yesterday and had an error so i thought it wasn't working properly. In fact the error was related to my config and as you said : 

aemContext.registerService(JobManager.class, jobManager); 

works well !

 

Thanks again. 

MohitKumarK
Level 3
October 26, 2021

Hi @dody-2 ,

You have to use @Mock for JobManager and after this line @InjectMocks for private MyCustomService myCustomService = new MyCustomService () which inject all your mocks to your service mock.

 

@Mock

private JobManager jobManager;

 

@InjectMocks

private MyCustomService myCustomService = new MyCustomService ();

 

Just curious, you said you are myCustomService, by the name it implies you are testing a service (which could be a interface). Does this line is valid private MyCustomService myCustomService = new MyCustomService ()

Please cross check if you are initializing your service properly.

 

Thanks!

 

Thanks!

Dody-2Author
Level 2
October 26, 2021

Hi @mohitkumark ,

 

it works well now ( cf previous answer ).

 

Thanks for your remark. I checked and everything is working fine.

 

Thanks for your time.

 

Regards.