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.

Convenience factory for stubs

See original GitHub issue

A stub(Class) factory method on the Mockito class would be a convenient way to create stub-only objects:

List stub = stub(List.class);

Such a factory method improves the clarity of test code by succinctly communicating the test author’s intent for the test-double.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fioan89commented, Jun 11, 2020

As a follow up, can the issue be re-opened and have the discussion flow continue?

1reaction
fioan89commented, Jun 11, 2020

Please reconsider this issue. As this blog post from Martin Fowler points out, it’s very important to make a clear distinction between mocks and stubs. Even though at a first glance the difference between them is not that significant - stubs are mocks but you can't verify them - from a testing theory point of view the difference is significantly huge. One enables behavioural testing (i.e mocks) and the other enables state testing. Two different styles of testing with deep implications.

Having a clean stub(...) API will actually encourage people to think more careful about what kind of testing they want to do and what should be actually used.

I’ve seen numerous people, including myself, using the mock(...) API as a stub - no verify assertions done. Believe it or not, most people ignore the MockSettings parameter, or they don’t look deep enough to what that interface provides.

I would like to argue that since mockito cared to provide a convenience factory for spies (Martin Fowler’s blog post considers them as stubs that also record some information based on how they were called) the stub method should also be provided for better readability, encourage correctness code, ease of use and finding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java 9 Convenience Factory Methods for Collections - Baeldung
A quick and practical guide to creating collections and maps using Java 9 factory methods.
Read more >
Static factory methods in Swift - Swift by Sundell
Test stubs. It's not only in our main app code that we have to perform lots of setup, we also often have to...
Read more >
Java Convenience Factory Methods for Collections
Java Convenience Factory Methods for Collections · Allows to create a list, set, and map of values in just one line. Creating immutable...
Read more >
express-test-stubs - npm
A helper factory function that produces test fakes for the need of unit-testing Express endpoints. This module makes use of stampit library -...
Read more >
Reference — Factory Boy stable documentation
The 'stub' strategy is an exception in the factory_boy world: it doesn't return an instance of the model class, and actually doesn't require...
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