Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

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.

2
votes
3answers
208 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 ...
1
vote
1answer
117 views

Handling hardware dependency while unit testing

I am writing a driver for an IMU sensor using test driven development approach. Communication protocol of choice is SPI. Using CubeMX and STM32F415 processor, SPI interface is implemented in ...
9
votes
7answers
2k 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 ...
1
vote
1answer
129 views

How to pass a mock object into a class for unit tests

There seem to be many ways to pass a mocked object into a class for unit testing and I am unsure which is the proper approach to take for my PHP application. If I was using Dependency Injection then ...
94
votes
11answers
15k 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 ...
1
vote
1answer
55 views

Using purely static, hardcoded files for UI tests or some logic to determine which data to send back

I'm a front-end developer, working on a large enterprise application. The application has a 'Dashboard' view, consisting of tiles, each of which display different types of data, or different ...
6
votes
1answer
316 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 ...
2
votes
1answer
129 views

Is it OK to mock or stub libraries in weakly typed languages?

I have quite some experience with TDD in Java and Kotlin and currently try to learn testing with Javascript. I am not sure if this is really a question about weak vs. strong typing or about general ...
0
votes
1answer
141 views

When to use stubs

I'm trying to get into TDD, and a lot of examples sugests that we should use stubs to make our code more flexible. If I'm using javascript (for example) then why should I use stubs, since methods and ...
0
votes
4answers
2k 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). ...
2
votes
3answers
514 views

Testing a class that uses external interface

I have a class that internally uses an interface that is defined externally. Testing this class becomes difficult since I need to mock out this interface but I am required to reference the external ...
1
vote
1answer
55 views

Mocked dependencies verification - Best practices

I have a simple question about best practices in unit test verifications. Given this example: @Test public void methodUnderTest() { when(mockedDependency.someMethod()).thenReturn(someValue); ...
4
votes
2answers
1k 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 ...
1
vote
2answers
140 views

TDD: How to handle recurring boot-up sequence?

I am new to adopting a completely TDD approach using DI so that I can mock every dependency. One of the pain points that I identified so far is the fact that whenever I do work in my constructor every ...
0
votes
1answer
99 views

What's the correct way to test code which performs IO?

Is there a nice way to test code which performs IO? In Java in particular, if you go the Mock route, you need to wrap most of the native APIs in your own mockable classes, something like: public ...
5
votes
3answers
418 views

Unit testing private calls, or not

I have this method, which has multiple private calls: GetConfigStatuses(), ApplyFilters(), GetConfigListInfo(). Since these do not expose anything public I cannot test them like I would a public. ...
0
votes
2answers
40 views

Unit testing mapped properties, inside service

Consider this simple method. public async Task AddItem(TodoItem item) { if(item.DueAt < DateTime.Today) { item.ValidationMessages.Add( "The date is missing!" ); ...
1
vote
1answer
171 views

What level of unit testing in a controller that simply calls ViewFactory to get Model

I have implemented a ViewFactory to seperate the generation of View Models from the Controller logic. The ViewFactory implementation has a comprenhensive set of unit tests that cover all paths. As ...
1
vote
4answers
182 views

Avoiding exposing property in order to satifsy unit test

I'm using dependency injection to supply one of my classes (SomethingManager) with something it needs (SomethingEngine). In the constructor of the SomethingManager I set some properties on ...
2
votes
1answer
321 views

How do I mock a large data source?

I'm developing a ML/AI solution and I'm looking to write some unit tests. I end up with large objects with a lot of data. These come from a data source which is built at runtime. The data created as ...
0
votes
3answers
2k 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 ...
1
vote
2answers
2k views

Should I mock Repository and Unit Of Work for Unit Testing?

I have read this article: https://www.gaui.is/how-to-mock-the-datacontext-linq/ Since this article is almost 4 years old I have no hope that in article's comments my question will be answered so I ...
1
vote
4answers
354 views

At what point in the TDD process should I mock?

Just to get any confusion out of the way: I'm starting from the position where I already know what to mock, where and how to do it. I just don't know how to fit the mocking into the TDD process. So ...
1
vote
5answers
290 views

TDD and waiting on dependencies

I am on a fairly new team that is also new to TDD and Agile/Scrum. Currently we are developing a project that consists of web API and a native iOS and Android application, with a small team of devs ...
0
votes
2answers
822 views

How should I unit test a function that uses setters?

I'm using a repository pattern design and I've hit a stumbling block when writing a unit test for one of my methods. I'm fairly new to writing unit tests, so I would appreciate any help! Let's say I ...
14
votes
2answers
5k 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 ...
-2
votes
2answers
414 views

Writing unit test cases are taking time, any advice?

I am new to unit testing. Started working on unit test using PHPUnit. But it seems to be taking too much time. If consider I take 3 hours to write a Class, its taking my 7 hours to write test case for ...
3
votes
4answers
473 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); ...
4
votes
2answers
239 views

Writing mockable code in C++

I am focusing on writing easy to test code these days and I realize that most of my classes follow the skeleton below: Some class ObjectManager needs a DatabaseRepo, while there is only one type ...
7
votes
3answers
371 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 ...
-1
votes
2answers
304 views

Is there no place for EasyMock.expect().andReturn() in TDD tests

TDD and general best practices around writing tests say that we should test the behavior and not implementation. Hypothesis: In that context, EasyMock.expect().andReturn() should never be used ...
15
votes
3answers
6k 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 { ...
8
votes
1answer
638 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 ...
0
votes
2answers
2k views

What is the best/most Pythonic way to mock a private function?

Consider a module with a 'public' function which looks something like this: def func(arg): val = _generate_something(arg) _do_something(val) As you can see this is a 'void function'. Its ...
3
votes
1answer
837 views

How to handle test with initialising functions that call mocked functions?

I am working on an embedded C project where there is a function used to initialise a module's (foo) context object handle. Thus when testing using the Ceedling I have used this initialising function ...
1
vote
1answer
415 views

How to not test implementation when method returns void?

Many places in stackexchange state that you should not unit test implementations, only the public interface of a class. But what if the public interface is a method that doesn't return any value, for ...
1
vote
2answers
1k views

Mocking a file parser

I have a file parser and my manager told me that I need to create unit tests for it. Here is my code: public class ParsedDetails { public int Id { get; set; } public Guid Guid { get; set; } ...
5
votes
3answers
356 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 ...
4
votes
2answers
159 views

How to handle mocking equivalent methods without making tests fragile?

I am writing a unit test with mocks and I am concerned about over specifying the test. The idea being that if the implementation changes in a compatible way the test shouldn't fail since that makes ...
3
votes
1answer
147 views

Doesn't Jmockit make a lot of techniques mentioned in “Working Effectively with Legacy Code” redundant?

I'm currently reading "Working Effectively with Legacy Code" by Michael Feathers. So far, a lot of techniques the book mentions, start with a problem similar to: "it's really difficult / it would ...
3
votes
2answers
142 views

When does a unit end and Integration begin

In TDD you write the smallest unit of code to make your test pass. For example if building a Stack object you will want to test the push and pop methods. That is pretty straightforward. Those are ...
1
vote
2answers
70 views

Should other instances of SUT be mocked?

Consider a playfield, with various player types (let's say Hunters and Animals), all moving on the playfield (and doing some other things). E.g., in a unit test for the Hunter class, there can be such ...
1
vote
1answer
524 views

Mocking of non-copyable objects

I find myself often in the situation where I want to mock a non-copyable object, for example a DbHandle handle. I was going back and forth looking at different design choices, and I settled on the ...
1
vote
1answer
809 views

Spring - rest API - How to unit test a rest API which gets data from another service

I am new to rest API's as well as mocks. Please help me .. I have a set of rest API's for which I need to write unit tests. rest API in turn calls another service , gets the JSON response and sends ...
2
votes
1answer
222 views

Should I have code that only exists for testing?

I have a service class which is responsible for instantiating repositories and using them. It makes an external Http request which ideally I would like to mock for testing. However, I am not quite ...
2
votes
3answers
1k views

When to mark a function as virtual?

I'm trying to understand the idiomatic way to code. I'm using gmock to unit test the components I write. Gmock requires methods to be virtual to be able to mock but the class I'm trying to mock has a ...
0
votes
0answers
404 views

Bringing in unit test code with Mocking Frameworks in an established ASP.NET C# software application

Here is the information about my development environment: MongoDB 3.0.0 MongoDB C# Driver Version 1.7.0.4714 Microsoft Visual Studio Professional 2013 .NET Framework 4.0 We are about to do our ...
7
votes
2answers
3k 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 ...
1
vote
2answers
1k views

Testing using mocking, must I mock all dependencies too?

I have the following method to test: public List<MarkId> getMarkIdList(ICar carDoc) { ICourseCar courseCarDoc = courseCarRep.get(carDoc); List<MarkWag> markWagList = ...
14
votes
1answer
3k 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 ...