-2

I have below method which i want to test using EasyMock.

public String createNode(Session session, String name) throws RepositoryException {
    Node root = session.getRootNode();
    Node testNode = root.getNode( "content" );
    if( !testNode.hasNode(name) ) {
        testNode.addNode( name, "nt:unstructured" );
    }
    return testNode.getPath()+"/"+name;
}

And test method is :

@Test
public void createNodeTest() throws RepositoryException {   
    final Session SESSION_MOCK = EasyMock.createNiceMock((Session.class));
    final Node ROOT_NODE_MOCK = EasyMock.createNiceMock(Node.class);
    final Node CONTENT_NODE_MOCK = EasyMock.createNiceMock(Node.class);
    final Node CHILD_NODE_MOCK = EasyMock.createNiceMock(Node.class);

    EasyMock.expect(SESSION_MOCK.getRootNode()).andReturn(ROOT_NODE_MOCK);       
    EasyMock.expect(ROOT_NODE_MOCK.getNode("content")).andReturn(CONTENT_NODE_MOCK);        
    EasyMock.expect(CONTENT_NODE_MOCK.hasNode("viv")).andReturn(false);        
    EasyMock.expect(CONTENT_NODE_MOCK.addNode("viv","nt:unstructured")).andReturn(CHILD_NODE_MOCK);        
    EasyMock.expect(CHILD_NODE_MOCK.getPath()).andReturn("/content/viv");
    EasyMock.replay(SESSION_MOCK,ROOT_NODE_MOCK,CONTENT_NODE_MOCK);       

    TestableClass tc = new TestableClass();
    assertEquals("/content/viv", tc.createNode(SESSION_MOCK,"viv"));          

    EasyMock.verify(SESSION_MOCK,CONTENT_NODE_MOCK,ROOT_NODE_MOCK);     
}

Getting failure :

org.junit.ComparisonFailure: expected:<[/content]/viv> but was:<[null]/viv>

Anyone can tell where i am wrong, i want to get the test pass. Thanks

Dan Temple
  • 2,736
  • 2
  • 22
  • 39
Vivek Dhiman
  • 1,967
  • 6
  • 46
  • 82
  • If you switch from `createNiceMock` to `createMock` EasyMock will throw an exception when it sees a call it is unprepared for. You may need to add some extra calls to `expect(...).andStubReturn(...)`, but in any case it'll help with debugging. – Jeff Bowman May 16 '14 at 21:55

1 Answers1

0

I think your issue is coming around these lines in your method:

if( !testNode.hasNode(name) ) {
    testNode.addNode( name, "nt:unstructured" );
}
return testNode.getPath()+"/"+name;

The expectations you have set up for it are as follows:

EasyMock.expect(CONTENT_NODE_MOCK.hasNode("viv")).andReturn(false);        
EasyMock.expect(CONTENT_NODE_MOCK.addNode("viv","nt:unstructured")).andReturn(CHILD_NODE_MOCK);        
EasyMock.expect(CHILD_NODE_MOCK.getPath()).andReturn("/content/viv");

The problem is that your code doesn't use the response from the addNode() method, but your test expectations are set up such that the code would use the response from the addNode() method.

The way I see it you have 2 options:

  1. If the code is correct, then you need to adjust your expectations to something like this:

    EasyMock.expect(CONTENT_NODE_MOCK.hasNode("viv")).andReturn(false);        
    EasyMock.expect(CONTENT_NODE_MOCK.addNode("viv","nt:unstructured")).andReturn(CHILD_NODE_MOCK);        
    EasyMock.expect(CONTENT_NODE_MOCK.getPath()).andReturn("/content/viv");
    
  2. If the test is correct (that'd be nice, TDD and all that) then you need to alter your code to look like this:

    Node childNode = testNode; //Or some other valid creation
    if( !testNode.hasNode(name) ) {
        childNode = testNode.addNode( name, "nt:unstructured" );
    }
    return childNode.getPath()+"/"+name;
    
Dan Temple
  • 2,736
  • 2
  • 22
  • 39