Writing and Testing FizzBuzz in Eclipse

I put my Java, Eclipse, and JUnit skills to a simple test today - I wrote the "FizzBuzz" program. FizzBuzz goes through the numbers 1-100. If the number if divisible by 3, it prints out "Fizz." If the number is divisible by 5, it prints "Buzz." If the number is divisible by both 3 and 5, it prints "FizzBuzz." In all other cases, it just prints the number. Obviously this is a simple program. It was a good way to ease back into writing Java (which I abandoned for C# a few years ago), get a feel for the features in the newest version of Eclipse, and introduce myself to JUnit.

It took me about 20 minutes to complete the program and unit testing.

As expected, the Java code was pretty straightfoward. I did spend a little time redoing some tasks to make sure I was taking full advantage of Eclipse, like creating the main method and managing packages. At first, I wrote everything in the main method and ran the code to check the output. Everything seemed to work fine, so I moved on to unit testing.

At this point I remembered that JUnit requires a method that takes an input and returns a value. Since my main method just printed to the screen, it wouldn't work. I split my code into a separate method that main could call.




Now I was REALLY ready for unit testing.
How did that go again? Assert something? What order do the parameters go in? I asked my partner in class, who kindly reminded me. AssertEquals - and you can press Control-Space if you forget. Oh yeah, thanks Sergey.

I wrote four test cases and ran it, but it failed. What had I done wrong? I checked my output again to ensure that "Fizz", "Buzz", and "FizzBuzz" were printing in the appropriate places. They were, so the problem had to be with the test. The error told me that "Test for Fizz" was failing, so I looked at that line.



It made sense to me, but I used control-space again to double check the parameters. Suddenly it was obvious - AssertEquals needed the output, and I was passing it the input. Calling the NumToString method in my FizzBuzz program fixed the error, and my unit test passed.




As basic as it seemed, writing a simple program like FizzBuzz turns out to be a good way to get acquainted (or re-acquainted) with Java, Eclipse, and JUnit. The mistake I made in my test cases was easy to spot and fix here. In a more complicated program where the correct output was less obvious, it could have easily led to a lot of frustration and wasted time. The lesson learned here is that even if it doesn't seem useful, taking twenty minutes to write up a trivial piece of code can save time later if you aren't entirely comfortable with the features being used.

Comments

  1. Learning the Eclipse shortcuts will come in handy one day. I use them all the time at my company. I never used CTRL-SPACE yet, though. Maybe this week.

    ReplyDelete
  2. Casinos like Harrah's Las Vegas, Caesars, Rivers Casino
    Looking for casino and dining options near Harrah's Las Vegas? Caesars Casino in 대구광역 출장안마 Reno 동해 출장안마 and Golden Nugget 파주 출장샵 offers more than 거제 출장안마 1,300 slots, table games, 강원도 출장마사지

    ReplyDelete

Post a Comment

Popular posts from this blog

Working with Esper: Problems and Solutions

WattDepot Katas