| Start |
|---|
| What is Unit Testing? This is a bit of a primer. It's mostly put it in because documentation is forced to have an example, even if it's intended as a stub article. If you already know unit-testing basics, feel free to skip forward to the remarks, where specific frameworks are mentioned. Unit testing is ensuring that a given module behaves as expected. In large-scale applications, ensuring the appropriate execution of modules in a vacuum is an integral part of ensuring application fidelity. Consider the following (trivial) pseudo-example: So this example is trivial; Now, assume, somewhere along the way in a release, for reasons unknown, the Clearly, this breaks expected behavior. But, because of the arduous processes involved with execution of the Eventually, after discussion between the teams and collaboration, the issue is traced, and the problem solved. But, it begs the question; how could this have been prevented? There are two obvious things: Tests need to be automatedOur reliance upon manual testing let this bug go by unnoticed far too long. We need a way to automate the process under which bugs are introduced instantly. Not 5 weeks from now. Not 5 days from now. Not 5 minutes from now. Right now. You have to appreciate that, in this example, I've expressed one very trivial bug that was introduced and unnoticed. In an industrial application, with dozens of modules constantly being updated, these can creep in all over the place. You fix something with one module, only to realize that the very behavior you "fixed" was relied upon in some manner elsewhere (either internally or externally). Without rigorous validation, things will creep into the system. It's possible that, if neglected far enough, this will result in so much extra work trying to fix changes (and then fixing those fixes, etc.), that a product will actually increase in remaining work as effort is put into it. You do not want to be in this situation. Tests need to be fine-grainedThe second problem noted in our above example is the amount of time it took to trace the bug. The product manager pinged you when the testers noticed it, you investigated and found that the The same point I made above about the quantity and difficulty of this trivial example hold here. Obviously anyone reasonably well-versed with Java could find the introduced problem quickly. But it's often much, much more difficult to trace and communicate issues. Maybe the In order to mitigate against this, we want rigorous testing on as fine a level as possible (you also want coarse-grained testing to ensure modules interact properly, but that's not our focal point here). We want to rigorously specify how all outward-facing functionality (at minimum) operates, and tests for that functionality. Enter unit-testingImagine if we had a test, specifically ensuring that the There's some omissions made here as to how to implement these tests, but those are covered in the framework-specific documentation (linked in the remarks). Hopefully, this serves as an example of why unit testing is important. RemarksUnit Test FrameworksThere are numerous frameworks available for unit testing within Java. The most popular option by far is JUnit. It is documented under the following: JUnit4 (opens new window) - Proposed tag for JUnit4 features; not yet implemented. Other unit test frameworks do exist, and have documentation available: Unit Testing ToolsThere are several other tools used for unit testing: Mockito (opens new window) - Mocking (opens new window) framework; allows objects to be mimicked. Useful for mimicking the expected behavior of an external unit within a given unit's test, as to not link the external unit's behavior to the given unit's tests. JBehave - BDD (opens new window) Framework. Allows tests to be linked to user behaviors (allowing requirement/scenario validation). No documents tag available at time of writing; here (opens new window) is an external link. |
Read Article
https://develop.horje.com/learn/1434/reference