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
2
votes
3answers
183 views

How can I avoid chasing my own tail when testing against complicated return values?

Sometimes there are functions that return complicated data and cannot be divided any further e.g. in the area of signal processing or when reading and decoding a bytestring to another format. How am I ...
-1
votes
2answers
56 views

Should we use wireframe tools for front end of our projects?

We just started a small startup company with 4-5 guys and since now we were working by talking to each other about the requirements and maybe some hand drawn sketches about the front end of the ...
0
votes
2answers
179 views

What if integration testing makes more sense than unit testing for a certain method?

I don't want to spam you with a ton of code, but please have a quick look at this boiler-plate method: In this scenario let's say I have a ProcessingText.py file (class) that I finished its unit ...
0
votes
2answers
44 views

Logic in data provider vs test body

Lets say we have a class Cat: class Cat{ public eat(String food){ if (food.contains("cat")){ burp(); } } private burp(){ System.out.println(&...
4
votes
2answers
244 views

Doesn't “Always test through the public interface” contradict testing of individual composed functions?

I'm currently reading "Composing Software" by Eric Elliott, which is about functional programming in JavaScript. He states that if you compose multiple functions together, and that these ...
2
votes
0answers
759 views

Should I mock ObjectMapper in my unit tests?

I have different services in a spring application that have a dependency on Jackson ObjectMapper, the unit tests rely on @InjectMocks to inject all the various dependencies to the class that is under ...
1
vote
1answer
112 views

How deep should I mock dependencies in unit tests

Consider the following function: def get_api_status(api_client): response = api_client.get('/status/') return response.content and the test for it: def test_get_api_status(): ...
2
votes
2answers
197 views

Unit-testing and mocking

There seems to be a lot of question regarding "when to mock". But I did not get an answer on my question so far. It can be, I do not know a specific search request that would point me to the ...
1
vote
1answer
149 views

Benefits of Dynamic Mocks over Static Mocks?

I'm not sure if static vs. dynamic mock is the terminology used to describe this comparison, but I got this terminology from types of mocking static vs dynamic and Hand-rolled mocks made easy. To ...
2
votes
2answers
270 views

Unit Testing Controllers without Mocks

I've done a lot of test writing using Mocks, and so I've learned that it makes refactoring difficult due to implementation coupling inherent with Mocks. I've done a lot of reading on the topic tonight,...
4
votes
2answers
332 views

How to mock a class not implementing an interface or having virtual methods

I'm trying to write unit tests for business logic classes I have control on, but which operates over some services that are not designed with the testability in mind. Currently I’ve extracted the ...
2
votes
1answer
144 views

Changing a class with static dependency injection to allow unit testing

I'm new to JUnit/Mockito and to unit testing in general. I'm asking this question in order to get feedback and learn best practices/patterns/strategies. I wrote a class but when came time to unit test ...
-1
votes
1answer
37 views

Should all third party methods that access outside resources (like other databases) be wrapped up?

From the perspective of unit testing, the code under test should obviously not be accessing outside resources so the third party methods need to be mocked. However, it seems like this is poor practice ...
2
votes
4answers
277 views

How do I deal with the fact that I am forced to make helper functions public for testing purposes?

I've encountered several scenarios that require me to mock certain helper methods because they call outside resources. As a result, I'm forced to convert all my helper methods from private into public....
1
vote
3answers
116 views

Mock a private dependency

I've started developing a Java API which will consist of just a couple of public classes, a public interface (to be implemented by the user and used as a callback, like in the Observer pattern). All ...
0
votes
2answers
110 views

What approach do I take to unit testing a class which has a method that internally calls other methods?

I have a class which has one method that is called from another class. This method internally calls several other methods to do its work. Those other methods are all public and can be called by the ...
0
votes
3answers
144 views

Is it good practice to create a facade only to be able to mock the wrapped implementation?

I'm currently writing unit tests for ASP.NET Core Controllers. Some controllers inject UserManager<T> which seems to be a really hard type to mock. After some attempts to mock or even fake it, I ...
1
vote
1answer
90 views

Is it better to test with dynamically generated input data or static data?

I have a little React app and I'm ready to test it. The first thing I need to do is to create some input objects with random data. I can proceed in one of two ways: I can create my own fake data line ...
-3
votes
2answers
66 views

Layer to mock in tests: database or higher?

Suppose there are 2 layers below the layer being tested: Technical Logic Layer: calls the DAO layer. DAO layer: calls the database (The layer being tested can call the Technical Logic Layer but ...
1
vote
2answers
106 views

Is it a best practice or anti-pattern to export mock versions of functions for a library?

Context We are creating a library that makes an API (HTTP) request to a 3rd party. During testing we have written mock versions of the functions that make external requests so that we can test the ...
1
vote
2answers
567 views

Are in-memory database a form of integration tests?

I have looked through most of the answers given regarding using in-memory database for unit test, but I could not find one that was clear to me. I am writing some unit tests to cover some of our ...
0
votes
1answer
203 views

How to avoid “mock returning mock” when using factory pattern

I have recently encountered multiple articles with title Everytime a mock returns a mock a fairy dies And I ran into exact same situation while using factory class in my code. I am writing a sample ...
1
vote
4answers
262 views

Black-box testing when testing without mocking?

My understanding of mocking vs not mocking is that mocking too much creates brittle tests that need to be changed all the time, while on the other hand less mocking better verifies that the system ...
1
vote
2answers
362 views

How to configure and setup an IOC Container for a library?

When using Dependency Injection across internal classes (implementation details) in a library which is to be consumed by multiple applications, is there a good way to configure the IOC container? ...
2
votes
1answer
327 views

Is the distinction between Mocks, Stubs and Fakes useful?

Automated software testing professionals often make a distinction between various kinds of test doubles such as Mocks, Stubs and Shim/Fakes. In fact, in the article Mocks Aren't Stubs Martin Fowler ...
1
vote
2answers
99 views

Unit Testing Complicated Service Call with Many Methods

I am trying to understand principle of Unit testing, Purpose, and Moq. We have complicated Service Call. The Service Call encompasses numerous Repositories and Services, with dependencies and ...
0
votes
1answer
664 views

Avoid too much dependencies for a Use Case in a Clean Architecture app

Context I'm currently developing an application following the Clean Architecture principles (at least I'm trying really hard to follow these). All my Dependency Injections are done manually, without ...
8
votes
3answers
1k 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 (...
0
votes
3answers
222 views

Getting random yet consistent data for testing

I have a big codebase yet to be covered with tests and rather limited access to the real data. The code that needs to be covered is very data dependent. And the data is anything but shallow and ...
12
votes
5answers
2k 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 for ...
2
votes
1answer
5k views

Unit testing abstract classes with Google mock (gmock/gtest) (C++)

I want to test an abstract class with: Pure virtual methods that should be overridden in sub-classes Non-pure virtual methods that use the pure virtual methods (as opposed to this question) class Fu ...
2
votes
4answers
225 views

Testing database insertion, removal, and modification from program code (i.e., rather than test code)

I want to ensure that my program code is correctly inserting, removing, and modifying data in my database. Full integration testing can be slow, and I have concerns about keeping mock objects and data ...
4
votes
2answers
1k views

What is recommended way to create test data for unit test cases?

I am new to TDD/unit testing. I am going to write a complex scheduling algorithm in Java. As this module is a core part of our application and there are number of scenarios in it, I want to write ...
0
votes
1answer
641 views

C# Unit Testing: Mock a Return Type

What is the best way to Mock a return type if we don't care about the actual state of the object. For example, I am Mocking a method whose return type is: IEnumerable<Document> Now, I can add ...
96
votes
9answers
26k 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 ...
8
votes
5answers
4k 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 ...
2
votes
1answer
430 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 ...
19
votes
7answers
28k 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 ...
0
votes
1answer
2k 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 ...
99
votes
11answers
17k 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
71 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 ...
7
votes
1answer
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 ...
2
votes
1answer
147 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
267 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 ...
8
votes
4answers
13k 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). ...
3
votes
3answers
2k 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
79 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); ...
16
votes
2answers
11k 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
147 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 ...
1
vote
1answer
2k 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 ...