Simplest Way To Unit Test An Android Library Application?
Solution 1:
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:
importstatic org.junit.Assert.*;
import org.junit.Test;
publicclassSemesterTest
{
@TestpublicvoidaNewSemesterShouldHaveANegativeId()
{
Semestersemester=newSemester(2010, SemesterType.FALL);
assertEquals(-1, semester.getInternalId());
}
@TestpublicvoidtoStringShouldPrintSemesterTypeAndYear()
{
Semestersemester=newSemester(2010, SemesterType.FALL);
assertEquals(SemesterType.FALL + " 2010", semester.toString());
}
@TestpublicvoidequalityShouldOnlyDependOnSemesterTypeAndYear()
{
SemesteraSemester=newSemester(2010, SemesterType.FALL);
aSemester.setInternalId(1);
SemesteranotherSemester=newSemester(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.
Post a Comment for "Simplest Way To Unit Test An Android Library Application?"