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
0 votes
1 answer
92 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 ...
Derek's user avatar
  • 111
2 votes
2 answers
125 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 ...
rklec's user avatar
  • 31
1 vote
3 answers
161 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 ...
Dave Cousineau's user avatar
1 vote
1 answer
176 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 :...
Guiomar Raissa's user avatar
-1 votes
3 answers
111 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 ...
sergiol's user avatar
  • 157
4 votes
2 answers
356 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 ...
dbird's user avatar
  • 159
0 votes
5 answers
498 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 ...
gbro3n's user avatar
  • 481
2 votes
1 answer
180 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 ...
Mr. Boy's user avatar
  • 231
4 votes
5 answers
940 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 ...
krazune's user avatar
  • 41
0 votes
1 answer
761 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(); ... } } @...
ch271828n's user avatar
  • 181
0 votes
2 answers
207 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); ...
MGOwen's user avatar
  • 744
1 vote
1 answer
974 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 ...
Riz's user avatar
  • 196
1 vote
2 answers
7k 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 ...
Prasad Darshana's user avatar
1 vote
1 answer
264 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 ...
Simon Nicholls's user avatar
4 votes
3 answers
1k 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 (...
Delgan's user avatar
  • 366
1 vote
2 answers
103 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) ...
gebbissimo's user avatar
0 votes
4 answers
2k 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 ...
srk's user avatar
  • 117
0 votes
2 answers
204 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 ...
George Z.'s user avatar
  • 705
2 votes
3 answers
200 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 ...
Agent49's user avatar
  • 23
-3 votes
2 answers
118 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 ...
user3486308's user avatar
0 votes
2 answers
392 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 ...
Ahmed Alhallag's user avatar
0 votes
2 answers
79 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(&...
Anmol Singh Jaggi's user avatar
5 votes
2 answers
364 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 ...
user1474326's user avatar
5 votes
1 answer
11k 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 ...
Pampa Nello's user avatar
1 vote
1 answer
579 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(): ...
Qback's user avatar
  • 231
2 votes
2 answers
270 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 ...
user1415536's user avatar
1 vote
1 answer
792 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 ...
Mario Ishac's user avatar
2 votes
2 answers
1k 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,...
rewolf's user avatar
  • 139
5 votes
3 answers
10k 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 ...
kalitsov's user avatar
  • 159
2 votes
1 answer
263 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 ...
totsubo's user avatar
  • 129
0 votes
1 answer
44 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 ...
JobHunter69's user avatar
3 votes
4 answers
775 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....
JobHunter69's user avatar
1 vote
3 answers
696 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 ...
DodgyCodeException's user avatar
0 votes
2 answers
1k 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 ...
Dhruv Prakash's user avatar
1 vote
3 answers
578 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 ...
Sandro's user avatar
  • 113
1 vote
1 answer
595 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 ...
elimist3's user avatar
-3 votes
2 answers
377 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 ...
user7340's user avatar
  • 136
2 votes
2 answers
365 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 ...
nelsonic's user avatar
  • 137
3 votes
2 answers
4k 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 ...
elfico's user avatar
  • 143
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 ...
bharathp's user avatar
  • 103
2 votes
4 answers
887 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 ...
Jokab's user avatar
  • 141
1 vote
2 answers
852 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? ...
brent's user avatar
  • 121
2 votes
1 answer
743 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 ...
Chedy2149's user avatar
  • 547
1 vote
2 answers
626 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 ...
GregDavis's user avatar
1 vote
1 answer
2k 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 ...
Rafi Panoyan's user avatar
8 votes
3 answers
2k 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 (...
user avatar
0 votes
3 answers
295 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 ...
Trident D'Gao's user avatar
15 votes
5 answers
7k 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 ...
Etienne Charland's user avatar
3 votes
1 answer
9k 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 ...
pooya13's user avatar
  • 187
2 votes
4 answers
856 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 ...
user2514157's user avatar