javamockingjmock

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


Solution

  • 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 {
        }
    }