Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockito generates mock with incorrect subtyping if trait extends abstract class #229

Open
YulawOne opened this issue Apr 28, 2020 · 4 comments

Comments

@YulawOne
Copy link

Java: 11
Scala: 2.13.1
Mockito: 3.3.3
Mockito Scala: 1.13.10

abstract class Foo
trait Bar extends Foo

val bar = mock[Bar]
val foo: Foo = bar // or bar.asInstanceOf[Foo]

fails with:

class Bar$MockitoMock$419587131 cannot be cast to class Foo (Bar$MockitoMock$419587131 and Foo are in unnamed module of loader 'app')
java.lang.ClassCastException: class Bar$MockitoMock$419587131 cannot be cast to class Foo (Bar$MockitoMock$419587131 and Foo are in unnamed module of loader 'app')
@ultrasecreth
Copy link
Member

@YulawOne hmm, it looks like a bug on the Scala compiler, I'll check with them and see if there is a solution for this

@ultrasecreth
Copy link
Member

As the issue in the scala compiler says, it's a limitation on the Java type system, which is what Mockito uses under the hood, I'll try to see if there is a way to overcome this limitation, but I'm not really sure it will be possible in the short term

@YulawOne
Copy link
Author

I also though it something specific to way how traits are compiled. I have found workaround in my codebase, but will be great if this issue could be solved in future

@ultrasecreth
Copy link
Member

I can't promise anything as the mock generator uses Java reflection which is completely oblivious of this particular case, as it's illegal in the Java type system.
I'll keep trying ideas, but most probably unless I re-write the core of mockito again in Scala, I don't think it will be feasible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants