简体   繁体   中英

Simplest way to unit test an android library application?

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.

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM