Always separate logic from user interface. This is generally good practice for standard applications, but is even more important in mobile applications, typically in Android applications. No matter how tempting it is to incorporate some logic into the UI, don't do it: you will end up paying for it in the long run.
Testing in Android
For a quick guide on how to set up your testing environment, please read last week's tutorial by Cristi.
How JUnit works
(Students already familiar with JUnit may skip that section)
JUnit is a java testing framework. In short, it will allow you to write tests for your code and run them. JUnit's main building block is the TestCase class: all test cases inherit from this class. Each TestCase subclass includes test methods. The convention to follow is to prefix these methods by 'test' (it will also allow JUnit to find your test methods automatically - otherwise they have to be specified manually). A typical JUnit test run is executed as follows:
You might wonder what's the point of such a structure. This guarantees that test methods are independent from one another and that they don't interfere (as a by-product, it also forces the programmer to think of their program as a bunch of independent modules).
Finally, JUnit provides a way to organize TestCases into TestSuites. This allows the programmer to organize their tests hierarchically.
What's different in Android
Not much really! For testing in Android, you should always use android.test.AndroidTestCase instead of junit.framework.TestCase. The difference between the two is that the AndroidTestCase has access to the Context object. As a matter of fact, you could potentially (although we don't advise it) use junit.framework.TestCase for testing the parts of your program that don't rely on the Android framework.
The other difference is that there is no way, unless you want to handle callbacks from the test runner, for you to display the test results on the device (or emulator). However, we don't care about this, the JUnit report one can get from Eclipse is more than enough for our needs.
How to use the test interface
At the beginning of the project, we provided you with a testing interface that will allow programmatic access to your application when implemented. You should make it a point to implement this interface thoroughly as your grade will mostly depend on the results of the tests we will run through that interface.
The interface allows to test functionality without the need for a server and while bypassing the GUI. This is immensely useful as UI testing is very difficult.
We now give a brief tour of the interface.
Below is a rough sketch of a test method that we will use to test your implementation...
Tips for implementing the interface
Stuff that may save you some time
As you know, the JUnit testing framework on Android is still pretty new. Here a some of the issues we ran into while implementing our test interface and our testing framework: