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.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Dody, could you please try below option
@Mock private JobManager jobManager; //... aemContext.registerService(JobManager.class, jobManager);
Views
Replies
Total Likes
Hi Dody,
please check below thread and see if that helps you:
Also , it might be an issue with Uber jar version as well, pls check below also:
thanks
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Hi @Dody, could you please try below option
@Mock private JobManager jobManager; //... aemContext.registerService(JobManager.class, jobManager);
Views
Replies
Total Likes
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.
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!
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.
Views
Replies
Total Likes
Views
Like
Replies
Views
Likes
Replies
Views
Likes
Replies