Testing OSGI Bundle Using JUnit

In my last blog post I explained how to create a hello world application using Maven, OSGi in Opendaylight framework (although it had nothing to do with networking, I will be explaining that too in coming blogs). For those who haven’t read that please follow the below link:

http://sdntutorials.com/hello-world-application-using-osgi-bundle/

As I believe “A not tested or less tested code is more harmful”, so it is better I first tell you the basics of that too. We will be testing our Hello World application by creating a basic build time test case. Build time tests are tests which run during the compilation phase of your project .

JUnit is a framework for writing build time tests in opendaylight.

Adding JUnit to your Maven Project

To add the JUnit dependency to your maven project you simply add this dependency into the dependencies section of your pom file.

Note: You can exclude the scope and version tags assuming a parent project has defined the dependency in the dependency management section.

Writing The Testcase

As mentioned in Hello World Application, we are maintaining a structure similar to other bundles of Opendaylight, so for us the path will be:

helloworld/src/test/java/org/opendaylight/controller/helloworld

Now we can create a file named HelloworldTest.java ( simple code to test the creation of our HelloWorld, you can add as many as you can to better test your bundle)

Running a JUnit Test

To run a JUnit tests via the command line you only need to run:

However, that only runs the tests, it doesn’t recompile them. You can run below command as it re-compiles the bundle and then executes the JUnit tests.

Note: This is just a simple example but it is necessary for everyone to write test cases before he/she submits code to opendaylight community and in general also it’s always a good practice.

Leave a Reply

Your email address will not be published. Required fields are marked *