Mocking in Unit Tests

Debate on the use and pitfalls of mocks in unit testing, advocating alternatives like integration tests, fakes, or avoiding mocks for tightly coupled code.

➡️ Stable 0.6x Other
2,610
Comments
19
Years Active
5
Top Authors
#6771
Topic ID

Activity Over Time

2008
2
2009
13
2010
19
2011
43
2012
71
2013
57
2014
171
2015
70
2016
108
2017
171
2018
107
2019
173
2020
282
2021
260
2022
217
2023
284
2024
283
2025
277
2026
2

Keywords

e.g TL DR ORM sendgrid.com metaobject.com Scrape.url HTTP x.size martinfowler.com mocks mock tests testing unit test unit tests mocking dependency unit testing

Sample Comments

cowardlydragon Mar 30, 2015 View on HN

If you're doing extensive mocking, then you should be doing integration tests instead.

winrid Jun 29, 2019 View on HN

If you don't use mocks then won't you just end up with integration tests?

pjc50 Apr 24, 2023 View on HN

It's kind of unavoidable once you're trying to do unit tests with mocks. You either end up with interfaces for everything and a dependency injection framework, or you end up with some very non-isolated tests that take ages to run because they need inseparable parts of the whole real system.

COil Mar 25, 2021 View on HN

Don't mock everything, by mocking everything, you are testing... nothing.

jeremyjh Dec 29, 2025 View on HN

I agree. I mostly only use mocks for external service dependencies (apart from primary application database), or library features that are very problematic to use in test. A lot of code simply should not be unit tested - if an integration test can cover the same paths its far better in the long run. Performance can become an issue but it is easier to deal with that than to spend so much time maintaining and debugging mock constructs.

mpweiher Aug 4, 2019 View on HN

Why I don't mock:https://blog.metaobject.com/2014/05/why-i-don-mock.html

vlovich123 Jan 2, 2022 View on HN

Because people hear “unit tests” and “integration test” and “behavior test” and bend over backwards to start DI and mock every single little thing and be very careful about organizing things in that way, which is a huge anti pattern in my experience. You should DI/mock only when you have no other choice (eg you need to corrupt the data returned by an I/O call to test some corner condition). Generally, the less DI/mocking/supporting code just for testing purposes you have, the

gautamdivgi Nov 15, 2016 View on HN

Why would it be of concern? I think mocks have a good place in unit tests. I generally believe the premise of unit tests is to test your code against specific pre-conditions. If those pre-conditions cannot be created in a unit testing environment then mocks are justified. I would, however, be very way of mocks moving past unit testing and into integration or system testing (or other varieties like failure injection testing, performance testing, etc.). Mocks have their uses & their limitation

sethammons Jun 12, 2020 View on HN

Seemingly unpopular opinion (with a lens of Go programming): Don't mock dependencies. It is brittle. Stub/fake with dependency injection. It is unimportant that a function was called N times with arguments foo and bar. What is important is that unit tests validate that units output what they should output (success and error cases) and that integration tests ensure things work together.

shadowlight Jul 5, 2021 View on HN

Mocks mean your code is too tightly coupled. You should be able to unit test your code by creating only fake data.Things like dependency injection increase coupling to the point where you have to mock. Avoid dependency injection and other complexity within complexity features.