Reflections on Testing with JUnit

junitLogoI’ve been coding a screensaver for the past 4 months. I honestly thought the project would be finished and out on the market two weeks after the main implementation of the code was finished.

Development was finished a month ago. But I can’t release it. I’m still reluctantly discovering new ways the screensaver could potentially crash on users’ computers.

I noticed that my screensaver would crash for any number of reasons:

  • The input images had been relocated.
  • The incorrect file format was used as input.
  • The user updated user preferences that broke the program’s execution.

I fixed those three bugs but there could be many many more.

Prior to writing this post I had only heard of Unit Testing. I had never applied unit testing to my projects. This week I had the opportunity to implement JUnit testing on a few programs.

“The ideal testing tool should give us confidence in our program exactly proportional to the confidence it deserves, so that we neither pass on a program containing errors nor continue probing a program which is error-free.

The Psychology of Computer Programming

Black box testing

JUnit allows programmers to specify tests on individual methods in their code. This week I applied JUnit tests to two Project Euler programming problems:

  • Summing multiples of 3 and 5.
  • Summing a sequence of Fibonacci numbers.

Completed WODs

Applying the JUnit code via IntelliJ’s built-in unit testing was very easy. The first time I attempted to clone a repo, create a testing branch, apply tests with JUnit, and push the changes I did it in 14 minutes and 28 seconds. Then I repeated the first WOD 2 times and managed to get my time down to 10 minutes.

I completed the second WOD in 10 minutes and 41 seconds. I did this WOD twice to exercise my ability to run the test configuration. The first time I ran this WOD I had trouble getting the test code to run. Every time I ran the code I would run the code execution. After doing some research online I found the keyboard shortcut to select which configuration to run. My second run of the second WOD was completed in 10 minutes.

This slideshow requires JavaScript.

Insights gained from WODs

After completing the WODs a few times I had the following insights:

  • Always run the edit configuration in Intellij with the keyboard shortcut: Ctrl+Option+R
  • All methods to be tested must be public or protected.
  • Always add to the tests folder.
  • Always run Inspect code on the test code.

Author: David Neely

Professional Software Developer. Technology and Web Coordinator at the University of Hawaii's Manoa Career Center.