Skip to content

Make MockSettings.useConstructor able to choose the right constructor #976

@mockitoguy

Description

@mockitoguy

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions