Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Mocking JobManager in junit unit TEst

Dody
Level 2
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
lukaszm
Correct answer by
Level 5
Level 5

Hi @Dody, could you please try below option

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

View solution in original post

8 Replies
Dody
Level 2
Level 2

Hi @SivaPrasadReddyS ,

 

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

 

Thanks.

SantoshSai
Level 5
Level 5

Hi @Dody 

 

May be some luck with changing annotation to?

@InjectMocks

Regards,

Santosh

Dody
Level 2
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.

lukaszm
Correct answer by
Level 5
Level 5

Hi @Dody, could you please try below option

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

View solution in original post

Dody
Level 2
Level 2

Hi @lukaszm ,

 

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 4
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!

Dody
Level 2
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.