I have problem with Mockito mocking library
My Junit4 test class with 2 test suites.
test one:
@Test
public void test1()
{
Class class = new Class();
Class classSpy = Mockito.spy(class);
Mockito.when( classSpy.getExpectedValue()).thenReturn("expected_one");
}
and second test:
@Test
public void test2()
{
Class class = new Class();
Class classSpy = Mockito.spy(class);
Mockito.when( classSpy.getExpectedValue()).thenReturn("expected_two");
}
and I have testable class:
public class TestableClass
{
x = class.getExpectedValue();
//some code
}
And the problem is:
1 - I run test class with test1()
and test2()
At first is running test1()
debugger indicate the x = "expected_one"
all is ok - this is expected behavior
2 - test2 is running
I put breakpoint in my testable class at line with x..
and x = "expected_one"
I seems Mockito uses the same reference to spying object (classSpy
) in both test.!!
thanks in advance for help
ps: i use mockito 1.9.0 and jre 6.0.370.6
ps: I have normal SetUp method:
@Before
public void setUp(){
testableClass = new TestableClass();
}
ps3: full test suite:
package xxx;
import java.io.File;
import java.io.IOException;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import xxx.testdata.JUnitConstants;
import xxx.testdata.JUnitUtils;
import xxx.testdata.MockNode;
@SuppressWarnings( "deprecation" )
public class MONodeModifierTest
{
private MONodeModifier moNodeModifier;
private Document doc;
static File file;
@Before
public void setUp(){
doc = JUnitUtils.CreateXMLDocumentFromFile(file);
moNodeModifier = new MONodeModifier(doc);
}
@Test
public void createNodeTest(){
Node inputMoNode = new MockNode();
boolean nodeisCreated = moNodeModifier.createMONode( inputMoNode, doc );
Assert.assertTrue(nodeisCreated);
}
@Test
public void removeNodeTest(){
final Node inputMoNode = new MockNode();
NodeList nodeList = new NodeList(){
@Override
public Node item( int index )
{
return inputMoNode;
}
@Override
public int getLength()
{
return 1;
}};
boolean nodeisRemoved = moNodeModifier.removeMONode( inputMoNode,nodeList );
Assert.assertTrue(nodeisRemoved);
}
@Test
public void updateMONodeWithPname(){
Node node = new MockNode();
NodeList nodeListMock = Mockito.mock( NodeList.class );
Node nodeSpy = Mockito.spy(node);
Mockito.when( nodeSpy.getChildNodes()).thenReturn( nodeListMock);
Mockito.when( nodeSpy.getNodeName()).thenReturn( "p");
Mockito.when( nodeListMock.getLength()).thenReturn( 1);
Mockito.when( nodeListMock.item(Mockito.anyInt())).thenReturn( nodeSpy);
boolean nodeisUpdated = moNodeModifier.updateMONode( nodeSpy, nodeListMock );
Assert.assertTrue(nodeisUpdated);
Mockito.verify( nodeSpy).setTextContent(Mockito.anyString());
}
@Test
public void updateMONodeWithNonEmptyListName(){
Node node = new MockNode();
NodeList nodeListMock = Mockito.mock( NodeList.class );
Node nodeSpy = Mockito.spy(node);
Mockito.when( nodeSpy.getChildNodes()).thenReturn( nodeListMock);
Mockito.when( nodeSpy.getNodeName()).thenReturn( "list");
Mockito.when( nodeListMock.getLength()).thenReturn( 1).thenReturn( 1);
Mockito.when( nodeListMock.item(Mockito.anyInt())).thenReturn( nodeSpy);
boolean nodeisUpdated = moNodeModifier.updateMONode( nodeSpy, nodeListMock );
Assert.assertTrue(nodeisUpdated);
Mockito.verify( nodeSpy).replaceChild(Mockito.any(Node.class),Mockito.any(Node.class));
}
@Test
public void updateNonExistMONodeType(){
Node node = new MockNode();
Node nodeSpy = Mockito.spy(node);
NodeList nodeListMock = Mockito.mock( NodeList.class );
Mockito.when( nodeSpy.getChildNodes()).thenReturn( nodeListMock);
Mockito.when( nodeSpy.getNodeName()).thenReturn( "p");
Mockito.when( nodeSpy.getNodeType()).thenReturn( (short) 1).thenReturn( (short) 1).thenReturn( (short) 1).thenReturn( (short) 2);
Mockito.when( nodeListMock.getLength()).thenReturn( 1);
Mockito.when( nodeListMock.item(Mockito.anyInt())).thenReturn( nodeSpy);
boolean nodeisCreated = moNodeModifier.updateMONode( nodeSpy, nodeListMock );
Assert.assertTrue(nodeisCreated);
Mockito.verify( nodeSpy).appendChild(Mockito.any(Node.class));
}
@BeforeClass
public static void prepareFileBeforeTests() throws IOException
{
file = JUnitUtils.copyFile(
new File( "xx.xml" ), new File( "testfile.xml" ));
}
@AfterClass
public static void deleteFileAfterTests()
{
JUnitUtils.deleteFile( new File(
"testfile.xml" ) );
}
}
and the spying class:
package xxx.testdata;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.TypeInfo;
import org.w3c.dom.UserDataHandler;
public class MockNode implements Node,Element,Attr
{
@Override
public NodeList getChildNodes()
{
return new NodeList(){
@Override
public int getLength()
{
return 1;
}
@Override
public Node item( int index )
{
return new MockNode();
}};
}
@Override
public String getNodeName()
{
return "name";
}
@Override
public short getNodeType()
{
return 1;
}
}
and updateMONode() method:
boolean updateMONode( Node inputMoNode, NodeList targetNodeList )
{
String inputMoDn = Utils.getAttrValue( inputMoNode, "distName" );
for( int i = 0; i < targetNodeList.getLength(); i++ )
{
Node targetMoNode = targetNodeList.item( i );
String targetMoDn = Utils.getAttrValue( targetMoNode, "distName" );
if( (targetMoNode.getNodeType() == Node.ELEMENT_NODE) &&
(inputMoNode.getNodeType() == Node.ELEMENT_NODE) )
{
if( Utils.compareDns( targetMoDn, inputMoDn ) )
{
NodeList parameters = inputMoNode.getChildNodes();
boolean isParameterChanged = false;
boolean isChanged = false;
for( int j = 0; j < parameters.getLength(); j++ )
{
if( (parameters.item( j ).getNodeType() == Node.ELEMENT_NODE) )
isChanged =
updateParamNode(
parameters.item( j ), targetMoNode,
inputMoDn );
if( isChanged )
{
isParameterChanged = isChanged;
isUpdatedParameterNode = false;
}
}
if( isParameterChanged )
{
return true;
}
}
}
}
return false;
}