easymock / easymock/easymock

New <T, R> R createMock(Class<T> toMock) signature fails when used for vararg calls

Open
#259 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
832
Forks
303
Avg merge
5h 25m
Merged PRs (30d)
9

Description

See this code:

```java
import java.util.Arrays;
import org.easymock.EasyMock;

class B { }

public class Mcve {
static void foo(B... someBs) {
System.out.println("someBs = " + Arrays.toString(someBs));
}
public static void main(String[] args) {
foo((B) EasyMock.createMock(B.class));
foo(EasyMock.createMock(B.class));
}
}
```

When I compile and run this with EasyMock 3.4, the output I get is:

```
someBs = [EasyMock for class B]
someBs = [EasyMock for class B]
```

But when I run it with EasyMock 4, the output is:

```
someBs = [EasyMock for class B]
Exception in thread "main" java.lang.ClassCastException: B$$EnhancerByCGLIB$$e255c05a cannot be cast to [LB;
at Mcve.main(Mcve.java:15)
```

In other words: the old ` T createMock(Class toMock)` ensures that the compiler sees *B required*, and thus ensures that the argument is pushed into the required array. With the new signature, things go haywire.

Sure, it can be mitigated by adding that cast, but that feels really wrong.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.