Home » Docs

Writing unit/integration tests

This tutorial describes how to write unit and integration tests for ACE. For its unit tests, ACE relies on TestNG, while for the integration tests a combination of BndTools and JUnit is used.

Writing unit tests

Unit tests test the behavior of classes or methods in complete isolation. In case this is not possible, due to dependencies on other classes, one can make use of a mocking framework such as Mockito to replace those dependencies with stub implementations.

Writing unit tests with TestNG is much like writing tests with JUnit: you create a class which contains methods annotated with the @Test annotation. Also, you can add methods that are run before or after each test or test case. One feature that is distinct to TestNG is the ability to group tests, making it possible to run only a certain group of tests instead of all1.

All unit tests are placed in the same project they are testing, in a separate directory named test. This allows the tests to be compiled and run independently from the remaining code. It is good practice to have the same package structure for your tests and other code.

Example

Lets take a look at an excerpt from the unit test2 for AuthenticationServiceImpl:

public class AuthenticationServiceImplTest {
   private LogService m_log;

   @BeforeMethod(alwaysRun = true)
   public void setUp() {
       m_log = Mockito.mock(LogService.class);
   }

   /**
    * Tests that an exception is thrown if a null context is given.
    */
   @Test(groups = { "UNIT" }, expectedExceptions = IllegalArgumentException.class)
   public void testAuthenticateFailsWithNullContext() {
      new AuthenticationServiceImpl(m_log).authenticate((Object[]) null);
   }

   /**
    * Tests that without any authentication processors, no authentication will take place.
    */
   @Test(groups = { "UNIT" })
   public void testAuthenticateFailsWithoutAuthProcessors() {
       Assert.assertNull(createAuthenticationService().authenticate("foo", "bar"), "Expected authentication to fail!");
   }

   // ...
}

This snippet shows us almost all important concepts for TestNG:

To run the unit tests for a project, you simply go to the root directory of the project itself, and call:

$ ant testng

This will run the unit tests using TestNG. The output of the tests can be found in the test-output directory of your project. To run the test from Eclipse, you can right click on it, and select "Run As -> TestNG Test" from its context menu.

Writing integration tests

In an integration test you test whether a small part of your system works as expected. For this, you need to set up an environment which resembles the situation in which the system is finally deployed. For ACE, this means that we need to set up an OSGi environment and a subset of the bundles we want to test. Fortunately, BndTools provides us with easy tooling to just do that with the use of JUnit33.

Integration test principles

To write a very basic (OSGi) integration test, you need to extend your test from junit.framework.TestCase. To access the bundle context of your test case, you can make use of some standard OSGi utility code:

public class MyIntegrationTest extends junit.framework.TestCase {
    private volatile org.osgi.framework.BundleContext m_context;

    //...

    protected void setUp() throws Exception {
        m_context = org.osgi.framework.FrameworkUtil.getBundle(getClass()).getBundleContext();
    }
}

With this in place, we can now make OSGi calls from our test methods.

To ease the writing of integration tests, ACE provides a IntegrationTestBase4 helper class that provides you with some boilerplate code commonly used in integration tests. Internally, it makes use of Felix Dependency Manager to control and manage the desired service dependencies.

Example

In this section, we'll describe how to write an integration test that makes use of a 'UserAdmin' service in its test method.

The first we need to do is create our test class:

public class UserAdminIntegrationTest extends IntegrationTestBase {
    private volatile org.osgi.service.useradmin.UserAdmin m_userAdmin;

    protected org.apache.felix.dm.Component[] getDependencies() {
        return new org.apache.felix.dm.Component[] {
            createComponent()
                .setImplementation(this)
                .add(createServiceDependency().setService(org.osgi.service.useradmin.UserAdmin.class).setRequired(true))
        };
    }

    // ...
}

Having explained that our test depends upon the 'UserAdmin' service, and that its a required dependency, means that our test won't run if this service is not available5.

Sometimes, you need to do some additional set up before or after the dependencies are resolved. For example, one might need to provision some configuration in order to get dependencies in the correct state, or wait until some other condition is met after all dependencies have been resolved. In order to do this, one can override the before() or after() methods6 in IntegrationTestBase.
For our simple integration test, however, we do not need such "advanced" set up.

The only thing left is the actual test itself. As the 'UserAdmin' service is being tracked for us, we can simply direct use it in our test:

public class UserAdminIntegrationTest extends IntegrationTestBase {
    // ...

    public void testUserAdminCreateGroupRoleOk() throws Exception {
        org.osgi.service.useradmin.Role group = 
            m_userAdmin.createRole("MyGroup", org.osgi.service.useradmin.Role.GROUP);

        Assert.assertNotNull(group);
        Assert.assertEquals("MyGroup", group.getName());
    }

    // ...
}

That is it! The only thing left is to run the test, which is as simple as:

$ ant test

When running Eclipse, you can also run your integration test by right clicking on it, and selecting Run As -> OSGi JUnit Test from its context menu.

Notes


  1. The consequence is that TestNG also allows you to write other kinds of tests than unit tests, like integration tests or performance tests. ACE does not make use of this kind of functionality, and only uses TestNG for its unit testing. 

  2. This test can be found in the org.apache.ace.authentication project. 

  3. The current version of BndTools still uses JUnit3, while there is some effort already to support JUnit4 as well, see: https://github.com/bndtools/bnd/issues/156

  4. This helper class can be found in the org.apache.ace.test project. 

  5. To be more precise: the test will wait a couple of seconds to allow all dependencies to be satisfied. If after this waiting period any dependency is not satisfied, the test will fail. 

  6. The naming of these methods could use a second thought, see also: https://issues.apache.org/jira/browse/ACE-289