gpt4 book ai didi

java - Mockito spy 引用了错误的对象

转载 作者:行者123 更新时间:2023-12-01 14:00:52 25 4
gpt4 key购买 nike

我对 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");
}

我有可测试的类(class):

public class TestableClass
{
x = class.getExpectedValue();
//some code
}

问题是:

1 - 我使用 test1()test2() 运行测试类 首先运行 test1() 调试器指示 x = "expected_one" 一切都很好 - 这是预期的行为

2 - test2 正在运行 我在我的可测试类中将断点放在与 x 一致的位置。 和x =“expected_one”

我似乎 Mockito 在两个测试中都使用了对 spy 对象 (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" ) );
}

}

和 spy 类(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;
}


}

和 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;
}

最佳答案

我相信情况是您没有将新创建的 spy 注入(inject)到TestableClass中。您在每个测试中唯一创建 spy ,是否还将新创建的 spy 分配到测试类的 class 字段中?

关于java - Mockito spy 引用了错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com