Archive‎ > ‎Fall 2009‎ > ‎Course Project‎ > ‎Buddy Suite‎ > ‎Tutorial #2‎ > ‎

Junit Testing


Junit testing is still not well documented in the Android SDK, so this tutorial will be updated in time, as the APIs are changing. 

The new version of Android SDK, 1.6 and the corresponding Eclipse plugin make it a bit easier to use Eclipse to set up a Test case suite. 

Here is the procedure to set up a test case in Android 1.6
  • Create a new Android Project called Sweng and select the GooleAPI 1.6 target. 
  • Create a default Activity
  • Add a new class to the same package as the activity. This can be any class which has a method you want to test. Let's call this class Adder and add this method: 
    • static int add(int a, int b);
  • Create a new Android Test Project called SwengTest. Note that this is a different project type.
  • Select the project test target (the project you want to test), in this case it would be Sweng. 
  • Right click on the package and click New->JunitTest. This will create a new Junit Test. Name it AdderTest. 
  • You can add a simple test method to the code of this class:
public class AdderTest extends TestCase {

public void testAdd() {
assertEquals(Adder.add(1,2), 3);
}
}

  • Right click on the class and select Run as Android Junit Test. 
  • The test should show as successful in the Junit Perspective. 
Here are two more complex examples http://www.danielswisher.com/ and http://codinghard.wordpress.com/ that follow the steps required for Android 1.5