CQ5 Junit test cases for OSGI services | Community
Skip to main content
October 16, 2015
Solved

CQ5 Junit test cases for OSGI services

  • October 16, 2015
  • 2 replies
  • 1435 views

Hi,

I am trying to write junit test cases for my osgi services. I tried to follow the below sling testing tool.

http://sling.apache.org/documentation/development/sling-testing-tools.html

But this is not very intuitive.

Here is what I did to set it up .

  1. Added maven dependency.
    <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.junit.core</artifactId> <version>1.0.8</version> </dependency>
  2.  Install the bundle from Felix console.

Now I tried accessing http://localhost:8080/system/sling/junit/ but this gives 404 error. 

Am I missing anything here ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by

Many OSGi services can be successfully [unit] tested outside of the run-time, that is, they are just POJO so you can test them using JUnit and your favourite mocking framework (e.g. EasyMock). If you need dependency injection, that's no problem either. Whilst in-context testing is certainly very important, so I'm not advocating you don't need do that as well, you can get a long way (catch most bugs early following TDD) by eliminating your external dependencies including complex run-time set-up. Once you are happy that all of your unit tests pass locally, by all means deploy to your OSGi run-time and repeat (along with any integration (black-box) tests).

HTHs

Fraser.

2 replies

Lokesh_Shivalingaiah
Level 10
October 16, 2015

Some basic verifications !

is 'Apache Sling Junit Core' bundle is installed and active ?

is test folder included in your bundle ?

does your testcases configured to RunWith SlingRemoteTestRunner.class ?

Accepted solution
October 16, 2015

Many OSGi services can be successfully [unit] tested outside of the run-time, that is, they are just POJO so you can test them using JUnit and your favourite mocking framework (e.g. EasyMock). If you need dependency injection, that's no problem either. Whilst in-context testing is certainly very important, so I'm not advocating you don't need do that as well, you can get a long way (catch most bugs early following TDD) by eliminating your external dependencies including complex run-time set-up. Once you are happy that all of your unit tests pass locally, by all means deploy to your OSGi run-time and repeat (along with any integration (black-box) tests).

HTHs

Fraser.