Skip to main content

Questions tagged [mocking]

Mocking and faking are ways to isolate code or components to ensure that unit tests run against the testable unit of code only without actually utilizing other components or dependencies of an application. Mocking differs from faking in that a mock can be inspected to assert the results of a test.

Filter by
Sorted by
Tagged with
133 votes
13 answers
44k views

(Why) is it important that a unit test not test dependencies?

I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on ...
dsimcha's user avatar
  • 17.2k
121 votes
8 answers
50k views

How exactly should unit tests be written without mocking extensively?

As I understand, the point of unit tests is to test units of code in isolation. This means, that: They should not break by any unrelated code change elsewhere in the codebase. Only one unit test ...
Alex Lomia's user avatar
  • 1,311
101 votes
11 answers
18k views

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

You have a class X and you write some unit tests that verify behaviour X1. There's also class A which takes X as a dependency. When you write unit tests for A, you mock X. In other words, while unit ...
bvgheluwe's user avatar
  • 1,187
93 votes
11 answers
86k views

Is static universally "evil" for unit testing and if so why does Resharper recommend it? [closed]

I have found that there are only 3 ways to unit test (mock/stub) dependencies that are static in C#.NET: Moles TypeMock JustMock Given that two of these are not free and one has not hit release 1.0, ...
Vaccano's user avatar
  • 4,048
86 votes
3 answers
157k views

Is this an appropriate use of Mockito's reset method?

I have a private method in my test class that constructs a commonly used Bar object. The Bar constructor calls someMethod() method in my mocked object: private @Mock Foo mockedObject; // My mocked ...
Duncan Jones's user avatar
  • 1,402
40 votes
2 answers
31k views

Does integration testing use mocks?

I am currently in a class for software testing where for our semester project, we have to perform multiple types of testing on it, such as unit testing and integration testing. For integration ...
TheLegendOfCode's user avatar
32 votes
6 answers
6k views

How do I test a system where the objects are difficult to mock?

I am working with the following system: Network Data Feed -> Third Party Nio Library -> My Objects via adapter pattern We recently had an issue where I updated the version of the library I was ...
durron597's user avatar
  • 7,610
23 votes
7 answers
43k views

How to avoid the need to Unit test private methods

I know you're not supposed to test private methods, and if it looks like you need to, there might be a class in there waiting to come out. But, I don't want to have a gazillion classes just so that I ...
Fran Sevillano's user avatar
22 votes
3 answers
12k views

Is it okay to fake part of the class under test?

Suppose I have a class (forgive the contrived example and the bad design of it): class MyProfit { public decimal GetNewYorkRevenue(); public decimal GetNewYorkExpenses(); public decimal ...
User's user avatar
  • 1,551
22 votes
3 answers
4k views

Brittle unit tests due to need for excessive mocking

I've been struggling with an increasingly annoying problem regarding our unit tests that we are implementing in my team. We are attempting to add unit tests into legacy code that wasn't well designed ...
PremiumTier's user avatar
19 votes
2 answers
18k views

Testing - In-Memory DB vs Mocking

When writing tests, why would someone want to use an in-memory database over just mocking the data? I could see that in-memory databases could be beneficial for testing out one's repositories. But ...
user avatar
16 votes
6 answers
2k views

From a TDD perspective, am I a bad person if I test against a live endpoint instead of a mock?

I follow TDD religiously. My projects typically have 85% or better test coverage, with meaningful test cases. I do a lot of work with HBase, and the main client interface, HTable, is a real pain ...
sangfroid's user avatar
  • 3,239
16 votes
6 answers
9k views

Should I Have One Interface Per Class For Unit Testing?

Should I define an interface for every public behavior class (excluding data classes)? I've spent many hours searching and reading to find a clear answer. If I search "Do you define an interface ...
Etienne Charland's user avatar
16 votes
1 answer
6k views

Unit testing an API client and wrappers

I've been going round in circles trying to figure out the best way to unit test an API client library I'm developing. The library has a Client class which basically has a 1:1 mapping with the API, and ...
Joseph Mansfield's user avatar
15 votes
3 answers
7k views

Mocking introduces handling in production code

Assuming an IReader interface, an implementation of the IReader interface ReaderImplementation, and a class ReaderConsumer that consumes and processes data from the reader. public interface IReader { ...
kristian mo's user avatar
15 votes
4 answers
18k views

How to test code that depends on complex APIs (Amazon S3 for instance)?

I am struggling with testing a method that uploads documents to Amazon S3, but I think this question applies to any non-trivial API/external dependecy. I've only come up with three potential solutions ...
springloaded's user avatar
  • 2,123
15 votes
5 answers
2k views

How are mock objects commonly misused?

I read an article recently which said that mock objects are often misunderstood and misused. Are there any clear mocking anti-patterns which I can look out for?
Armand's user avatar
  • 6,528
15 votes
3 answers
5k views

What are the key factors in choosing a Mocking Framework?

I'm looking to get started with objects in my unit tests. It seems there are tons of good mocking frameworks out there. Do the different frameworks have different target audiences? What factors ...
epotter's user avatar
  • 2,846
14 votes
4 answers
2k views

Do mocks violate the Open/Closed principle?

Some time ago I read, on a Stack Overflow answer that I can't find, a sentence that explained that you should test public APIs, and the author said that you should test interfaces. The author also ...
Christopher Francisco's user avatar
14 votes
4 answers
20k views

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

In the advice of Mockito about how to write good tests, it is written that we should not mock value objects (https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-value-objects). ...
Ricola's user avatar
  • 279
14 votes
4 answers
2k views

When should I use mock objects?

I've read a lot of things about TDD but I still have doubts. For example, I have these class diagrams: It's a simple example, just to learn about TDD and mock objects. Which test should I write ...
user avatar
12 votes
5 answers
8k views

How far should I go with dependency injection and mocking?

Say I have a following class: public class A { public void execute() { if (something) ThirdPartyApi.method(); } } Now, I would like to test in particular the execute() method. I ...
aarnaut's user avatar
  • 259
11 votes
3 answers
7k views

Mocking concrete class - Not recommended

I've just read an excerpt of "Growing Object-Oriented Software" book which explains some reasons why mocking concrete class is not recommended. Here some sample code of a unit-test for the ...
Mik378's user avatar
  • 3,888
11 votes
1 answer
1k views

How much mocking is "just right?"

I titled the question jokingly because I'm sure that "it depends," but I have some specific questions. Working in software that has many deep layers of dependency, my team has become accustomed using ...
ardave's user avatar
  • 252
10 votes
6 answers
1k views

I'm confused about which is the correct way to work with TDD

I'm trying to grasp what's the idea behind TDD and how a team is supposed to work with it. I have the following test case with NUnit + Moq (just writing by memory, it is not assured the example ...
David Jiménez Martínez's user avatar
10 votes
5 answers
16k views

Should I use virtual methods to make mocking possible?

So far I have used to write interfaces to those classes I want to mock/fake. Reason for that is that those classes don't have any virtual method to overwrite. But resently I have figured out that I ...
Lassi Autio's user avatar
10 votes
5 answers
3k views

TDD: Mocking out tightly coupled objects

Sometimes objects just need to be tightly coupled. For example, a CsvFile class will probably need to work tightly with the CsvRecord class (or ICsvRecord interface). However from what I learned in ...
Phil's user avatar
  • 3,660
10 votes
3 answers
22k views

How to mock method with hard coded object?

I am working on an application which have multiple layers. Data access layer to retrieve and save data from data source, business logic to manipulate data, user interface to show the data on screen. ...
Arnab's user avatar
  • 377
10 votes
1 answer
2k views

How does functional style helps with mocking dependencies?

From the interview with Kent Beck in a recent Java Magazine issue: Binstock: Let’s discuss microservices. It seems to me that test-first on microservices would become complicated in the sense that ...
Dan's user avatar
  • 221
10 votes
2 answers
420 views

How would type errors be detected while creating mocks in a dynamic language?

The problem occurs while doing TDD. After a couple of test pass, the return types of some class/module change. In a statically typed programming language, if a previous mocked object was used in the ...
jvliwanag's user avatar
  • 269
9 votes
6 answers
9k views

Are HSQLDB unit tests an anti pattern?

HSQLDB is great. It (also) has an embedded mode (no dedicated server needed), which allows for quick prototyping of stuff like Proof of Concepts, and it can also be great in production-ready ...
Shivan Dragon's user avatar
9 votes
4 answers
554 views

How would a new language look if it was designed from scratch to be easy to TDD?

With the some most common languages (Java, C#, Java, etc) it sometimes seems that you are working at odds with the language when you want to fully TDD your code. For example, in Java and C# you will ...
Geoff's user avatar
  • 207
9 votes
3 answers
1k views

Faking - Dependency vs Internal Details

Just for the sake of discussion below, when I refer to mocking, I mean mocking/stubbing/faking, as contrast to use real implementation in testing I just had some discussion with my colleague during a ...
Phuong Nguyen's user avatar
8 votes
4 answers
14k views

Unit test a class that sequentially calls other classes

Hi i have a class like this class MyClass { private ExternalClass1 ex1; private ExternalClass2 ex2; private ExternalClass3 ex3 public String doSomething(String arg1){ val1=ex1.invoke(arg1); ...
Belin's user avatar
  • 145
8 votes
3 answers
3k views

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

Forward I've read a lot of things before asking this question, including many relevant questions right here on SE: (Software Engineering SE) Writing tests for code whose purpose I don't understand (...
user avatar
8 votes
3 answers
5k views

What should I mock in tests of an application with service tier and DAO tier?

My classes are following this structure Service Tier (creates and maps InputDTO to DB Data) DAO Tier (actually executes DB calls) When I write service tier JUnit tests, the DAO tier is called, and ...
shinynewbike's user avatar
8 votes
2 answers
667 views

Who should initialize dependencies in a TDD application?

I'm trying to learn implementing TDD with mocking/fake objects. One of the questions I have is how to initialize a dependency in an application which implements TDD? An example from this article ...
Louis Rhys's user avatar
  • 6,142
8 votes
2 answers
7k views

Is it possible to mock and inject traits in PHPUnit?

I need to extend a third party class I cannot modify. The class's dependencies are for the most part injected through the constructor, making them easy to mock. However the class also uses methods ...
TravisCarden's user avatar
7 votes
7 answers
3k views

Is mocking for unit testing appropriate in this scenario?

I have written around 20 methods in Java and all of them call some web services. None of these web services are available yet. To carry on with the server side coding, I hard-coded the results that ...
Vinoth Kumar C M's user avatar
7 votes
2 answers
11k views

Testing C# Eventhandler Subscription

I want my unit tests to be able to verify that some events were subscribed or unsubscribed at specific points. Currently, I see two options. VerifiableEventHandler Each client subscribes using the ...
Wilbert's user avatar
  • 1,703
7 votes
4 answers
2k views

Do large test methods indicate a code smell?

I have a particular method called TranslateValues() (Cyclomatic-Complexity of 5) which I would like to test. The test requires a substantial number of mock objects which take up most of the method; ...
Ray's user avatar
  • 266
7 votes
3 answers
881 views

Is it good practice to register development mocks using conditional compilation?

I'm developing an application that connects to a database, and I'm using DI/IOC. When running the entire application while developing, I naturally want to avoid hitting the DB. Currently, I have ...
cmeeren's user avatar
  • 329
7 votes
4 answers
19k views

How to use mock objects [C++] without passing them as arguments to functions

I'm in the process of integrating a Unit Testing Framework for an existing code base in C++. I've zeroed down on CxxTest, but as it turns out we can use other Mocking Frameworks (like googlemock) in ...
dotbugfix's user avatar
  • 382
7 votes
3 answers
652 views

Unit Testing: Should we care about distinguishing between Mocks and Stubs?

There's been a lot of talk in various blogs, forums and on StackExchange about the distinction between Mock and Stub objects (most of them specifically referencing the Rhino Mocks framework). These ...
mjhilton's user avatar
  • 653
7 votes
1 answer
1k views

Is mocking microservices a thing?

I'm trying to get my head around a the development workflow for working with microservices and docker multicontainer applications. The thing that I'm particularly trying to solve - is getting a good ...
dwjohnston's user avatar
  • 2,563
6 votes
3 answers
7k views

How to unit test code that downloads a file from a github repo

This is my solution's structure: Storage.csproj > FileDownloader.cs > GitHubProvider.cs (implements IStorageProvider) > IStorageProvider.cs Storage.Test.csproj > ...
Maria Ines Parnisari's user avatar
6 votes
5 answers
1k views

Are mocks in unit tests dangerous in dynamic languages?

I've started relying heavily on a mocking framework in php for my unit tests. My concern is that with a dynamic language, there is no way of enforcing a return type. When mocking, you have to ensure ...
GWed's user avatar
  • 3,135
6 votes
1 answer
1k views

Is contructing complex mocks for unit testing a sign of bad architecture?

The title is fairly self explanatory. To elaborate: I'm currently updating a suite of unit tests for an application which makes heavy use of Entity Framework. We connect to this via a classic ...
Bob Tway's user avatar
  • 3,636
6 votes
3 answers
3k views

Implementing common logic in base class

Background In the documentation of a project I'm working on I came across the following sentence which immediately triggered an alarm for me: when having several concrete classes that inherits ...
Omribitan's user avatar
  • 171
6 votes
2 answers
2k views

How well am I writing my tests?

I've been coding as a career for about 2 years now but am just now writing my first "real" tests for a non-trivial application. I've been trying to follow best practices I've picked up from the ...
Matt Foxx Duncan's user avatar