Skip to main content

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
2 answers
87 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 ...
mike1952's user avatar
  • 109
1 vote
1 answer
88 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 ...
Veverke's user avatar
  • 451
0 votes
1 answer
805 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?
Akshunya's user avatar
  • 111
-1 votes
1 answer
202 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, ...
oligofren's user avatar
  • 107
2 votes
3 answers
260 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 ...
Bimo Adityarahman's user avatar
1 vote
1 answer
129 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
  • 121
2 votes
2 answers
198 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
170 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
190 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 :...
Ahri's user avatar
  • 279
-1 votes
3 answers
115 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
488 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
857 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
  • 491
2 votes
1 answer
218 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
  • 261
4 votes
5 answers
2k 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
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(); ... } } @...
ch271828n's user avatar
  • 181
0 votes
2 answers
266 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
  • 734
1 vote
1 answer
1k 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
10k 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
344 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
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 (...
Delgan's user avatar
  • 366
1 vote
2 answers
104 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
1 vote
4 answers
3k 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
  • 127
0 votes
2 answers
218 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
201 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
127 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
442 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
93 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
406 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
6 votes
1 answer
14k 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
691 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
276 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
975 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
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,...
rewolf's user avatar
  • 139
6 votes
3 answers
13k 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
  • 169
2 votes
1 answer
299 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
54 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
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....
JobHunter69's user avatar
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 ...
DodgyCodeException's user avatar
0 votes
2 answers
2k 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
703 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
889 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
452 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
454 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
4 votes
2 answers
5k 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
  • 153
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
1k 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
1k 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
4 votes
1 answer
997 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
  • 577
1 vote
2 answers
844 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