0 Replies Latest reply on Jul 20, 2017 3:50 AM by kum_deep

    Unable to inject OSGI Services: jUnit Test Case !!

    kum_deep

      I am unable to get the reference of the OSGI Services when ran through Mockito. Every time I run the Test Class it gives me null wherever that service is invoked.

      Any help is highly appreciated.

       

      I am following this tutorial: Apache Sling - OSGi Mocks , but no luck.

       

      Below is the code snippet (Test Class) that i run:

       

      HashMap<Object, Object> _configServiceEndpoint = new HashMap<Object, Object>();

        HashMap<Object, Object> _configCommonConfig = new HashMap<Object, Object>();

       

       

        /** Mocking required sling request params */

        slingRequest = mock(SlingHttpServletRequest.class);

       

       

        /** Mocking required sling request params */

        slingResponse = mock(SlingHttpServletResponse.class);

       

       

        /** Setting Service End Point */

        _configServiceEndpoint.put("sendEmailURL",

        "https://MLIGATEWAY.MAXLIFEINSURANCE.COM/mli/prod/soa/MLI_E-mail_rest");

        _configServiceEndpoint.put("sendSmsURL", "https://gatewayuat.maxlifeinsurance.com/apimgm/sb/soa/sms/v2/real");

        _configServiceEndpoint.put("otpGenerationURL",

        "https://gatewayuat.maxlifeinsurance.com/apimgm/sb/soa/otp/generate?client_id=763b645e-4034 -4152-9631-840ed5719ad8&client_secret=X8hT0tW1vS7gD2qE7gK1lD1kE8iF1rF7vG5bQ1gI0jG4xX3jI6");

        _configServiceEndpoint.put("otpValidationURL",

        "https://gatewayuat.maxlifeinsurance.com/apimgm/sb/soa/otp/validate?client_id=763b645e-4034 -4152-9631-840ed5719ad8&client_secret=X8hT0tW1vS7gD2qE7gK1lD1kE8iF1rF7vG5bQ1gI0jG4xX3jI6");

        serviceEndPointConfigImpl = context.registerInjectActivateService(new ServiceEndPointImpl(),

        _configServiceEndpoint);

       

       

        /** Mocking up the timeout config needed for service Agent class */

        _configCommonConfig.put("serviceTimeOut", "25000");

        //commonConfigImpl = context.registerInjectActivateService(new CommonConfigImpl(), _configCommonConfig);

       

        GenerateOTP generateOTP = new GenerateOTP();

        SendOtpSms sendOtpSms = new SendOtpSms();

        SendOtpEmail sendOtpEmail = new SendOtpEmail();

        //ServiceAgent serviceAgent = new ServiceAgent();

        CommonConfigImpl commonConfig = new CommonConfigImpl();

       

        commonConfig = mock(CommonConfigImpl.class);

        serviceAgent = mock(ServiceAgent.class);

       

        BundleContext bundleContext = MockOsgi.newBundleContext();

       

        MockOsgi.activate(commonConfig, bundleContext, _configCommonConfig);

        //MockOsgi.injectServices(cmConfig, bundleContext);

       

        MockOsgi.activate(serviceAgent, bundleContext);

        MockOsgi.injectServices(commonConfig, bundleContext);

       

        MockOsgi.activate(generateOTP, bundleContext);

        MockOsgi.injectServices(serviceAgent, bundleContext);

       

        MockOsgi.activate(sendOtpSms, bundleContext);

        MockOsgi.injectServices(serviceAgent, bundleContext);

       

        MockOsgi.activate(sendOtpEmail, bundleContext);

        MockOsgi.injectServices(serviceAgent, bundleContext);

          

       

       

      @Test

          public void testMethods() throws Exception{

        this.doGet(slingRequest, slingResponse);

          }

       

      The class where "doGet" is implemented, has two service References which internally reference another Service.

      It gives me the following error:

       

      org.apache.sling.testing.mock.osgi.ReferenceViolationException: Unable to inject mandatory reference 'commonConfig' for class com.mli.neo.global.http.ServiceAgent : no matching services were found.

        at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServiceReference(OsgiServiceUtil .java:407)

        at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServices(OsgiServiceUtil.java:39 0)

        at org.apache.sling.testing.mock.osgi.MockOsgi.injectServices(MockOsgi.java:148)

        at com.mli.neo.global.SendOTPTest.<init>(SendOTPTest.java:90)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

        at java.lang.reflect.Constructor.newInstance(Unknown Source)

        at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:178)

        at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java: 227)

        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

        at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:224)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)

        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

        at org.junit.runners.ParentRunner.run(ParentRunner.java:292)

        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.ja va:37)

        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)

        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:8 6)

        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459 )

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675 )

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

       

       

      Request a concrete solution to achieve this. Any help is highly appreciated.

       

      Regards,Deepak Kumar