Question

I recently came across a weird problem with EasyMock. Here's the class which I am testing

package com.test.junits;

import org.openide.nodes.Node;

public class IsRoot {
    public boolean isRoot(Node node) {
        Node parentNode = node.getParentNode();
        return (parentNode == null);
    }
}

And here is the Junit class

package com.test.junits

import static org.junit.Assert.assertNotNull;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.openide.nodes.Node;

public class IsRootTest {

private IsRoot isroot;
private Node node;

@Before
public void setUp() {
    isroot = new IsRoot();
    node = EasyMock.createMock(Node.class);
}

@Test
public void gettingExpectationsForGetParentFromNode() {
    Node node = EasyMock.createMock(Node.class);
    Node parentNode = EasyMock.createMock(Node.class);

    EasyMock.expect(node.getParentNode()).andReturn(parentNode);

    EasyMock.replay(node);
    isroot.isRoot(node);
    EasyMock.verify(node);
}

When I try running it I get the following exception (with maven and eclipse)

java.lang.IllegalStateException: no last call on a mock available
    at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
    at org.easymock.EasyMock.expect(EasyMock.java:499)
    at com.test.junits.IsRootTest.gettingExpectationsForGetParentFromNode(IsRootTest.java:47)
    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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Am I missing something here? Any help would be appreciated.

Was it helpful?

Solution

It's probably something to do with the fact you have a local field called node and a class variable called node both of which are Node classes and both of which are mocks.

I would remove the test local node altogether, or rename it if you really want a test-local mock node

EDIT

My previous answer was wrong, and should really have been a comment. The hidden objects weren't causing any problems. (Although I still think they should have been removed/renamed)

The actual problem here is that the method you are trying to mock is final and as such, can't be mocked.

When EasyMock mocks method/classes, it does so by extending and overriding them. So final classes and final methods cannot be mocked with EasyMock as they can't be overridden.

The call to the getParentNode() method is not mocked and as such isn't registered during the record state within the expectation call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top