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)

0
votes
2answers
84 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 ...
2
votes
3answers
209 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 ...
15
votes
6answers
707 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 ...
2
votes
4answers
165 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
3answers
223 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. ...
0
votes
2answers
93 views

Using interfaces as part of encapsulation

I'm creating interfaces for a number of our existing classes for mocking reasons. Many of these classes also have package scope methods as an attempt to give some level of encapsulation by ensuring ...
4
votes
6answers
318 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 ...
1
vote
2answers
142 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
291 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 ...
6
votes
3answers
459 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. ...
2
votes
1answer
318 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 ...
-1
votes
1answer
156 views

What functionality should a (basic) mock framework have? [closed]

If i would start on writing a simple Mock framework, then what are the things that a basic mock framework MUST have? Obviously mocking any object, but what about assertions and perhaps other things? ...
2
votes
5answers
126 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 ...
4
votes
1answer
205 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 ...
0
votes
1answer
89 views

Method visibility for testing partial mocks

I'm currently writing unit tests to test behavior of a method and would like to partially mock the methods calling injected properties. For example: public void doSomething() { int complicatedInt ...
2
votes
1answer
143 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 ...
0
votes
2answers
353 views

when to mock in unit testing [duplicate]

I am trying to write unit tests in javascript for an application made in nodejs. What I want to know is - Say there is a class Vehicles which is dependent on another class Roads. Should I mock the ...
3
votes
1answer
289 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 ...
3
votes
1answer
108 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 ...
1
vote
3answers
333 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 ...
13
votes
3answers
802 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 ...
2
votes
2answers
163 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 ...
5
votes
2answers
3k 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 ...
2
votes
1answer
158 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 ...
3
votes
2answers
172 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 ...
10
votes
2answers
224 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 ...
4
votes
2answers
94 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 ...
8
votes
3answers
817 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 ...
2
votes
2answers
212 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 ...
4
votes
7answers
748 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 ...
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 ...
7
votes
1answer
268 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
4answers
356 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 ...
4
votes
1answer
170 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 ...
7
votes
5answers
560 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 ...
3
votes
2answers
223 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
4answers
361 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; ...
1
vote
4answers
849 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
350 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) ...
1
vote
4answers
277 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 ...
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
2answers
450 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 ...
13
votes
3answers
987 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 ...
10
votes
3answers
2k 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 ...
15
votes
5answers
802 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?
7
votes
3answers
362 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 ...
41
votes
10answers
4k 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 ...
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 ...
10
votes
4answers
817 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 ...
36
votes
8answers
13k 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 ...