Your Unit Tests Should Mind Their Own Business
Join the DZone community and get the full member experience.Join For Free
as the unit testing debates continue on my project, i can't help but noticing that people are spending all sorts of time pontificating over the right way to unit test, without stepping back to consider what they're trying to achieve with unit testing. and because they don't know where they're going, they're not reaching any conclusions on how to get there. sound familiar?
there are actually a number of fabulous reasons for doing unit testing:
help us think about our design
in a way that we might not otherwise.
2. they lead to cleaner code because it's often easier to re-factor our code to be more testable then it is to write unit tests that exercise obfuscated or otherwise hard to access code.
3. they validate that our modules are behaving correctly.
4. they provide a safety net to ensure our existing modules continue to function properly as our application evolves.
these aren't all intuitive and so a lot of people will only consider #3 when they think about unit testing. and yet, what's interesting is that #3 typically provides the smallest return on our unit testing investment because we tend to find way more defects through #1 and #2 as we work out how to develop our tests, and then of course in #4 as we continue to change our code over the application's lifetime. and, unfortunately, by focusing only on #3, it can lead us to make some faulty decisions when we try to reason about the best way to do unit tests.
one of the most common questions people ask is whether to unit test private methods. i struggle to find any reasons why we should , but i do know a lot of reasons why we should not . i'm not saying that there's never a reason to unit test private methods, i'm just saying to make sure you understand what that reason is before you go to all the effort.
in my opinion, the best way to achieve our unit test objectives is by validating our class's interfaces. and when i think about interfaces , i'm thinking about the public methods we expose to other modules in our application. if we can validate that each of these methods is behaving as expected, then i'm going to feel satisfied that the class works correctly. for the purposes of unit testing , i don't really care how the method achieves the result, as long as it's the right one. i don't care if the method figures out the result in-line, or by calling external methods or through internal private methods. if i call calculator.add( 2, 2 ) and get 4 then i'm happy. if i get 5, there's a problem.
in fact, if we're being good developers and remembering to re-factor our code as the application evolves, then there's a good chance that the method we use to determine that result is going to change over the application's lifetime. that gets back to reason #4 for having unit tests - they provide a safety net to ensure that our modules continue to function as we make changes to them. if those unit tests are dependent on how we provide results, then it means that every time we decide to change our class's inner workings, we have to update our unit tests. and if we have to rewrite our unit tests, then what is happening to our safety net?
"being able to quickly create, move around, and change the functionality of private methods is vital to remaining agile" -- charles miller, the fishbowl
okay, sure if we change our public interfaces, we have to update our unit tests. but that makes sense. it means our interface specifications have changed and so our tests better be updated accordingly. changing our public interfaces should incur a significant cost because other code relies on them. in fact, one might even argue that having high costs (such as having to update our unit tests) provides a good reminder to us that it's in our best interest to keep our public interfaces stable.
on the other hand, the private parts of our implementations - those details that have been hidden behind our class's interface - should not be costly to change. if we give them the high cost of having to redo our unit tests then it puts up a barrier against refactoring that we really don't want. if we find that we're using the same logic in 3 different places in our class, we should be able to easily move that logic into a single private method and then simply re-run our unit tests as they already exist to make sure our class continues to function as expected. we shouldn't have to go write another unit test at this point because we haven't added any new functionality . we've simply refactored our existing code to be more maintainable.
"pure" unit tests with mock objects
another big question people ask about unit testing revolves around how pure our unit tests need to be . pure unit testing requires isolating an individual unit to validate its correctness.
so, for example, if our class interacts with external servers, we might create a stub to take the place of that server (such as a dummy server that simply listens on a port and returns the expected response). our unit test can then validate our class using the stub rather than the real server. this enables us to validate that our class is doing its part correctly without depending on the external server acting right.
but what about if our class interacts with other classes? how do we factor them out? a popular idea is to use mock objects . say we want to unit test our training class, which contains a method enroll( student ). rather than passing it a real student object, we could create a mockstudent object that implements the same interface as student. we then tell mockstudent what methods we expect to be called, such as getfirstname() and getlastname(), and what to return for them.
once we call training.enroll( mockstudent), our mock object validates that every method we expected to be called was, indeed, called. we even have the option of requiring that these methods are called in a specific order. if any of the expected methods were not called, or if any un expected methods were called, our test fails.
well, i really like the idea of mock objects - up until that last little bit. we seem to be back to the same concern that i have with unit testing private methods, and that is who cares which methods in student are called, much less in what order? all i care about is that the student was successfully enrolled in the training. maybe training.enroll() was only using the student's first and last name to set a unique id and the developer later realized that it's a bad idea to have data in our key, so updated the method to use a guid instead. should the training.enroll() unit test now break because that method is no longer calling student.getfirstname()? i don't think so. and, again, i think we're setting up unnecessary barriers to refactoring.
so, this isn't meant as a "never use mock objects" anymore than a "never test private methods." just a recommendation to consider your unit testing objectives and what's going to best serve them before blindly diving in to the latest cool thing you just read on some schmo's blog.
here are some great resources if you'd like to learn more:
» the flawed theory behind unit testing (michael feathers)
» testing private methods with junit and suiterunner (great stuff, even if you're not using these tools)
» mocks aren't stubs (martin fowler)
reposted from: the hacker chick blog
Opinions expressed by DZone contributors are their own.