question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support constructor parameters for spying on abstract classes

See original GitHub issue

Nice, self-contained enhancement that makes Mockito API more robust. The implementation does not require proficiency with Mockito codebase.

Feature

We already support spying on abstract classes by allowing parameterless constructor. However, there is no support for constructor parameters. This has been asked about long time ago. Mockito API is not robust enough if it supports mocking with constructor but not when one has any constructor parameters.

//current api:
Foo spy = mock(Foo.class, withSettings() .useConstructor().defaultAnswer(CALLS_REAL_METHODS));
//existing method (will remain):
MockSettings useConstructor();

//new api (change existing method):
Foo spy = mock(Foo.class, withSettings() .useConstructor("someArg").defaultAnswer(CALLS_REAL_METHODS));
//changed method:
MockSettings useConstructorArgs(Object ... args);

Open questions

  • in case we find multiple matching constructors, do we just use the 1st matching (option 1) or throw an exception (option 2)?

I’d say we go for option 1 because it’s simpler to implement and seems more convenient (works out of the box for certain use cases). If we go for option 2 we need to inform the user what to do to resolve the problem (for example document and suggest @fluentfuture idea of creating an inner implementation)

  • do we add new method or add vararg to existing useConstructor() method?

We decided that using existing method is simpler, keeps the API small and is easy to discover.

Implementation notes

  • the main complexity is to identify and detect the right constructor to use based on types of parameters supplied by the user
  • we already deal with detecting constructors for the @InjectMocks functionality - there should be code to reuse

Test coverage

  • see existing tests that cover “useConstructor” method for
  • ensure decent, informative exception messages
    • if user supplies wrong constructor args (wrong types, we cannot find matching constructor)
    • if the constructor throws some exception (constructors of some types have code that can fail)
    • when one uses existing parameter-less “useConstructor” method but the mocked class requires constructor args, the exception message should tell the user about new “useConstructorArgs” method.
  • what if arguments supplied by the user match more than 1 constructor - either we fail gracefully with decent message or we pick one of the constructors.
  • update documentation to describe new feature. Update documentation for existing parameter-less “useConstructor” method. Update documentation in main Mockito class if it references “useConstructor”.
  • other use cases?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mockitoguycommented, Mar 5, 2017

Thank you very much for contribution. It’s really nice work!

0reactions
fluentfuturecommented, Feb 14, 2017

I happened to need to dig up the history of @Spy AbstractClass in #106. And I found that all my concerns against constructor-args were already stated in that thread. And it was clear that @szczepiq is fine with the trade-off.

So, while I disagree with the design decision, my apologies for repeating myself over again. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spying or mocking abstract classes (Since 1.10.12 ... - CWIKI.US
It is now possible to conveniently spy on abstract classes. ... At the moment, only parameter-less constructor is supported, let us know if ......
Read more >
Testing abstract classes with arguments on constructor
I'm trying to learn about tests but I'm facing some problems whilst testing abstract classes. I know ...
Read more >
Mockito Spying or Mocking Abstract Classes - Javatpoint
In this section, we will discuss mocking or spying of abstract classes. We will analyze several cases of testing the abstract classes using...
Read more >
Spy (Mockito 3.2.4 API) - Javadoc.io
Allows shorthand wrapping of field instances in an spy object. Example: public class Test{ //Instance for spying is created by calling constructor ......
Read more >
Mocking an abstract class with a mocked constructor argument?
Down the road we probably need to figure out some API to create mocks using real constructors, with real parameters. The use case...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found