Making a mockery of unit testing
-
About two months ago I started a new job. I'm on my company's first ever development team and we're ultimately going to redevelop a mission-critical system from scratch, in-house. But for the moment we're doing some enhancements on the existing system, working with some people from the supplier who built and up to now maintained it.
Thanks to raisins, it was only at the end of last week that I got to see the codebase at all, and only yesterday that I had a chance to look at any of it on my own, as opposed to pair-programming from the passenger seat with one of the guys who developed this.
This is my first Java role, and my first with serious software (as opposed to front-end webdev or maintaining a squillion undocumented and mostly unconnected Perl scripts that run on, around, and vaguely in the vicinity of a third-party system), so I'm trying not to be judgemental. I've never seen a real production codebase of this sort before and obviously it won't be as clean or consistent as a toy system developed fresh by one person without release deadlines to worry about. So my first instinct when I see something strange in there is that there's probably some reason for it.
Then I found this in a test suite:
public class FooTest { Foo mockFoo = null; // snip other fields @Before public void setup() { mockFoo = new Foo(); // snip other setup } @Test public void testDoThingMethod() { mockFoo = mock(Foo.class); when(mockFoo.doThing(bar)).thenReturn(baz); // snip a little more setup assertEquals(baz, mockFoo.doThing(bar)); } }
This is Java, using JUnit and Mockito.
Yes, they mock the class under test. Then they set an expectation on the mock to return a certain value from one of its methods. Then they assert that the mock returns what they just told it to return. Yes, that is the entirety of the test, apart from a few lines setting up the object to be returned. No, callRealMethod() or anything else that would induce a mock to do the class's real behaviour is not used anywhere.
And most of the tests in the class follow that pattern.
I included the setup method because for bonus points, they instantiate the variable named as a mock as a shiny new real object before every test, and then immediately throw it away.
-
@CarrieVS said in Making a mockery of unit testing:
About two months ago I started a new job
@CarrieVS said in Making a mockery of unit testing:
Thanks to raisins, it was only at the end of last week that I got to see the codebase at all
This shit smells of telecoms.
Filed under: Yoo-nit tess-ts?!
-
@loopback0 said in Making a mockery of unit testing:
This shit smells of telecoms.
Nope. Government agency.
-
@CarrieVS said in Making a mockery of unit testing:
Government agency.
Please accept my condolences.
-
@loopback0 said in Making a mockery of unit testing:
Please accept my condolences.
In fairness, until three months ago this department consisted entirely of project managers since all development and maintenance was outsourced. Now we have three devs, an architect, and some testers, all of us new, and we're basically making up the processes as we go along.
-
@CarrieVS And that, kids, is why having bad tests is worse than having no tests.
-
@CarrieVS said in Making a mockery of unit testing:
Government agency.
@CarrieVS said in Making a mockery of unit testing:
this department consisted entirely of project managers
Yes.
For the record, my previous IT job was (indirectly) for government agencies.
-
@CarrieVS said in Making a mockery of unit testing:
my company's first ever development team
ultimately going to redevelop a mission-critical system from scratch, in-house
Oh dear. I have a feeling that will not end well...
@CarrieVS said in Making a mockery of unit testing:
Government agency
The feeling just grew exponentially.
-
@loopback0
Soo, you wrote that conde, hence the condolences?
-
@Jarry At severe risk of ... no?
-
@CarrieVS said in Making a mockery of unit testing:
This is my first Java role, and my first with serious software (as opposed to front-end webdev or maintaining a squillion undocumented and mostly unconnected Perl scripts that run on, around, and vaguely in the vicinity of a third-party system), so I'm trying not to be judgemental.
I always feel the urge to stab people with the attitude that web development and dev ops isn't a serious job.
I do my best to avoid them because they strike me as working for a living.
-
@DogsB said in Making a mockery of unit testing:
I always feel the urge to stab people with the attitude that web development and dev ops isn't a serious job.
it's even worse then that attitude is apparently held by the web developers in question (and dev ops i guess, but ive never met a dev op that didn't act like devops was srs bsns
i mean how else can i explain webs decision to move all our joomla sites to wordpress, fail utterly on the conversion leading to our CRM at the time generating bad orders because our websites were feeding it bad data, pissing off our CRM hoster to the point that they fired us, implementing a wordpress based CRM, whjich they balles up to the point that we have no idea who our current subscribers are, or when they are up for renewal, or how many times we are charging them per year for their yearly membership.
and they apparently see nothing wrong with this state of affairs as it is "just a reporting issue"
-
@accalia said in Making a mockery of unit testing:
and they apparently see nothing wrong with this state of affairs as it is "just a reporting issue"
-
I would just like to mention that this thread has a near perfect title. Congratulations, @CarrieVS
-
@CarrieVS
Save yourself some time, skip all of the unit tests ;)
That's a protip from me to you :D
-
So apparently the
@
annotations in the highlighted java code are marked up as if they are mentions. If you click them, it will bring up profile pages for the users "test" and "before".
-
@DogsB said in Making a mockery of unit testing:
I always feel the urge to stab people with the attitude that web development and dev ops isn't a serious job.
I don't think that at all. It was a poor choice of words, in hindsight, but all I was trying to convey was that this is a very different kind of codebase to anything I've worked with before, which is why I was unsure what to expect and uncertain of my own judgement about it, even though it's not the first production code I've seen.
@Jaloopa said in Making a mockery of unit testing:
I would just like to mention that this thread has a near perfect title. Congratulations, @CarrieVS
Why thank you very- wait, almost perfect? Well what would you have called it, if you're such an expert?
-
Double posting here but I had another look at that same test class today.
I said that most of the tests were like that.
There are eight tests. Five of them are exactly like that. Two more are like that but don't actually assert anything or even call the mock method - one has that line commented out, another just doesn't have it at all.
That makes seven. The other one goes like this:
@Test public void testConstants() { assertEquals(Constants.FOO, Constants.FOO); assertEquals(Constants.BAR, Constants.BAR); // snip several dozen more }
I only glanced at that one yesterday and assumed it was testing that all the constants are equal to their hard-coded values, but I was mistaken.
Note that this is a separate Constants class (with no logic, just a list of public static final fields), not part of the class that's supposed to be under test.
The best tests I've found in there, by the way, mock the dependencies properly, set-up appropriate input, and run the actual methods, but don't bother asserting anything other than, at most, that the output isn't null.
-
@CarrieVS said in Making a mockery of unit testing:
all development and maintenance was outsourced
@loopback0 said in Making a mockery of unit testing:
For the record, my previous IT job was (indirectly) for government agencies.
@loopback0 said in Making a mockery of unit testing:
@Jarry At severe risk of ... no?
you worked indirectly for government agencies, she inherited outsourced code in a government agency....
IDK, it's a small world after all