Expand my Community achievements bar.

SOLVED

Mocking JobManager in junit unit TEst

Avatar

Level 2

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.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Dody, could you please try below option

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

View solution in original post

8 Replies

Avatar

Level 2

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.

Avatar

Community Advisor

Hi @Dody 

 

May be some luck with changing annotation to?

@InjectMocks

Regards,

Santosh

Avatar

Level 2

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.

Avatar

Correct answer by
Community Advisor

Hi @Dody, could you please try below option

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

Avatar

Level 2

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. 

Avatar

Level 4

Hi @Dody ,

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!

Avatar

Level 2

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.