easymock

EasyMock

浪尽此生 提交于 2019-12-30 05:13:02
  JUnit是Java开发中常用的单元测试工具,对方法的测试很合适,但是一些情况下,JUnit就不是很适用了:    对象结构复杂、难以构造,对象的某些行为很难触发 。这时可以使用Mock来创建对象进行测试,同时还可以Mock一个未实现的接口来进行测试,简单的看就是模拟。   EasyMock是针对Java的Mock工具,通过 EasyMock,我们可以为指定的接口动态的创建 Mock 对象:    使用 EasyMock 生成 Mock 对象 设定 Mock 对象的预期行为和输出 将 Mock 对象切换到 Replay 状态 调用 Mock 对象方法进行单元测试 对 Mock 对象的行为进行验证   下载所需的jar包:easymock-3.2.jar,junit-4.4.jar,objenesis-1.1.jar。      EasyMock-3.1 之前默认只支持为接口生成 Mock 对象,如果需要为类生成 Mock 对象,需要扩展包实现此功能。EasyMock-3.1之后的可为类Mock对象,但是需要objenesis的jar包。   final,private和静态方法,easymock也无法mock其行为,这时可以考虑PowerMock。   假如有一个IStudent接口类和StudentApplication类

EasyMock的使用

廉价感情. 提交于 2019-12-30 05:08:39
1.Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂的对象,从而把测试与测试边界以外的对象隔离开。同时也可以当调用别人的模块,而该模块又没有实现时(只提供接口),我们可以在独立的环境中测试自己的模块逻辑。 2.使用前的准备,下载所需的jar包:easymock-3.0.jar(或以上版本),junit-4.4.jar,cglib-nodep-2.1_3.jar 3.使用方法较简单。主要有以下步骤: *•使用 EasyMock 生成 Mock 对象; *•设定 Mock 对象的预期行为和输出; *•将 Mock 对象切换到 Replay 状态; *•调用 Mock 对象方法进行单元测试; *•对 Mock 对象的行为进行验证。 测试实例:假如我有一个IStudent接口类和StudentApplication类,StudentApplication类中用到了IStudent中的没实现的方法,而我想测试StudentApplication,这时用EasyMock构造一个IStudent的Mock对象,并给要用到的的未实现的方法设定已知返回值。 code: 1 public interface IStudent {2 public String doMethod1();3 public String doMethod2();4 public

我应该如何对线程代码进行单元测试?

大憨熊 提交于 2019-12-25 18:50:03
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 到目前为止,我似乎避免了测试多线程代码的噩梦,因为它似乎太多了。 我想问一下人们如何去测试依赖于线程的代码才能成功执行,或者人们如何去测试那些仅在两个线程以给定方式交互时才会出现的问题? 对于当今的程序员来说,这似乎是一个非常关键的问题,将我们的知识集中在这一恕我直言中将很有用。 #1楼 我曾经承担过测试线程代码的不幸任务,它们绝对是我编写过的最难的测试。 在编写测试时,我结合使用了委托和事件。 基本上,所有有关将 PropertyNotifyChanged 事件与 WaitCallback 或某种轮询的 ConditionalWaiter 使用。 我不确定这是否是最好的方法,但是它对我来说是可行的。 #2楼 确实很强悍! 在我的(C ++)单元测试中,按照使用的并发模式将其分为几类: 对在单个线程中运行且不了解线程的类进行单元测试-轻松进行常规测试。 暴露公开的公共API的 Monitor对象 (在调用者的控制线程中执行同步方法的 对象)的 单元测试-实例化使用该API的多个模拟线程。 构造适用于被动对象内部条件的方案。 包括一个运行时间更长的测试,该测试基本上可以长时间消除来自多个线程的麻烦。 我知道这是不科学的,但确实建立了信心。 Active对象 (封装了自己的一个或多个控制线程的 对象)的 单元测试

How to cover a method calling a static method using JUnit mocking?

自作多情 提交于 2019-12-24 15:03:52
问题 Consider two classes A and B . class A { static int a(){} } class B { void something(){ int value=A.a(); .......}} Now I have to cover class B using Junit Test case and hence I create a new class (class TestB ) to cover the class B . class TestB { @Test public void testsomething(){...} } Here my question is if there is any way I can cover the line A.a() as this is the static method. I know that I can't easy mock it because there is no object involved. So how would I proceed? I am using JUnit

How to test the inner classes by using EasyMock

穿精又带淫゛_ 提交于 2019-12-24 09:51:53
问题 I am new to the EasyMock. I need to test my class using the EasyMock, but here the problem is my class has inner class and this inner class is instatiated in the outer class's method and calling the method of inner class by passing some parameters. I am not sure how to test this class. Below is some sample code. Any help or suggetions are highly appreciated. public class ServiceClass implements ServiceInterface { public void updateUSer(USer) { //some logic over here. sendEmailNotice(subject,

How to mock local variables in java? [duplicate]

给你一囗甜甜゛ 提交于 2019-12-24 02:14:23
问题 This question already has answers here : Mocking methods of local scope objects with Mockito (5 answers) Closed 6 years ago . In this situation? class A { public void f() { B b = new B(); C c = new C(); // use b and c, and how to modify their behaviour? } } How can I fullfill my idea with PowerMock and EasyMock ? I don't want to change my compact code for test reasons. 回答1: You can do this, see the answer by Matt Lachman. That approach isn't recommended however. It's a bit hacky. The best

How to expect void method call with any argument using EasyMock

北城余情 提交于 2019-12-23 09:03:27
问题 As a part of unit test I need to mock a void function(Which accept any non-primitive paramter. e.g. MAP) call with any argument. mockObj.myMethod(<anyObject>) Is it possible to do this with EasyMock? 回答1: Use either of the anyObject methods: anyObject() or anyObject(T) So expect(mockObj.myMethod(anyObject())); See the Flexible Expectations with Argument Matchers section of the documentation 来源: https://stackoverflow.com/questions/17123993/how-to-expect-void-method-call-with-any-argument-using

How should I use EasyMock's @Mock annotation (new in version 3.2)?

ε祈祈猫儿з 提交于 2019-12-23 04:26:58
问题 It looks like EasyMock version 3.2 now supports using annotations to setup mock objects. I am new to EasyMock (and Java in general) and am trying to understand how to use this. Do these annotations do something new or just provide an alternative way to do things? The documentation says: Since EasyMock 3.2, it is now possible to create mocks using annotations. This is a nice and shorter way to create your mocks and inject them to the tested class. Here is the example above, now using

PowerMock - Mocking static system class throws IllegalStateException

百般思念 提交于 2019-12-22 12:26:18
问题 I have the following code public class A{ public void createFile() { File tempXmlFile = null; String extension = ".xml"; String name = "someName"; try { tempXmlFile = File.createTempFile(name, extension); if (tempXmlFile.exists()) { tempXmlFile.delete(); } } catch (IOException e) { System.out.println(e.getStackTrace()); } } } @RunWith(PowerMockRunner.class) @PrepareForTest(A.class) public class testA extends TestCase{ private A classUnderTest; @Override @Before public void setUp() {

JUnit mocking with Mockito, EasyMock, etc

蓝咒 提交于 2019-12-22 08:36:33
问题 I'm trying to mock a method of an object inside the class I'm testing. For instance class ClassToTest { public doSomething () { SomeObject a = new SomeObject (); a.doSomethingElse (); } } Is there a way to mock the methods of the variable "a"? I'd like doSomethingElse to do nothing during testing. I'm currently using Mockito but I'm open to any mocking framework. Thanks 回答1: Yes, there is a way, as shown by the following JMockit test: public void testDoSomething(final SomeObject mock) { new