Gerko Ford Gerko Ford - 1 year ago 83
Java Question

How to call a method from a mocked interface using EasyMock

I am writing a Junit unit test for a class and I am getting a java.lang.NullPointerException on the following line :


I think (i am not sure though) that it has something to do with the method (getDeviceControlHandler) that I am calling from within the mocked interface . Because I have added this line of code before the submentioned line:


And I am having the following error:


I am stuck here and really need some help.

Thanks in Advance.

The thrown Exception:

at de.myproject.project.classTest.testGetParameters(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
at org.junit.runners.model.FrameworkMethod.invokeExplosively(
at org.junit.internal.runners.statements.InvokeMethod.evaluate(
at org.junit.internal.runners.statements.RunBefores.evaluate(
at org.junit.runners.ParentRunner.runLeaf(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.ParentRunner$
at org.junit.runners.ParentRunner$1.schedule(
at org.junit.runners.ParentRunner.runChildren(
at org.junit.runners.ParentRunner.access$000(
at org.junit.runners.ParentRunner$2.evaluate(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

Here is the written test:

public class classTest {

// class under test
private classUnderTest classUnderTest;

private LineConfigurationHandler LineConfigurationHandlerMock;

private IMocksControl mocksControl;

List<DeviceParameter> myDeviceParameters;
DeviceParameter deviceParameter1;
DeviceParameter deviceParameter2;

public void setUp() throws Exception
mocksControl = EasyMock.createControl();

LineConfigurationHandlerMock = mocksControl.createMock(LineConfigurationHandler.class);

classUnderTest = new classUnderTest();

String item1 = "item1";

myDeviceParameters = new ArrayList<DeviceParameter>();

//Other stuff

public void testGetParameters()



//Some code .....

Here is the class under test :

public Class ClassUnderTest
private LineConfigurationHandler lineConfigurationHandler;

public List<DeviceParameter> getDeviceParameters(String deviceId)
// Method implementation

public void setLineConfigurationHandler(LineConfigurationHandler lineConfigurationHandler)
this.lineConfigurationHandler = lineConfigurationHandler;

The interface in which the method is declared

public interface LineConfigurationHandler {

DeviceControlHandler getDeviceControlHandler();

//other Method declaration ...


public interface DeviceControlHandler extends Serializable{

List<DeviceParameter> getDeviceParameters(String deviceId);

//Other methods declaration ...

Answer Source

It is not simple, but very deterministic:


That line contains two items that can throw NPE:

A) lineConfigurationHandlerMock --> that object can be NULL

B) .getDeviceControlHandler() --> that method can return NULL

That's it. You can do simple printouts, like

System.out.println("mock: " + lineConfigurationHandlerMock)
System.out.println("handler: " + lineConfigurationHandlerMock.getDeviceControlHandler())

to figure which one is null. In your case, I think you are missing the setup for your lineConfigurationHandlerMock object: you have to tell it what to return when getDeviceControlHandler() is called.

In order to do that, you first have to create another mock object that should be returned when getDeviceControlHandler() is called. And that other mock, you have to configure for a call to getDeviceParameters()!

In other words: you can't specify like "mock.getA().doSomething()" - instead, you need another "mockedA" which you tell "doSomething()"; and then you tell "mock" that getA() should return "mockedA".

Update: I am not familiar with these annotations; I am used to use "EasyMock in a bare metal mode"; like

SomeObject innerMock = EasyMock.createMock(SomeObject);
expect(innerMock.doSomething()).andReturn("who cares");

SomeOther outerMock = EasyMock.createMock(SomeOther);
expect(outerMock.getDeviceControlHandler("sounds familiar")).andReturn(innerMock);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download