EasyMock 3.2: Creating an expect() for a method that takes a generic as an argument

StackOverflow https://stackoverflow.com/questions/22888933

  •  28-06-2023
  •  | 
  •  

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?

Was it helpful?

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top