java - Simplest way to unit test an android library application? -


sorry if bit of vague question, im struggling find single solid example on how unit testing (isolated testing) android...

here example of i'm wanting achieve:

// 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 1 project contains models , logic, project contains tests said library. there no front end or ui, want bare minimum able test methods work in isolation.

as long "library" doesn't contain references resources in android sdk, there isn't more regular unit testing. in eclipse workspace, have main project myandroidlibproject, create new java project (e.g. myandroidlibprojectunittests). inside here, create ordinary unit tests referring calculator class in main project (just make sure main project added build path of test project).

you might find additional information in similar question asked myself earlier, answer question.

updated 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 test of own semester class (a simple data class representing semester). semester located inside android project mymainproject (but class doesn't contain references android sdk). semestertest located inside test project mytestproject (an ordinary java project), both mymainproject , mytestproject being in same workspace (and since semestertest has same package name semester, don't need special import statement reference semester either). mytestproject has mymainproject added build path (junit.jar added build path, happens automatically, @ least in eclipse think).

so can see, nothing ordinary unit test (junit 4, have mentioned it). hope helps.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -