Question

I am attempting to mock the method below with JMock and I am running into a compiler error.

Class to Mock:

public interface myClass<T extends SomeClass>{
public void myMethod(T parameter);
public void myOtherMethod();
}

Mock Expectation:

exactly(1).of(myClassMocked).myOtherMethod();
exactly(1).of(myClassMocked).myMethod( with(any(SomeClass.class)) );

Compile Error:

The method myMethod(capture#6-of ?) in the type myClass is not applicable for the arguments (SomeClass)

I have attempted switching Object.class with other compatible classes and playing around with the matcher.

I am unable to simply change the method signature, that would be too easy.

The long and short of it is I want to ignore this particular method call but I cannot ignore the entire MyClass mock because it is a service I am using for other tasks in my test method.

EDIT: The fix is to change final myClass<?> myClassMocked= mockery.mock(myClass.class); (generic specification suggested by eclipse) to final myClass<SomeClass> myClassMocked= mockery.mock(myClass.class);

Was it helpful?

Solution

Parametrizing the interface with T means that the type has to be defined at compilation time. The problem is in the way you obtain the myClassMocked, that's why it's expecting the "any" wildcard ?.

You should be mocking a subclass of the myClass interface parametrized with a type that extends SomeClass for example:

public class myClassImpl implements myClass<SomeSubclassOfSomeClass>{
    public void myMethod(SomeSubclassOfSomeClass parameter){...}
    public void myOtherMethod(){...}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top