This ended up being a Java question rather than an EasyMock one, but this is what worked:
expect(core.createInitInstance(isA(QueryParserPlugin.class.getClass()))).andReturn(queryPlugin);
Question
In one of my mocked classes, I need to create an expect
for a method with a signature similar to this:
public <T extends Object> T createInitInstance(Class<T> cast)
What I return will differ based on the cast
argument. It will be another mocked object, of the type specified by cast
. Some of the ways I've tried are:
expect(core.createInitInstance(anyObject(QueryParserPlugin.class))).andReturn(queryPlugin);
and
expect(core.createInitInstance(isA(QueryParserPlugin.class))).andReturn(queryPlugin);
and
expect(core.createInitInstance(isA(Class.class))).andReturn(queryPlugin);
Where queryPlugin
is a mock of the class QueryParserPlugin
. The first two give a compiler error, saying:
required: Class<T>
found: QueryParserPlugin
The third one compiles, but always returns a null, probably because it's not matching the expect, and the mock was created with createNiceMock
.
Any idea how I can correctly specify the parameter?
Solution
This ended up being a Java question rather than an EasyMock one, but this is what worked:
expect(core.createInitInstance(isA(QueryParserPlugin.class.getClass()))).andReturn(queryPlugin);