내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

Unable to inject a service in junit test cases to test a sling model (having an injection of service using @OSGiService annotation)

Avatar

Level 1

Hi All,

I recently started using AemMocks and am still exploring. In one of the requirements, Sling models are implemented all looks fine till now. I started unit testing the same using Aem Mocks.

Problem:

I am unable to inject the service reference into the test class using 'context.registerInjectActivateService'.I have gone through numerous reference documents online but nothing is helpful.

any help is appreciated.

Thanks,

Harshith.

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Community Advisor

Hi HKesani,

Could you please give a try to below code to mock OSGi service?

1852742_pastedImage_1.png

Reference: https://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service

Regards,

Arpit Varshney

원본 게시물의 솔루션 보기

2 답변 개

Avatar

정확한 답변 작성자:
Community Advisor

Hi HKesani,

Could you please give a try to below code to mock OSGi service?

1852742_pastedImage_1.png

Reference: https://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service

Regards,

Arpit Varshney

Avatar

Level 9

Unfortunately, none of these methods work if the service you want to call calls another service. In this case, the first service fails with the error:

 

org.apache.sling.testing.mock.osgi.ReferenceViolationException: Unable to inject mandatory reference 'secondService' for class com.mycompany.core.services.impl.FirstServiceImpl : no matching services were found.