Saturday, October 19, 2013

Java Unit testing: Mocking JNDI, final classes, statics and private parts

To improve quality of an application, it is a good practice to provide unit tests together with the code which is written. This makes it easier to determine the impact of changes and fix them early during development.

Writing unit tests can be challenging. Often applications run inside an application server which provides services and a unit test runs outside of the server. Applications can depend on the services provided by the application server which makes testing outside of this scope difficult. One of those services is JNDI. JNDI makes it possible for an application to look up for example a datasource. Luckily JNDI can be provided outside of application server scope.

To make mocking methods in unit tests easier, the Mockito framework ( can be used. Mockito however uses a subclassing mechanism which does not work when dealing with final classes. Also static and private methods cannot be mocked because of this. PowerMock provides solutions for those issues;


First we'll create an OracleConnectionPoolDatasource which we then make available outside of application server scope via JNDI. Next there is an example how final classes, private fields/methods and statics can be mocked.


We want to create an OracleConnectionPoolDatasource since we'll be working with an Oracle database. The Oracle datasource class is not present in a legal public Maven repository due to license restrictions.

You can however still use Maven for this dependency as follows;

Download the driver from Oracle;

Define a dependency in your pom file as below and make sure the jar file is in the correct place.

    <systemPath>${basedir}/lib/ojdbc6.jar</systemPath>   <--- must match file name

The above is also useful when you're working with Liquibase ( from Maven.

Mocking JNDI

JNDI can be mocked with;

A Maven dependency for this;


Below some sample code to create an Oracle Db testuserDS datasource and make it available via JNDI.

SimpleNamingContextBuilder builder = null;
OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
builder.bind("java:comp/env/jdbc/testuserDS", ds); // mocking for default Java application lookups
builder.bind("jdbc/testuserDS", ds); // mocking for hibernate

The above code can be placed in the BeforeClass method if you're using JUnit.

Mocking final classes, statics and private parts

Mockito in combination with Powermock make your life a lot easier to accomplish the tasks as specified in the topic. The class to be tested;

public final class App {

    private String getGreeting() {
        return "Hello";
    public static String getName() {
        return "Maarten";

    public String helloName() {
        return getGreeting()+ " " + getName();

My unit test class is shown below. It illustrates mocking of a final class, a static method and a private method in that class.

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.mockito.PowerMockito.spy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;

 * @author maarten

public class AppTest {
     * Test of helloName method, of class App.
    public void testHelloName() throws Exception {
        App instance = spy(new App()); //this allows me to mock private methods
        PowerMockito.mockStatic(App.class); //this allows me to mock static methods
        PowerMockito.when(App.getName()).thenReturn("MockedMaarten"); //mocking static method here
        PowerMockito.doReturn("MockedHello").when(instance,"getGreeting"); //mocking private method here
        String expResult = "MockedHello MockedMaarten";
        String result = instance.helloName();
        assertEquals(expResult, result);

For reference and to provide an easy way to get the dependencies, my pom file is as follows. Using properties to specify dependency versions is recommended.

<project xmlns="" xmlns:xsi=""





No comments:

Post a Comment