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.

learn more… | top users | synonyms (1)

41
votes
10answers
5k 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 ...
39
votes
8answers
15k views

Is static universally “evil” for unit testing and if so why does resharper recommend it?

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 ...
15
votes
6answers
765 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 ...
15
votes
5answers
836 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?
13
votes
3answers
1k 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 ...
13
votes
3answers
895 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 ...
11
votes
3answers
3k 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 ...
10
votes
4answers
854 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 ...
10
votes
2answers
232 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 ...
8
votes
3answers
895 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 ...
8
votes
2answers
5k 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 ...
7
votes
5answers
614 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 ...
7
votes
4answers
363 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 ...
7
votes
1answer
278 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 ...
7
votes
3answers
368 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 ...
6
votes
3answers
836 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. ...
5
votes
1answer
227 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 ...
4
votes
6answers
510 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 ...
4
votes
7answers
826 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 ...
4
votes
1answer
179 views

What is a “behavior rich object” and why would it be advantageous?

I am referring to the article Mocks aren't Stubs by Martin Fowler. When naming cases when he think "mockist" TDD will be advantageous, he said It's particularly worth trying if you are having ...
4
votes
4answers
397 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; ...
4
votes
2answers
96 views

How deep should we dive into testing different layers

We use Drupal 7 as our base CMS tool. For one specific product, something like an ERP, we've created sort of a non-drupal layer, to keep our specific business code in. It would be something like ...
4
votes
3answers
111 views

Using mocks to set up object even if you will not be mocking any behavior or verifying any interaction with it?

When building a unit test, is it appropriate to use a mocking tool to assist you in setting up an object even if you will not be mocking any behavior or verifying any interaction with that object? ...
3
votes
2answers
174 views

Is it okay to mock multiple objects in one class?

For developers with extensive experience using mocks, is it okay to mock multiple objects in one class (ie satisfy multiple interfaces) or is this not recommended? I am wondering because mocks are ...
3
votes
1answer
1k views

How to unit test image processing code?

I'm working in image processing (mainly OCR) and I wonder how I should integrate unit tests in my development. I'm already using unit tests for more "common" type of code but when dealing with image ...
3
votes
2answers
239 views

Reusing Mocks/Stubs as Test Specification

We use mock to ensure that a unit test only runs against the code that should be tested. This prevents that a bug in a module A causes tests of a dependent module B to fail. However, now we also have ...
3
votes
1answer
117 views

Stubbing and mocking boundaries

Suppose I'm building a JSON API and would like to include an endpoint that returns a list of recently created posts. Somewhere in my system is an existing Post model with the following test: create ...
3
votes
2answers
337 views

What is the most appropriate testing method in this scenario?

I'm writing some Objective-C apps (for OS X/iOS) and I'm currently implementing a service to be shared across them. The service is intended to be fairly self-contained. For the current functionality ...
2
votes
2answers
378 views

How do functional languages handle a mocking situation when using Interface based design?

Typically in C# I use dependency injection to help with mocking; public void UserService { public UserService(IUserQuery userQuery, IUserCommunicator userCommunicator, IUserValidator userValidator) ...
2
votes
3answers
234 views

how and should I 'unit test' an entire package?

I'm still learning to be good about doing unit level testing, as I've always been a little sloppy about only doing functional testing in the past, so I want to double check I'm doing this 'right'. I ...
2
votes
1answer
166 views

Unit Testing Open Source projects that require keys for api calls

What's the recommended method for setting up tests on code that requires a private key to make calls to external web services? Full on mock objects or is there a way of distributing open source unit ...
2
votes
5answers
130 views

Should mock objects for tests be created at a high or low level

When creating unit tests for those other objects, what is the best way to create mock objects that provide data to other objects. Should they be created at a 'high level' and intercept the calls as ...
2
votes
3answers
367 views

Mocking static methods

Having recently returned from a Test Driven Development (TDD) course I had the following thought. While writing unit tests using Mockito we came up against of the problem of mocking static methods. ...
2
votes
2answers
217 views

Returning a mock object from a mock object

I'm trying to return an object when mocking a parser class. This is the test code using PHPUnit 3.7 //set up the result object that I want to be returned from the call to parse method ...
2
votes
1answer
498 views

How to unit test a web client?

I am having a lot of trouble understanding how to unit test my web client. I have just finished a project using TDD for the first time - this project hd no external dependencies such as API calls or ...
2
votes
2answers
180 views

Mock up class object

For example I have class ItemDownloader which has method downloadItem. This method creates instance of another class - Download to perform actual downloading. Question is: I want to test ...
2
votes
1answer
5k views

How do I use PowerMock / Mockito / EasyMock to use a mocked object for dependency injection?

I have an AuthenticationManager.authenticate(username,password) method that gets called in someMethod of a SomeService under test. The AuthenticationManager is injected into SomeService: @Component ...
2
votes
1answer
87 views

Library to fake intermittent failures according to tester-defined policy?

I'm looking for a library that I can use to help mock a program component that works only intermittently - usually, it works fine, but sometimes it fails. For example, suppose I need to read data ...
2
votes
4answers
205 views

Test Doubles, Mocks and Stubs - when not to use & why, for Ruby, Rspec

I've learned about mocking and stubbing and I've seen how they can help me create great test suites that run blindingly fast and thus speed up my development process hugely. However I've also seen ...
2
votes
1answer
184 views

Mocking complex data operations in integration tests

We have some problems figuring out how to mock complex data and operations on it. First the problems with generating complex data: In one of our components we have really complex data structures ...
2
votes
2answers
492 views

How to populate a private container for unit test?

I have a class that defines a private (well, __container to be exact since it is python) container. I am using the information within said container as part of the logic of what the class does and ...
1
vote
4answers
916 views

Moving from mock to real objects?

I'm like doing TDD so I started everything mocking objects, creating interface, stubbing, great. The design seems to work, now I'll implement the stuff, a lot of the code used in the stubs are going ...
1
vote
2answers
129 views

How to unit test without mocks and not be tied to a concrete implementations of an interface

I consider myself to be a 'mockist' on the 'mockist' vs 'classicist' debate, but I am trying to understand the other point of view. Imagine I have this class, where a concrete instance of IDependency ...
1
vote
4answers
299 views

Is application-specific data required for good unit testing?

I am writing unit tests for a fairly simple function that depends on a fairly complicated set of data. Essentially, the object I am manipulating represents a graph and this function determines whether ...
1
vote
2answers
185 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 ...
1
vote
3answers
365 views

Unit-testing functions without business logic (only checks) [duplicate]

My team is trying to find out the best way to test one of our components; the main scope is to check if the user/actor has the right to complete the operation requested. The component is formed by ...
1
vote
2answers
159 views

TDD - Making assertions about C# Expression trees

How would you unit test the PerformUpdate method in the sample below? This is a (very) simplified version of some code that performs updates against a database where the schema is dynamic and ...
1
vote
3answers
551 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 ...
0
votes
4answers
176 views

What does “mockable” mean?

I came across this term when looking into PHP's "traits" (which are apparently bad, since, among things, they're not mockable), but I can't really find a definition for this "mockability". It might be ...
0
votes
2answers
77 views

Should we write a unit test for class that call another class that have code written

Suppose that there are two class 'A' and 'B' 'A' has a lot of nested conditions that have all unit test covered. 'B' has a property that will call class 'A' and return value according to the result ...