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.

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.

Most commonly, isolation frameworks are used to dynamically build a mock, such as:

200 questions
133
votes
13 answers

(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 StackOverflow emphasize testing only a unit, not its…
dsimcha
  • 17,284
125
votes
9 answers

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 should break by a bug in the tested unit, as opposed…
101
votes
11 answers

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 testing A, you set (postulate) the behaviour of…
bvgheluwe
  • 1,187
93
votes
11 answers

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 1.0, mocking static stuff is not too easy. Does that…
Vaccano
  • 4,077
  • 5
  • 34
  • 39
86
votes
3 answers

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 object ... private Bar getBar() { Bar result =…
Duncan Jones
  • 1,402
40
votes
2 answers

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 testing, the professor said to use mocks and mocking…
32
votes
6 answers

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 using, which, among other things, caused timestamps…
23
votes
7 answers

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 can test their public interfaces and I find that…
23
votes
3 answers

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 GetNewYorkProfit(); public decimal GetMiamiRevenue(); …
User
  • 1,571
22
votes
3 answers

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 and while we haven't had any difficulty with the…
PremiumTier
  • 599
  • 1
  • 5
  • 15
20
votes
2 answers

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 if utilizing a framework (such as Spring Data),…
user253058
16
votes
6 answers

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 for every public class", 90% of the answers say you…
16
votes
1 answer

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 an additional Wrapper class which provides a…
16
votes
6 answers

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 to mock. It takes me 3 or 4 times longer to write…
sangfroid
  • 3,259
15
votes
3 answers

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 should I consider when choosing which framework is…
epotter
  • 2,846
  • 26
  • 27
1
2 3
13 14