- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 EasyMock 为许多协作类编写单元测试。其中一个类(我们称之为 Foo
)打开一个到远程服务器的网络连接,并将服务器的 XML 响应解析为其他类可以使用的东西。
目前,我的测试仅包含以下场景:一切都很正常,远程服务器已启动并运行并按预期返回 XML。但是,如果我可以模拟 Foo
以便模拟远程服务器关闭或其他导致 的问题时会发生什么,我会更高兴
。Foo
抛出的 IOException
我查看了 EasyMock API,但看不到任何看起来像要求模拟抛出异常的方法的东西。
对我来说,基于 Exception
的测试并不是绝对必要的,但我很好奇 EasyMock 是否可能,我认为测试 Foo
' 会很有用s 以这种方式公开契约(Contract)。
以前有人用 EasyMock 做过类似的事情吗?
引用文献
最佳答案
来自 documentation :
For specifying exceptions (more exactly:
Throwable
s) to be thrown, the object returned byexpectLastCall()
andexpect(T value)
provides the methodandThrow(Throwable throwable)
. The method has to be called in record state after the call to the Mock Object for which it specifies theThrowable
to be thrown.Unchecked exceptions (that is,
RuntimeException
,Error
and all their subclasses) can be thrown from every method. Checked exceptions can only be thrown from the methods that do actually throw them.
例如:
expectLastCall().andThrow(new HibernateException("Something terrible happened"));
expect(query.list()).andThrow(
new HibernateException("Something terrible happened"));
关于java - 让 EasyMock 模拟对象抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5027533/
我想更好地理解 EasyMock,并使用它我想出了这个问题。 我想做的是对一个对象设置一个负期望,以检查在测试期间是否没有调用某个方法(在验证那些初始期望时)。 我知道 verify 的默认行为是检查
有没有一种简单的方法来模拟这个调用: objectA.getB().getC(); 现在我这样做的方式是: A mockA = EasyMock.createMock(A.class); B mock
我试图模拟对 final方法的调用 ResourceBundle.getString() .使用 PowerMock 1.4.12 和 EasyMock 3.1,调用不会被模拟;而是调用“真实”方法。
我正在使用 EasyMock 和 EasyMock CE 3.0 来模拟依赖层并测试我的类。以下是我无法找到任何解决方案的场景 我有一个要测试的类,它调用一个依赖类void 方法,该方法接受输入参数,
我有一个间接使用类 Foo 的测试用例。为了测试用例的目的,我不在乎 Foo 是什么。我应该能够 mock 它。 Foo mock = EasyMock.createMock(Foo.class);
我已经使用 EasyMock 编写了很多 Mock 对象。然而,我经常发现编写部分模拟非常耗时,而且感觉不“正确”。 我会说这是一个设计错误,因为我尝试模拟的类将多个关注点合二为一,因此我应该创建单独
在我的单元测试中,我使用 EasyMock 创建模拟对象。在我的测试代码中我有这样的东西 EasyMock.expect(mockObject.someMethod(anyObject())).and
所以我已经使用 EasyMock 的类扩展有一段时间了。突然间,我遇到了这个异常,但只有当我运行整个测试套件时: java.lang.IllegalStateException: 0 matchers
这有什么区别: ResultSet set = EasyMock.createNiceMock(ResultSet.class); EasyMock.expect(set.getInt("col1")
我正在尝试完成有关 easymock 的教程以首次使用它。 http://www.vogella.de/articles/EasyMock/article.html 我正在使用- 面向 Java 开发
我正在使用 TestNG、EasyMock 和 PowerMock 进行测试。根据下面的代码,我尝试模拟从被测静态方法(fetchAuthenticator)调用的静态方法。当我运行测试时,execu
我被分配了将我们的模拟代码从 EasyMock 1-style 升级到 EasyMock 2/3-style 的任务(现在是时候了,因为 EasyMock 1 的大部分内容已于 2005 年被弃用并于
在 API 文档中提到在 strictmock 中默认启用顺序检查,而在 nice mock 的情况下它不是。我不明白他们所说的“订单检查”到底是什么意思。 最佳答案 如果您告诉 mock 期望调用
根据标题,只是想知道是否有一种带有 easymock 的机制来测试方法在其生命周期内是否未被调用。 最佳答案 我知道这个问题很老,但我和 OP 有同样的问题,并做了更多的环顾四周。我找到了以下解决方案
我正在尝试在带有 EasyMock 的 JUnit 中设置测试,但遇到了一个我似乎无法解决的小问题。我希望这里有人可以提供帮助。 这是我尝试测试的方法的简化版本: public void myMeth
这是我第一次使用 EasyMock,我正在尝试向一些遗留代码添加一些单元测试。 遗留代码位于 Spring 3.1 中,我使用的是 EasyMock 3.4。 我在这里想要完成的是测试调用 dao 的
是否可以为循环调用的模拟设置不同的期望? 考虑到用例, while(condition){ List list = Database call //Call to be mocked ..
我正在使用 EasyMock 尝试通过两层深度的项目列表来模拟迭代。我的单元测试代码如下 EventContext ctx = EasyMock.createMock(EventContext
我在使用 EasyMock 时遇到一些奇怪的行为。我已经设置了一些期望,但是当我运行测试时,EasyMock 失败了,因为我的期望按指定执行。以下是失败的示例: Unexpected method c
例如,我创建一个对象,并想为其设置一个属性。问题是,该属性永远不会被设置,因为 EasyMock 会拦截它。如何解决这个问题? ctx = control.createMock(EGContext.c
我是一名优秀的程序员,十分优秀!