- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 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/
我的内部类如下: public class ClassWithInnerObject { private final InnerObject innerObject; public Class
我在 STM32F4 Discovery 上安装了 STM32F4407VGT6 Controller 。我尝试使用 SPI + DMA 从 AD7683 ADC 读取数据,但 DMA 接收缓冲区始终
假设您有一个只有一个片选的 SPI 总线。 是否有可以将 8 个或更多设备连接到该 SPI 总线的芯片? 为简化起见,您可以假设所有设备都同意 SPI 模式(数据需要在上升沿有效)。此外,所有设备都是
我有一个 32 GB 的金士顿 SDHC microSD 卡,它必须与 MSP430F2618 通信通过 SPI .我无法通过使用 CMD55 + ACMD41(bit30 设置为 1)来初始化它,如
我正在使用 chai-spies 来确保调用 Controller 中的函数,这是我的测试: it('Should show right season and analysts when compet
我刚开始进行 JS 单元测试,但很难理解如何使用 Jasmine spy 创建有意义的测试。 it('should take an array of shopping items', function
我一直在阅读 SPI 以及如何创建内核驱动程序,但我仍然不确定所有这些是如何工作的。 例如: static struct spi_driver ds1305_driver = {
这是我正在测试的代码 eventsApp.factory('userData', ['userResource', function(userResource){ return{ ge
我有一个简单的组件: .html: {{title}} Change title .ts: export class AppComponent { title = 'app works!'
当我从 SPI 总线上的 PIC-18F4520 向我的卡发出地址为 (0x00000000) 的 cmd17 时,我从命令问题中获得了正确的返回 R1 token 。然后,经过几次循环检查后,我从发
我正在使用rspec-spies我想知道在打完所有电话后是否有办法检查 spy 。 例如,如果我做类似的事情 # setup Post.stub(:find).with(@post.id).and_r
我正在使用 rspec-spies并且想知道是否有办法在所有电话都打完后检查 spy 。 例如,如果我做类似的事情 # setup Post.stub(:find).with(@post.id).an
我正在尝试制作一个可以点击另一个程序按钮的程序。我被告知我需要使用 spy++ 来获取我想要点击的按钮的 ID,所以我现在正在使用它。我找到了包含我希望从中获取按钮 ID 的按钮的窗口(窗口中有 3
问题 在我们的代码库中,我们有一个 sinon 问题,可以使用下面的代码片段重现。问题是,它似乎是间接调用的 spy 返回力 false,console.log 明确指出该方法被调用但 spy.cal
考虑以下 react 组件。 import React, { Component } from "react"; import { reduxForm, Field } from "redux-for
我正在开发一个 Linux spi 驱动程序来处理通过 SPI 端口的通信。 我的 SoC 提供了三个 spi 模块(我将其理解为端口),称为 ecspi1/ecspi2/ecspi3。 我需要使用
当我将类的方法包装成这样的 Sinon-spy 时: sinon.spy(myObject, "myMethod") spy 内部会发生什么? 我猜 spy 对象有一个指向“myObject.myMe
我正在为遗留代码编写一些 JUnit 测试,并且我非常喜欢使用注释。我想知道是否可以创建一个 spy 对象的声明,然后实例化它。我问的原因是因为我有一个带有非空构造函数的类。该构造函数的值直到测试用例
我有两个不同的设备要连接 Arduino .一个Ethernet屏蔽和轴编码器。第一个有 SPI模式 0 和第二个 SPI 模式 2。它们冲突。这个问题有解决方案吗? 我使用不同的芯片选择引脚,这两个
我正在运行使用 Yocto (Pyro) 构建的嵌入式 Linux (4.14.16)。我在具有 i.MX6DL 且 SPI 连接到 FPGA(Xilinx Artix 7)的定制板上运行。我目前正在
我是一名优秀的程序员,十分优秀!