EasyMock mock new instance of a class with arguments

116 Views Asked by At

So I'm trying to get rid of PowerMock as I'm updating to java 17 by using EasyMock.

But I hit a wall trying to convert this :

PowerMock.expectNew(File.class, "path").andReturn(fileMock).times(1);

Into the Easymock alternative. I wrote smth like this.

EasyMock.expect(new File("path")).andReturn(fileMock).times(1);

But it doesn't seem to work, and I keep hitting java.lang.IllegalStateException: no last call on a mock available.

Any ideas would be welcome. Thanks

1

There are 1 best solutions below

0
Henri On

You can't mock new with EasyMock. It's not supported. But I will argue that you do not need to mock File. You can just create and actual file.

If needed, extract to a method the piece of code creating the File, create a partial mock and make that method returning a File that suits your purpose.