Skip to main content
Share Your Experience: Take the 2024 Developer Survey
126 votes

How do you detect dependency problems with unit tests when you use mock objects?

When you write unit tests for A, you mock X Do you? I don't, unless I absolutely have to. I have to if: X is slow, or X has side effects If neither of these apply, then my unit tests of A will test ...
David Arno's user avatar
  • 39.3k
81 votes
Accepted

How exactly should unit tests be written without mocking extensively?

the point of unit tests is to test units of code in isolation. Martin Fowler on Unit Test Unit testing is often talked about in software development, and is a term that I've been familiar with ...
VoiceOfUnreason's user avatar
79 votes

How do you detect dependency problems with unit tests when you use mock objects?

You need both. Unit tests to verify behavior of each of your units, and a few integration tests to make sure they're connected correctly. The problem with relying only on integration testing is ...
Eternal21's user avatar
  • 1,594
74 votes

How do you detect dependency problems with unit tests when you use mock objects?

When you write unit tests for A, you mock X. In other words, while unit testing A, you set (postulate) the behaviour of X's mock to be X1. Time goes by, people do use your system, needs change, X ...
VoiceOfUnreason's user avatar
66 votes
Accepted

Mocking introduces handling in production code

Instead of initializing the reader from your method, move this line { this.reader = new ReaderImplementation(this.location) } Into the default parameterless constructor. public ReaderConsumer()...
RubberDuck's user avatar
  • 8,961
66 votes

How exactly should unit tests be written without mocking extensively?

How exactly should unit tests be written without mocking extensively? By minimising side-effects in your code. Taking your example code, if calculator for example talks to a web API, then either you ...
David Arno's user avatar
  • 39.3k
54 votes

Mocking introduces handling in production code

You only need the single constructor: public class ReaderConsumer() { private IReader reader = null public string location // constructor public ReaderConsumer(IReader reader) { ...
Ewan's user avatar
  • 76.4k
40 votes
Accepted

Does integration testing use mocks?

If you have a piece of functionality that touches several external components, you might mock all but one to isolate and test a specific component. For example, suppose you have a function that calls ...
FrustratedWithFormsDesigner's user avatar
35 votes

How exactly should unit tests be written without mocking extensively?

These questions are quite different in their difficulty. Let's take question 2 first. Unit tests and integration tests are clearly separated. A unit test tests one unit (method or class) and uses ...
Kilian Foth's user avatar
28 votes
Accepted

How to avoid the need to Unit test private methods

You're partially right - you shouldn't directly test private methods. The private methods on a class should be invoked by one or more of the public methods (perhaps indirectly - a private method ...
Thomas Owens's user avatar
  • 82.8k
19 votes

Testing - In-Memory DB vs Mocking

Mocking is the ideal solution for unit tests, and it may also be used for integration tests to improve speed, but it doesn't provide the same level of confidence as when you use an in-memory database. ...
Samuel's user avatar
  • 9,197
17 votes

How do you detect dependency problems with unit tests when you use mock objects?

Let me start by saying that the core premise of the question is flawed. You are never testing (or mocking) implementations, you are testing (and mocking) interfaces. If I have a real class X that ...
Vlad274's user avatar
  • 295
15 votes

Is it really a bad practice to mock a POJO (value object) if you don't care about what it contains?

Stubs (that you've called mocks in your question) exist for a reason: they make it possible to replace business code from the classes you depend on in the method you're testing by some very basic ...
Arseni Mourzenko's user avatar
14 votes
Accepted

Unit testing private calls, or not

Unit Tests verify the public observable behavior of a unit of code. Distribution of this behavior to a bunch of private methods is implementation detail which you do not test. The reason is that you ...
Timothy Truckle's user avatar
13 votes

Mocking introduces handling in production code

Look into Dependency Injection and Inversion of Control Both Ewan and RubberDuck have excellent answers. But I wanted to mention another area to look into which is Dependency Injection (DI) and ...
Reginald Blue's user avatar
13 votes

Should I Have One Interface Per Class For Unit Testing?

The underlying problem is the need for mocking in unit testing. Mocking is a code smell - it is unavoidable in some cases, but should not be pervasive in your unit tests. Mocking is necessary for ...
JacquesB's user avatar
  • 59.7k
12 votes
Accepted

Should I have code that only exists for testing?

It's a matter of perspective. Writing tests for our code forces us to decouple the code. You COULD view this as "code that exists only for the tests", but I think this is misleading. What you're ...
sara's user avatar
  • 2,559
12 votes
Accepted

Should I use virtual methods to make mocking possible?

How would virtual methods help? The idea of mocking is that you rip out a class completely from your application and plug in a completely different mocked class, with the only thing in common that ...
gnasher729's user avatar
  • 45.7k
12 votes
Accepted

Are in-memory database a form of integration tests?

IMHO you are asking the wrong question. It does not matter if what you create is called a unit test by some people, or an integration test by others. What matters is here, is this test useful for ...
Doc Brown's user avatar
  • 209k
10 votes

Writing unit test cases are taking time, any advice?

Writing test cases generally take a long time if you don't use test-driven development, because of several factors which are explained very well in Working Effectively with Legacy Code. In your ...
l0b0's user avatar
  • 11.5k
10 votes

Does integration testing use mocks?

It is not immediately clear what is meant by Integration Test but the usage of Fakes/Mocks/Test Doubles is a valid technique to create test scenarios on all levels within the pyramide of tests.[1][2][...
jschnasse's user avatar
  • 201
10 votes

Should I use virtual methods to make mocking possible?

I favour making methods virtual over extracting an interface. The performance hit is negligible. In Java, where methods are virtual by default, people don't worry about this nearly as much. I've seen ...
mcintyre321's user avatar
10 votes
Accepted

How far should I go with dependency injection and mocking?

Why would a user need to provide me a class from this library through a constructor? It's something that's a mere side tool used to accomplish the use case. IMHO this is not the really important ...
Doc Brown's user avatar
  • 209k
10 votes

How exactly should unit tests be written without mocking extensively?

Mocking should only be used as a last resort, even in unit tests. A method is not a unit, and even a class is not a unit. A unit is any logical separation of code that makes sense, regardless of what ...
TiggerToo's user avatar
  • 208
10 votes

What is recommended way to create test data for unit test cases?

What is recommended way to create test data for unit test cases? Use a whiteboard To compute this schedule, I need to interact with 25-30 entities and each entity, on average, have 10 fields. No. ...
candied_orange's user avatar
9 votes
Accepted

How does functional style helps with mocking dependencies?

A pure function is one that: Will always give the same result given the same arguments Doesn't have any observable side effects (e.g. state changes) Suppose we are writing some code to handle user ...
Justin's user avatar
  • 1,738
9 votes

How do you detect dependency problems with unit tests when you use mock objects?

Taking your questions in turn: what value does unit testing have then They're cheap to write and run and you get early feedback. If you break X, you'll find out more or less immediately if you ...
Robbie Dee's user avatar
  • 9,805
9 votes

How do you detect dependency problems with unit tests when you use mock objects?

That's correct. Unit tests are there to test the isolated functionality of a unit, a first-glance check that it works as intended and doesn't contain stupid errors. Unit tests are not there to test ...
gbjbaanb's user avatar
  • 48.6k
9 votes

How exactly should unit tests be written without mocking extensively?

OK, so to answer your questions directly: How should unit tests be written properly? As you say, you should be mocking dependencies and testing just the unit in question. Where exactly does the ...
Ewan's user avatar
  • 76.4k
9 votes
Accepted

How do I write unit tests for legacy code (that I don't understand)?

To a first approximation, the stakeholders of a test suite are the code developers/maintainers. You're going to need some of their time. Insist on this. Ask them about problems they're facing. Ask ...
ShapeOfMatter's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible