by brianwawok · 10y ago · 0 comments
Also, they are basically required if you want to do any testing with external services. I'm not sure how else you could create unit tests for DBs etc.
I find it funny that in most discussions around mocking, a database is the given example. 100% of the time when I need a database for "unit tests", I use a database. Not a mock. I don't want to know that when I do select blar from flar I get back the value 7, and hardcode it. I want to actually load the data and see it happen. Because if I don't, how do I know that my code will actually work in production?
Sometimes that involves sqllite (easy but not preferred because I don't use it in production). Sometimes it just involves running a local database that gets built up for a unit test run. Sometimes something like docker, sometimes not. Sometimes a shared "dev" database. Lots of options. But literally every project I have been involved with that used a REAL database instead of mocking it out, has worked better and been more stable and easier to change.
Now for actual external services.. say you call some Netflix API. Yes don't call that for real in a unit test. But you can do that with either:
a) Basic mocking, because you created a very simple API. You don't just call some random driver directly with your code, right? You have an interface in front of it. Then you can mock it with no final or static override crap going on.
b) If your API is clean enough, you can just code up a very simple dummy implementation. Lets you set the return value for methods, then the next call it gives them back.
I don't feel that strongly between A and B above. But I feel very very very strongly about people mocking databases or, even worse, every random call between classes in their own codebase, or someone elses codebase.
That is not a unit test. What if your database is corrupted? What if your mock Netflix API is buggy? The second you do anything like that you're no longer unit testing. The whole point is you're testing a single method/class in isolation -- Mockito gives you the power to assume external structures (the database) work as expected.
If you don't like unit testing, sure. But unit testing is what Mockito is all about.
For most use cases, a given piece needs 1 test around it. "The test". If you can test your logic + view + db all in 1 test, you get a pretty huge efficiency gain.. over sitting there writing "unit tests" where you mock what a select * from a database returns. Life is too short to do that.
The cases you've mentioned - I'd most likely leave up at integration & failure injection testing. Still handled by a CI/CD infrastructure but done at a level where the availability of an entire environment (DBs, multiple nodes, h/w, etc.) can be expected.
This would fall under the purview of integration/acceptance testing?
I do not write a test that would involve mocking a database. If I have 1 test, it is going to be 1 test that uses the database. Its "the test".. not a "unit test" or an "acceptance test" or a "integration test".. just, "the test."
Unit tests should be testing code that has a small set of dependencies and test a single operation with those dependencies.
In my experience, most people just throw up their hands and integration test instead of aggressively working the design into shape.
I've had lots of challenges with some frameworks (EasyMock makes it easy to make a mess) but Mockito is so nice to use, I can't imagine writing Java code without it for my basic unit tests.
The classic example is again storage, suppose you use Redis for storing key-value strings. Create an interface that has the store and get method and have two implementations of that, one that actually injects a Redis client and one that injects nothing and saves everything in a map.
Now you can test the real implementation with mocks and a real Redis if you want, but all the other tests can run on the in-memory implementation just fine and super fast. No need to mock anything.
I've found mocks useful to exercise interactions (and Mockito is awesome), but once you have a fake for the types at the boundary of your dependency graph you're pretty much sorted.
For people looking for an alternative take on why it is good practice to let mocked objects have default stubs for unmocked methods, see this article [1]. This article is referenced in the mocktio documentation.
[0] http://site.mockito.org/mockito/docs/current/org/mockito/Moc...
[1] https://monkeyisland.pl/2008/07/12/should-i-worry-about-the-...
As the other reply says, that doesn't help because it never gets that far.
To my mind the article you link has the wrong premise, because I do make my data tests that way - I assert that the data is exactly what I expected. Surprise changes to data would be bad, as would surprise changes to state.