-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Description
Use case
We recently added a very nice new feature #935 - ability to mock abstract classes / use real constructor for creating mocks. Currently we fail fast when multiple constructors are found matching provided arguments. However, Mockito could potentially do a better job identifying which constructor to use. Example scenario:
class A {}
class B extends A {}
class C extends B {}
class Foo {
Foo(A a) {};
Foo(B b) {};
}
//Below fails with current implementation:
mock(Foo, withSettings().useConstructor(new A()));
mock(Foo, withSettings().useConstructor(new B()));
//However, we could make above work because the constructor argument are not really ambiguous
//Example use case of ambiguous constructor that we can remain failing on
mock(Foo, withSettings().useConstructor(new C()));
Implementation
To get started, see CreatingMocksWithConstructorTest class. Unless the code changes, the interesting test method is this one.