I Don’t TDD: Pragmatic Testing With Java
Testing real-life applications, not just Kata-like code snippets, is rough! In this video tutorial, learn how to pragmatically test a Java web application.
Join the DZone community and get the full member experience.
Join For FreeWe're building a Google Photos clone, and testing is damn hard!
How do we test that our Java app spawns the correct ImageMagick processes or that the resulting thumbnails are the correct size and indeed thumbnails, not just random pictures of cats? How do we test different ImageMagick versions and operating systems?
What’s in the Video
00:00 Intro
We start the video with a general overview of what makes testing our Google Photos clone so tricky. As in the last episode, we started extracting thumbnails from images, but we now need a way to test that. As this is done via an external ImageMagick process, we are in for a ride.
01:05 Setting Up JUnit and Writing the First Test Methods
First off, we will set up JUnit 5. As we're not using a framework like Spring Boot, it serves as a great exercise to add the minimal set of libraries and configuration that gets us up and running with JUnit. Furthermore, we will write some test method skeletons, while thinking about how we would approach testing our existing code and taking care of test method naming, etc.
04:19 Implementing ImageMagick Version Detection
In the last episode, we noticed that running our Java app on different systems leads to unexpected results or just plain errors. That is because different ImageMagick versions offer a different set of APIs that we need to call. Hence, we need to adjust our code to detect the installed ImageMagick version and also add a test method that checks that ImageMagick is indeed installed, before running any tests.
10:32 Testing Trade-Offs
As is apparent with detecting ImageMagick versions, the real problem is that to reach 100% test coverage with a variety of operating systems and installed ImageMagick versions, you would need a pretty elaborate CI/CD setup, which we don't have in the scope of this project. So we are discussing the pros and cons of our approach.
12:00 Implementing @EnabledIfImageMagickIsInstalled
What we can do, however, is make sure that the rest of our test suite only runs if ImageMagick is installed. Thus, we will write a custom JUnit 5 annotation called EnabledIfImageMagickIsInstalled
that you can add to any test methods or even whole classes to enable said behavior. If ImageMagick is not installed, the tests simply will not run instead of display an ugly error message.
16:05 Testing Successful Thumbnail Creation
The biggest problem to tackle is: How do we properly assert that thumbnails were created correctly? We will approach this question by testing for ImageMagick's exit code, estimating file sizes, and also loading the image, and making sure it has the correct amount of pixels. All of this with the help of AssertJ and its SoftAssertions to easily combine multiple assertions into one.
23:59 Still Only Works on My Machine
Even after having tested our whole workflow, we still need to make sure to call a different ImageMagick API for different versions. We can quickly add that behavior to support IM6 as well as IM7, and we are done.
25:53 Deployment
Time to deploy the application to my NAS. And this time around, everything works as expected!
26:20 Final Testing Thoughts
We did a fair amount of testing in this episode. Let's sum up all the challenges and pragmatic testing strategies that we learned about.
27:31 What’s Next
We'll finish the episode by having a look at what's next: multithreading issues!
See you in the next episode.
Opinions expressed by DZone contributors are their own.
Comments