124
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 ...
80
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 ...
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 ...
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 ...
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()...
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 ...
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)
{
...
36
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 ...
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 ...
32
votes
Accepted
How to test code that depends on complex APIs (Amazon S3 for instance)?
There are two issues we have to look at here.
The first is that you seem to be looking at all of your tests from the unit test perspective. Unit tests are extremely valuable, but are not the only ...
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 ...
27
votes
Accepted
How do I test a system where the objects are difficult to mock?
It sounds like you're already doing due diligence. But ...
At the most practical level, always include a good handful of both "full-loop" integration tests in your suite for your own code, and write ...
18
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. ...
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 ...
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 ...
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 ...
13
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 ...
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 ...
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 ...
12
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 ...
11
votes
How do I test a system where the objects are difficult to mock?
Short answer: It's hard. You're probably feeling like there are no good answers, and that's because there are no easy answers.
Long answer: Like @ptyx says, you need system tests and integration ...
11
votes
Unit testing an API client and wrappers
TLDR: Despite the difficulty, you should stub the service and use for client unit testing.
I'm not as certain that the "job of a remote API client is to issue certain calls, no more, no less...", ...
11
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 ...
10
votes
Do mocks violate the Open/Closed principle?
The Open/Closed principle is mostly about being able to change the behavior of a class without modifying it. Therefore, injecting a mocked component dependency inside a class under test does not ...
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 ...
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][...
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. ...
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 ...
9
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mocking × 186unit-testing × 129
testing × 48
tdd × 40
java × 23
c# × 18
integration-tests × 14
.net × 12
dependency-injection × 11
stub × 11
design × 8
php × 8
python × 8
c++ × 7
junit × 5
object-oriented × 4
javascript × 4
object-oriented-design × 4
phpunit × 4
architecture × 3
database × 3
functional-programming × 3
refactoring × 3
c × 2
api × 2