4

Sorry if this is a bit of a vague question, however im struggling to find a single solid example on how to do unit testing (isolated testing) with Android...

Here is an example of what i'm wanting to achieve:

// Some class
class Calculator
{
    public int Add(int a, int b) { return a+b; }
}


// Simple test
import org.junit.Assert;
import org.junit.Test;

class CalculatorTests
{
    @Test
    public void should_add_numbers_correctly()
    {
        Calculator calculator = new Calculator();
        int expectedResult = 5 + 5;
        int actualResult = calculator.Add(5,5);
        Assert.assertEqual(actualResult, expectedResult);
    }   
}

So one project contains models and logic, then another project contains tests for said library. There is no front end or UI, so I want to do the bare minimum to just be able to test that my methods all work in isolation.

Grofit
  • 17,693
  • 24
  • 96
  • 176
  • Just incase it confuses anyone else, it appears that to use the normal JUnit style testing @Test you have to test on the normal JVM, so you basically test your code as if it were J2SE. However the alternative approach which I couldnt get working is where you run your tests on the emulator and then you need to replace a load of guff and extend classes as it runs with Junit 3 not 4... This link helped me understand it alot better: http://daverog.wordpress.com/2009/12/14/why-android-isnt-ready-for-tdd-and-how-i-tried-anyway/ – Grofit Nov 19 '10 at 08:11

1 Answers1

2

As long as your "library" doesn't contain any references to resources in the Android SDK, there isn't anything more to this than regular unit testing. In your Eclipse workspace, say you have your main project MyAndroidLibProject, you simply create a new Java Project (e.g. MyAndroidLibProjectUnitTests). Inside here, you create ordinary unit tests referring to the Calculator class in your main project (just make sure that your main project is added to the build path of your test project).

You might also find some additional information in a similar question I asked myself earlier, as well as the answer to that question.

Updated with example:

import static org.junit.Assert.*;
import org.junit.Test;

public class SemesterTest
{
    @Test
    public void aNewSemesterShouldHaveANegativeId()
    {
        Semester semester = new Semester(2010, SemesterType.FALL);
        assertEquals(-1, semester.getInternalId());
    }

    @Test
    public void toStringShouldPrintSemesterTypeAndYear()
    {
        Semester semester = new Semester(2010, SemesterType.FALL);
        assertEquals(SemesterType.FALL + " 2010", semester.toString());
    }

    @Test
    public void equalityShouldOnlyDependOnSemesterTypeAndYear()
    {
        Semester aSemester = new Semester(2010, SemesterType.FALL);
        aSemester.setInternalId(1);

        Semester anotherSemester = new Semester(2010, SemesterType.FALL);
        anotherSemester.setInternalId(2);

        assertEquals(aSemester, anotherSemester);
    }
}

The above is a test of my own Semester class (a simple data class representing a semester). Semester is located inside my android project MyMainProject (but the class itself doesn't contain any references to the Android SDK). SemesterTest is located inside my test project MyTestProject (an ordinary Java project), with both MyMainProject and MyTestProject being in the same workspace (and since SemesterTest has the same package name as Semester, I don't need any special import statement to reference Semester either). MyTestProject also has MyMainProject added to its build path (junit.jar is also added to the build path, but this happens automatically, at least in Eclipse I think).

So as you can see, this is nothing but a completely ordinary unit test (JUnit 4, just to have mentioned it). Hope this helps.

Community
  • 1
  • 1
Julian
  • 20,008
  • 17
  • 77
  • 108
  • Im using IntelliJ X EAP at the moment, prefer the IDE to Eclipse. Although for some reason trying to use the above code just doesn't seem to work. Will make sure the class path etc is setup right. – Grofit Nov 17 '10 at 08:18
  • It also seems to be complaining about me not having an AndroidManifest.xml file, which the IDE hasnt created for me and to my knowledge i should not need if im just using a library and a test, as there is no activity or action in a library project. Anyway this is IDE specific and not really related to the question at hand, if i can solve that issue and make sure it all works then i will mark you as the answer – Grofit Nov 17 '10 at 08:27
  • @Grofit: I have no experience with IntelliJ, so I cannot give you any concrete advice there. However, the #1 prerequisite for this to work, is that the project you're going to test is set up and working correctly. Also, if you give me a couple of minutes, I'll update my answer with a concrete example of a unit test from my own project, just to show that there is nothing special to it. – Julian Nov 17 '10 at 08:56
  • Im still having problems but I think its IntelliJ related, your answer is more than concise enough for other people so thanks alot for taking the time to help! – Grofit Nov 18 '10 at 08:03