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.
200 questions
1
vote
4
answers
234
views
Avoid Test duplication if a function created via TDD has an implementation detail that is later used in other functions
Lets say I have an API with two functions:
CreateNewMachine(CreateMachineCommand createMachine);
EditExistingMachine(EditMachineCommand editMachine);
The internal logic of both functions should be ...
12
votes
9
answers
8k
views
is it okay to mock a database when writing unit test?
I read an article about unit testing by Vladimir Khorikov, written in 2020. Here is The Article
https://vkhorikov.medium.com/dont-mock-your-database-it-s-an-implementation-detail-8f1b527c78be
It said ...
-3
votes
1
answer
146
views
Mock an API for development purpose (no testing)
If I want to implement a connection from my software to an API, which is documented but not accessible yet, what is a common way to imitate the API until it is available?
My first idea was to mock the ...
0
votes
3
answers
290
views
.Net 8 XUnit: How to mock MySql in a CQRS API using integration tests?
This is a rewrite of my own
.Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS?
in a much more clear way:
The reason for rewriting my previous question is because in the ...
3
votes
2
answers
690
views
.Net 8 XUnit: Should my tests use real data or how to mock MySql with CQRS?
I'm creating tests for our .Net 8 API. We use MySql for data storage and CQRS pattern for queries.
In my personal opinion, I shouldn't use the real database for testing, because you will use an ...
0
votes
2
answers
2k
views
Setting up private properties for unit testing
I'm doing some TDD practice for my own learning.
I'm trying to figure out how to transparently set up private properties of an object to support testing.
I have a binary tree node:
internal class Node ...
1
vote
1
answer
221
views
Benefits of resolving dependencies with IServiceCollection and IServiceProvider for Unit Tests instead of inheriting mocks from a base test class
Articles such as this point out some of the pitfalls of manually instantiating dependencies in UTs, while showing some of the benefits of doing it, instead, with the .NET's dependency container by use ...
0
votes
1
answer
2k
views
Why are sealed classes not allowed to be mocked?
Using Moq framework, it is not allowed to mock a class in C# which is sealed. Same goes for many other frameworks as well. But why is it not allowed?
-1
votes
1
answer
753
views
Alternatives to using mock libraries such as Mockito in unit testing
Typically, when writing unit tests I tend to need to stub out collaborators and also mock some behavior in one or more of the collaborating objects. Say if I am testing a Service that is using a Dao, ...
2
votes
3
answers
315
views
How do you decompose big object for testing?
I have a package that provides an object with quite a lot of features owned by it.
Let us say the object is an HTTPServer, and when the user initializes it by providing config and a request handler ...
1
vote
1
answer
173
views
How do I mock API responses with security in mind?
My team and I are beginning to mock our API responses in our iOS app so we don't have to worry about our backend being up when testing.
I have conditional compilation directives based on the ...
2
votes
2
answers
311
views
Where to put interface files for mocking aka what are best practices for organizing interfaces in a C#/.NET Core project with NSubstitute, Moq etc.?
Question
I'm working on a C#/.NET Core project, and I'm looking for guidance on organizing interfaces, especially when it comes to using NSubstitute, Moq or other libraries for mocking, because there ...
1
vote
3
answers
189
views
Isolate or Redundant Test?
I have lots of code like the following. An "Entity" type that has some numerical properties. To be able to reuse the arithmetic I write the arithmetic functions against an interface. I use ...
1
vote
1
answer
211
views
Abstracting away small function inside a class for easier mocking during testing
I have a function that looks roughly like this:
async function getValue(connection: Connection): Promise<number> {
const value = await connection.getValue();
return value < 0 ? value :...
-1
votes
3
answers
138
views
Code Coverage and Unit Tests nomenclature [closed]
About tests:
I have the following view on nomenclature:
Unit tests are the kind of testes where you have a function
ExtractBacon, where there is a function with an entry parameter Pig
and a return of ...
4
votes
2
answers
786
views
How to write maintainable unit tests using classical style of unit testing
When I am using the classical style of unit testing, how do I keep the number of test cases for an object that has many collaborators from growing too large? And how do I keep the setup of each test ...
0
votes
5
answers
1k
views
Unit Testing - should / how should I write tests to cover new code that doesn't affect the interface of a method?
Having been trying to improve my unit tests, I'm trying to adhere to the principle of avoiding call verification. This is because it aligns with other principles I believe to be true:
We Should test ...
2
votes
1
answer
273
views
Test coverage for various code logic permutations... Metadata/Artifacts in algorithm results to describe which case?
I'm sorry if the title is confusing, I don't know if what I am describing has a proper name so let me describe...
I have an algorithm which contains quite a bit of nested if/else if/else logic for ...
4
votes
5
answers
3k
views
When mocking a class in a unit test, how should I handle dependency classes that have multiple similar get methods?
Let's say there's a class that processes text, and it gets that text from another class as a buffer. If this buffer class has multiple get methods, like readLine(), readChar(), readCharCode(), how ...
0
votes
1
answer
1k
views
Mock a bean with 10 methods when I only use one?
I face some situations similar to the following simplified one:
@Component class ServiceOne {
@Autowired ServiceTwo two;
void act() {
...
two.a();
...
}
}
@...
0
votes
2
answers
331
views
Automated testing: should I check a sub-function was called, or should I check it's result?
I have a big function that does several things, including some database operations, and calling another smaller function. Something like:
BigFunction()
{
DB.SomeTable.AddRow(newRow);
...
1
vote
1
answer
2k
views
How to test around a giant static class?
I am dealing with an old .net code base which has a PrivilegeChecker static class with hundreds of static methods, each of which takes in some user id and some other params, and then fetches some info ...
2
votes
2
answers
13k
views
Is it a good practice to Mock entity manager in spring boot unit testing
I currently design an API using spring boot. In my service layer, I use Entity Manager for accessing the database. I have provided a method in my service layer below as an example.
public Object ...
1
vote
1
answer
576
views
Python Unit Tests Mocking Imports - Removing Dependencies for CI/CD
I have a project written in python that I would like to create unit tests on.
This project has a dependency on a database project which is a sort of abstraction layer to data connections.
The issue ...
5
votes
3
answers
2k
views
Should I make my class mockable by marking its methods as virtual or by creating an interface?
In the process of refactoring non-testable code we are re-designing some of ours classes so that it can be easily replaced by a stub or mock during unit tests.
Here is an example of such class (...
1
vote
2
answers
119
views
Where shall we start mocking?
Scenario:
Our CLI-script downloads data
Therefore, amongst other things such as pre/postprocessing, it calls a function from another (internal) python package (which is maintained by another group)
...
1
vote
4
answers
4k
views
How to effectively unit test code with lots of database dependencies?
I find myself writing a lot of boilerplate mocking code for my unit tests. I think there must be a better way.
Background
I am working on a project that relies on complex configuration that is stored ...
0
votes
2
answers
266
views
Is there any benefit testing only with mocks/fakes/doubles?
Say I want to test the behavior of the GUI while I follow a PassiveView approach. I also use the command pattern to handle the actions of the user. So given a PersonView and a PersonService with a ...
2
votes
3
answers
231
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 ...
-3
votes
2
answers
158
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
2
answers
494
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
2
answers
121
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(&...
5
votes
2
answers
470
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 ...
6
votes
1
answer
16k
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
1
answer
855
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
2
answers
305
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
1
answer
1k
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
2
answers
2k
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,...
6
votes
3
answers
17k
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
1
answer
364
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 ...
0
votes
1
answer
100
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 ...
3
votes
4
answers
1k
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
3
answers
1k
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
2
answers
3k
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 ...
1
vote
3
answers
825
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
1
answer
1k
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
2
answers
544
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 not ...
2
votes
2
answers
579
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 ...
4
votes
2
answers
6k
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
1
answer
2k
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 ...