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
3 answers
124 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
340 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 ...
14 votes
4 answers
2k views

Do mocks violate the Open/Closed principle?

Some time ago I read, on a Stack Overflow answer that I can't find, a sentence that explained that you should test public APIs, and the author said that you should test interfaces. The author also ...
0 votes
2 answers
104 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 ...
6 votes
1 answer
792 views

Cons of using unconstrained frameworks for unit testing

It's very easy to write unit tests for legacy code using unconstrained frameworks, such as TypeMock Isolator. But what about writing unit tests for newly written code? Is it a good practice to use an ...
1 vote
1 answer
96 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
889 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?
16 votes
6 answers
9k 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 ...
2 votes
3 answers
261 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 votes
1 answer
233 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
2 answers
202 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
1 answer
132 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 ...
15 votes
4 answers
18k views

How to test code that depends on complex APIs (Amazon S3 for instance)?

I am struggling with testing a method that uploads documents to Amazon S3, but I think this question applies to any non-trivial API/external dependecy. I've only come up with three potential solutions ...
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 ...
1 vote
1 answer
192 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
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 ...
133 votes
13 answers
44k views

(Why) is it important that a unit test not test dependencies?

I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on ...
4 votes
2 answers
505 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
883 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 ...
5 votes
3 answers
6k 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 ...
2 votes
1 answer
224 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
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 ...
121 votes
8 answers
50k 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 ...
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(); ... } } @...
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 ...
0 votes
2 answers
268 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); ...
10 votes
5 answers
16k 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 ...
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 ...
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 ...
1 vote
1 answer
351 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 ...
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 (...
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) ...
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 ...
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 ...
0 votes
2 answers
220 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
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 ...
-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 ...
0 votes
2 answers
444 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
95 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
411 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 ...
1 vote
2 answers
2k views

Using interfaces as part of encapsulation

I'm creating interfaces for a number of our existing classes for mocking reasons. Many of these classes also have package scope methods as an attempt to give some level of encapsulation by ensuring ...
1 vote
1 answer
697 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
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 ...
1 vote
1 answer
997 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,...
2 votes
1 answer
303 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
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 ...
4 votes
1 answer
1k 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 ...
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 ...