我对Mockito模拟库有问题
我的Junit4测试课程有2个测试套件。
测试一个:
@Test
public void test1()
{
Class class = new Class();
Class classSpy = Mockito.spy(class);
Mockito.when( classSpy.getExpectedValue()).thenReturn("expected_one");
}
和第二次测试:
@Test
public void test2()
{
Class class = new Class();
Class classSpy = Mockito.spy(class);
Mockito.when( classSpy.getExpectedValue()).thenReturn("expected_two");
}
我有可测试的课程:
public class TestableClass
{
x = class.getExpectedValue();
//some code
}
问题是:
1 - 我使用test1()
和test2()
运行测试课程
一开始正在运行test1()
调试器指示x = "expected_one"
一切都好 - 这是预期的行为
2 - test2正在运行
我把断点放在我的可测试类中,与x一致。
和x = "expected_one"
我似乎Mockito在两个测试中都使用了与间谍对象(classSpy
)相同的引用。!!
提前感谢您的帮助
ps:我使用mockito 1.9.0和jre 6.0.370.6
ps:我有正常的SetUp方法:
@Before
public void setUp(){
testableClass = new TestableClass();
}
ps3:完整的测试套件:
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" ) );
}
}
和间谍课:
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;
}
}
和updateMONode()方法:
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;
}
答案 0 :(得分:1)
我认为情况是你没有将新创建的间谍注入TestableClass
。您在每个测试中唯一地创建间谍,您是否还将新创建的间谍分配到测试类的class
字段中?