Share
Introduction
Since I’ve been toying with Android development in the last few months, I’ve been focusing on the Android SDK side of things instead of fundamentals such as project set up, code cleanliness, and testing. This post starts rectifying that situation, and is an introduction to unit testing Java code using JUnit & Eclipse, with the context being that of Android development being done by a .NET guy. Should be fun, eh?
We’ll assume that we have everything that’s needed to develop for Android on a Windows machine. If not, you can refer to the Android SDK docs here to get that up and going.
We’ll also assume that you’ve installed the latest version of Eclipse, which comes with JUnit already available, meaning we don’t have to install it.
Since I’ve been toying with Android development in the last few months, I’ve been focusing on the Android SDK side of things instead of fundamentals such as project set up, code cleanliness, and testing. This post starts rectifying that situation, and is an introduction to unit testing Java code using JUnit & Eclipse, with the context being that of Android development being done by a .NET guy. Should be fun, eh?We’ll assume that we have everything that’s needed to develop for Android on a Windows machine. If not, you can refer to the Android SDK docs here to get that up and going.
We’ll also assume that you’ve installed the latest version of Eclipse, which comes with JUnit already available, meaning we don’t have to install it.
Starting Up
Our first test is to create a new Java project. After that, we add two new interfaces to the src folder, IBoard and IPlayer.
1 |
package com.personal.UnitTestingFun; |
Writing Tests
We will segregate our tests into a separate source folder called tests. To do this, right click on the root note in the package explorer and select New –> Source Folder.
Once we’ve done this our project should look like below.

Now to create our first JUnit test file, right click on the tests source folder and select New –> JUnit Test Case.

We go ahead and enter the options for our new test case, namely the package, the name of the test class, and the class under test.

You’ll notice that this dialog isn’t letting us continue onwards, namely because the class we want to test doesn’t exist yet. To get around this, we’ll go ahead and create an empty class called TicTacToeBoard in our src folder and indicate that implements the IBoard interface. Once we do this, Eclipse will yell at us because our new class doesn’t have the needed methods; we’ll let Eclipse create those for us as well, giving us a class definition as below.
1 |
package com.personal.UnitTestingFun; |
Now we can try to create our test case again, and sure enough, Eclipse lets us finish the job.

Clicking Next brings us to a new dialog where Eclipse asks us what methods on the class under test we actually want to write tests for.

Looks like a nifty feature, but since we want to just write enough code for a test and then for that test to pass, we’ll turn down Eclipse’s offer to help us and just click Finish. That gives us the beautiful test class below.
1 |
package com.personal.UnitTestingFun; |
So finally we can write our test. JUnit uses method attributes just like NUnit does to indicate that a given method is a test method. Our test looks like below.
1 |
@Test |
To run the test, all we have to do is right click anywhere in the test body and select Run As –> JUnit Test.

This is actually pretty hot, given that the same functionality isn’t available integrated into Visual Studio without a relatively pricey tool (unless you’re using MSTest, which we determined wasn’t as good as NUnit in the above post). Our test results appear in the left-hand set of tabs as below.

Again, pretty hot. We go ahead and make our tests pass by creating a backing store for the rows property and setting it to 3 when a TicTacToeBoard class is created; we also do the same thing for the columns property, since it’s exactly the same.
Our next test ensures that we have two players, and appears below.
1 |
@Test |
We fail with a NullPointerException because we haven’t written the code to handle players yet, which is exactly as expected. To get our test to pass, we modify our constructor to instantiate and populate our player list, which is held in a backing store for the Players property.
1 |
public TicTacToeBoard() { |
1 |
private ArrayList<IPlayer> _players; |
With this magic, our test passes.

Collection Test
Our next test is a test to ensure that our two players have the correct names, namely “Nought” and “Cross”. The tests to verify this appear below.
1 |
@Test |
1 |
@Test |
Note that the CollectionUtils functionality is provided by the Apache Commons-Collection library, and can be located here.
To make the above tests pass, we modify our Player class to accept a string representing the name of the player being created. The new class definition appears below.
Then, we modify our tic tac toe board implementation to pass in the right player names when it’s constructed.
1 |
public TicTacToeBoard() { |
Exception Test
Our final test will ensure that an out of bounds move will raise an exception. The test appears below.
1 |
@Test(expected=IndexOutOfBoundsException.class) |
Note that we augment the test attribute to indicate that we are expecting an exception. To get this test to pass, we implement the MakeMove method as below.
1 |
@Override |
And with that, our test passes, and we have 6 green lights.

Closing Thoughts
The integration of JUnit into Eclipse was pretty straightforward to work with, and wasn’t too much of a departure from working with NUnit in Visual Studio. I definitely missed the fluent interface; that said, there seems to be a way to get the same kind of functionality using custom matchers with JUnit 4.4+. I simply wasn’t able to get it to work in the time I gave myself for this post. With a working fluent interface, the unit testing picture in Java is better than the same picture in the .NET space from a cost perspective. MSTest equalizes things a bit, but you still need at least the Professional SKU for Visual Studio, and that is definitely not free.Bottom line, if cost is an issue and everything else is the same, Java + Eclipse win from a unit testing perspective.
Thanks BareBones Coder for this article!!!





