Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jMock Mocking Classes and Interface

I was experimenting jMock as my mocking framework for my project. 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.

Supposing a class Validator and an interface Person to mock. When I was going to mock the Interface Person, I ran to a problem NoClassFoundDefError. When I mocked the class Validator, there was no problem.

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

like image 498
Roy Marco Aruta Avatar asked Jun 09 '09 04:06

Roy Marco Aruta


1 Answers

The code as you present it won't compile (it should be 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 {
    }
}
like image 55
bm212 Avatar answered Oct 07 '22 03:10

bm212