简体   繁体   English

EasyMock中的模拟抽象类

[英]Mock Abstract Class in EasyMock

I am using EasyMock 2.4 and can't upgrade to latest version due to dependencies. 我正在使用EasyMock 2.4,由于依赖关系,无法升级到最新版本。 I need to mock abstract class but not able to do it with createMock method. 我需要模拟抽象类,但不能使用createMock方法来实现。 It throws an error that class is not an interface. 抛出一个错误,该类不是接口。

Can anyone help me in solving this problem? 谁能帮助我解决这个问题?

There is an abstract class called ClassA (I can't modify this class): 有一个称为ClassA的抽象类(我无法修改此类):

public abstract class ClassA {

}

There is another MyTest class which mocks ClassA: 还有一个模拟类A的MyTest类:

public class MyTest {
    private ClassA mockClassA;

    @Before
    public void setup() {
        mockClassA = createMock(ClassA.class); //Line number: 28
    }
}

while running this it throws below exception at createMock call: 运行此命令时,它在createMock调用时引发以下异常:

java.lang.IllegalArgumentException: ClassA is not an interface

at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:590)
at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:557)
at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:230)
at java.lang.reflect.WeakCache.get(WeakCache.java:127)
at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:419)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:719)
at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:13)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:40)
at org.easymock.EasyMock.createMock(EasyMock.java:60)
at mypackage.MyTest.setup(MyTest.java:28)

EasyMock prior to v3.0 was using Java proxies mechanism to create mocks. v3.0之前的EasyMock使用Java代理机制创建模拟。 This mechanism is capable only of creating proxies for interfaces, so there is no way you can mock a class (abstract class) with easy mock without upgrading to v3.0 at least. 该机制仅能为接口创建代理,因此,至少在不升级到v3.0的情况下,无法通过简单的模拟来模拟类(抽象类)。

You have the following options: 您有以下选择:

  1. Upgrade EasyMock to v3.0+ (what prevents your from?) 将EasyMock升级到v3.0 +(是什么阻止了您?)
  2. Use other mocking library in parallel with EasyMock (eg Mockito) 与EasyMock并行使用其他模拟库(例如Mockito)
  3. Create your own subclass of ClassA in test and override methods there for testing. 在测试中创建您自己的ClassA子类,并覆盖那里的方法进行测试。 But this one is clearly a workaround that may not provide you with enough flexibility. 但这显然是一种解决方法,可能无法为您提供足够的灵活性。

Actually what do you expect from your mock? 实际上,您对模拟游戏有什么期望? (Eg to stub some method calls, or to do some method call verifications, other...) (例如,存根某些方法调用,或进行某些方法调用验证,其他...)

EasyMock Class extension worked to create mock object for class or interface. EasyMock Class扩展用于为类或接口创建模拟对象。 I used import static org.easymock.classextension.EasyMock.* ; 我使用import static org.easymock.classextension.EasyMock。* ; instead of import static org.easymock.EasyMock.* ; 而不是导入静态org.easymock.EasyMock。* ;

Yes. 是。 Prior to EasyMock 3, you need the class extension to mock classes. 在EasyMock 3之前,您需要类扩展才能模拟类。 However, latest EasyMock versions have not much dependencies (only Objenesis in fact). 但是,最新的EasyMock版本没有太多依赖性(实际上只有Objenesis)。

Which one is blocking you? 哪一个阻止了您?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM