[英]Mockito spy referenced to wrong object
我對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"
行的可測試類中放置了斷點
我似乎在兩個測試中classSpy
都對間諜對象( classSpy
)使用相同的引用。
在此先感謝您的幫助
ps:我使用mockito 1.9.0和jre 6.0.370.6
ps:我有正常的設置方法:
@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;
}
我認為情況是您尚未將新創建的間諜注入TestableClass
。 您在每個測試中唯一地創建間諜,您是否還將新創建的間諜分配到測試類的“ class
字段中?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.