简体   繁体   English

jMock Mocking类和接口

[英]jMock Mocking Classes and Interface

I was experimenting jMock as my mocking framework for my project. 我正在尝试将jMock作为我项目的模拟框架。 I came into a situation where I need to mock both a class and an interface. 我遇到了需要模拟类和接口的情况。 I used the ClassImposteriser.INSTANCE to initiate the impostor of the context. 我使用ClassImposteriser.INSTANCE来启动上下文的冒名顶替者。

Supposing a class Validator and an interface Person to mock. 假设一个类Validator和一个接口Person来模拟。 When I was going to mock the Interface Person , I ran to a problem NoClassFoundDefError . 当我要模拟Interface Person ,我遇到了一个问题NoClassFoundDefError When I mocked the class Validator , there was no problem. 当我模拟类Validator ,没有问题。

I need both that class and interface but I can't solve the problem. 我需要那个类和接口,但我无法解决问题。 Please HELP. 请帮忙。

Code Example: 代码示例:

Mockery 蔑视

private Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.Class) }};

Class : 课程:

private Validator validator;

Interface : 界面:

private Person person;

Inside Test Method 内部测试方法

validator = context.Mock(Validator.class); ----> Working ---->工作

person = context.Mock(Person.class); ----> NoClassFoundDefError ----> NoClassFoundDefError

The code as you present it won't compile (it should be ClassImposteriser.INSTANCE). 您呈现它的代码将无法编译(它应该是ClassImposteriser.INSTANCE)。 The example code below seems to work fine. 下面的示例代码似乎工作正常。 Perhaps you could provide some more details? 也许您可以提供更多细节?

public class Example {
    private Mockery context = new JUnit4Mockery() {
    {
        setImposteriser(ClassImposteriser.INSTANCE);
    }
    };

    @Test
    public void testStuff() {
    Validator validator = context.mock(Validator.class);
    Person person = context.mock(Person.class);

    // do some stuff...
    }

    public static interface Person {
    }

    public static class Validator {
    }
}

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

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