News for hackers. Stuff that matters.
Re:Mockito 2.1.0 by brianwawok on Monday October 03, 2016 @10:50AM (#12627777)
Yes. Every project I have ever used with a web of mocks, especially around final things made refactoring near impossible. At some point the codebase got so brittle, it fell apart.

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.

Re:Mockito 2.1.0 by ng12 on Monday October 03, 2016 @11:03AM (#12627879)
> 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?

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.

Re:Mockito 2.1.0 by brianwawok on Monday October 03, 2016 @11:50AM (#12628419)
What if my DB is corrupted - Why did it get corrupted? Sounds like you have a good failure to chase down. It would be even worse if your prod DB got corrupt!

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.

Re:Mockito 2.1.0 by uw_rob on Monday October 03, 2016 @12:35PM (#12628924)
Having just a single set of tests has a lot of draw backs compared to splitting up your tests.

Consider this, we have a simple CRUD app that uses a datastore.

Since you want everyone to be able to run the test suite locally, setting up a local datastore manually isn't an option. You need a script/function that will setup the database.

Because you want your tests to be as deterministic as possible, and you don't want the order that tests are executed in to change the results, you need to setup the database before every test, and then clean it up again after the test.

However, setting up the datastore and cleaning it up again can take significant amount of time. Even if it only takes 5 seconds, when you multiply it out for each test your test suite can now take 100x longer to run.

This means you have now limited yourself to being able to run tests after you have completed large changes, and not incrementally.

Also when a test breaks, you now need to determine if it is broken because your DB isn't configured correctly, or because something is wrong with your code.

You can help mitigate these problems by creating 2 sets of tests. Unit tests and integration test. Unit tests use mocks, and run quickly. Integration tests will create a environment that mimics what you have in prod, and will take a while to complete.

I think you also overestimate how long it takes to write mock tests.

Hopefully this provides some insight into why I think unit + integration tests are the way to go.

Re:Mockito 2.1.0 by brianwawok on Monday October 03, 2016 @01:23PM (#12629393)
> Since you want everyone to be able to run the test suite locally, setting up a local datastore manually isn't an option. You need a script/function that will setup the database.

Since we want our environment to be reproducible and stored in git, this is a requirement anyway. If your schema is not versioned in Git, and cannot automatically be applied in all environments, including prod, you are doing it wrong.

> Because you want your tests to be as deterministic as possible, and you don't want the order that tests are executed in to change the results, you need to setup the database before every test, and then clean it up again after the test.

No not really. Lots of ways to solve this. For example DJango will run a test in a transaction, ensure it passes, then roll back the transaction. Order thus does not matter, the DB is cleaned up for free.

> However, setting up the datastore and cleaning it up again can take significant amount of time. Even if it only takes 5 seconds, when you multiply it out for each test your test suite can now take 100x longer to run. This means you have now limited yourself to being able to run tests after you have completed large changes, and not incrementally.

So the old wisdom on this is that unit tests should be able to run in 5 minutes or less. Just a silly Django crud app with a parallel test runner, you can do something like 50k-100k tests in 5 minutes (Most tests take about 1/10th of a second, with several going on in parallel). Past the 50-100k test mark it gets harder to do locally, but is trivial to do in CI (for example, there are several CI as a service services that split your tests between containers, so you can run any number of parallel containers).

> Also when a test breaks, you now need to determine if it is broken because your DB isn't configured correctly, or because something is wrong with your code.

Because database state and schema is stored in git, and applied programmatically, we already ruled out the DB. So if a test breaks, it is the code.

> You can help mitigate these problems by creating 2 sets of tests. Unit tests and integration test. Unit tests use mocks, and run quickly. Integration tests will create a environment that mimics what you have in prod, and will take a while to complete.

To me, you had a problem (Database not consistent), and rather solve it in a sane way (store schema in source control, apply automatically) - You solved it in a very roundabout way (worry about which class of a tests a given test is in, write two types of tests, still fight bad schema in prod issues because you didn't solve the root problem (schema not in source control) )

Re:Mockito 2.1.0 by ng12 on Monday October 03, 2016 @12:04PM (#12628583)
I'm just pointing out that your problem is with unit tests, not with Mockito. I think unit tests are invaluable on large projects, so I find Mockito to be an incredibly valuable tool. I'm also not arguing against integration testing, just that unit testing is not the time to worry about your prod environment.
Re:Mockito 2.1.0 by brianwawok on Monday October 03, 2016 @12:14PM (#12628688)
Why should your tools having to spin up a database in 3 seconds conflate local dev testing and your prod environment? Not advocating a 8 database cluster with hot failover on your laptop.. just a simple database.
Re:Mockito 2.1.0 by gautamdivgi on Monday October 03, 2016 @02:00PM (#12629751)
Would all those scenarios really be a part of a "unit" test? I generally approach unit tests to ensure that given pre-conditions of the "external structures" you have a defined set of post-conditions that the test can exercise.

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.

Re:Mockito 2.1.0 by wlamartin on Monday October 03, 2016 @10:58AM (#12627833)
> 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?

This would fall under the purview of integration/acceptance testing?

Re:Mockito 2.1.0 by brianwawok on Monday October 03, 2016 @11:49AM (#12628399)
I don't really want to split hairs about what test is what test.

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."

Re:Mockito 2.1.0 by wlamartin on Monday October 03, 2016 @12:16PM (#12628714)
That's fair if you don't want to make that distinction but it is common vernacular and each testing layer has a set of properties which lead to separate pros and cons. The discussion then becomes, "what are the advantages/disadvantages of testing in isolation versus testing integrated components" which is a well trodden topic.

In a previous comment you mentioned that you get huge efficiency gains testing everything together. Well, during writing you could make that argument (though I would consider that contentious) but when it comes to maintenance and refactoring, I would argue having "the test" is going to result in a much larger surface area for you to debug when you change something and "the test" breaks.

Re:Mockito 2.1.0 by ng12 on Monday October 03, 2016 @12:09PM (#12628641)
Are you working with other developers on these projects? "The test" failing is much less useful information than a "unit test" failing. If the unit test fails I know it's something pretty fundamental -- I can isolate it to a single method. If the integration test fails, but the unit test passed, I can be reasonably assured the problem is with the database.

Tests are just as much about making software maintainable as they are about making sure it's correct.

Re:Mockito 2.1.0 by brianwawok on Monday October 03, 2016 @12:15PM (#12628704)
Yes, these were for fortune 2000 companies or late state startups... projects with piles of "unit tests" where you try to test one class at a time and hand type what a database does were all inevitably crap, and either failed and got rewritten, or all the crap got written out.
Re:Mockito 2.1.0 by softawre on Monday October 03, 2016 @12:44PM (#12629017)
My goal is to be pragmatic with testing. We have "tests" that actually spin up a real database too, but not all of our tests do that.

Where I'm with you is fighting the "architects". The guys that will constantly say you're not doing "real unit testing" and that you should mock every function (and then have to change every mock with every change and blah blah).

To me, being pragmatic with testing is a key component of GTD in modern software development.

Re:Mockito 2.1.0 by whoisthemachine on Monday October 03, 2016 @01:40PM (#12629560)
It sounds like you are not testing units of work with your unit tests that require "webs of mocks", but rather higher level integration tests.

Unit tests should be testing code that has a small set of dependencies and test a single operation with those dependencies.