I am a totally noob in writing jUnit tests. I have an Android program and have to write tests for it. I know how to and i wrote tests with robotium, but now i need functional tests and i don’t where to start. I have one function from my code:
private void onCoordinatesUpdate(Location loc){
List<Event> list = wc.requestEvents(loc.getLatitude(), loc.getLongitude());
if(list != null){
adapter = new EventsArrayAdapter(this.getApplicationContext(), R.id.eventsView, (ArrayList<Event>)list);
ev.setAdapter(adapter);
}
}
I have to write test for for it. The thing which makes problem for me, is that i have no idea how to write test, which would detect if expression in IF sentence is changed for example from “list != null” to “list == null”. The only idea of test which i can think of is this:
public void testCheckInternet(){
assertTrue(mClassToTest.checkInternet());
}
But his test only checks if device is connected to internet and that’s it. I don’t need anybody to write test for me, but maybe somebody could at lest show me the way 🙂 how to start 🙂 thanks in advance.
The method you provided is private and as a consequence is hard to test with JUnit. When doing unit testing with JUnit you should only test public methods. The private methods will be tested indirectly.
If there are private methods in your classes which you would like to test explicitly you should rethink your class design. One solution would be to extract this private method into a new class and make the method public.
Generally speaking there is code which is testable and there is code which is not. Test driven development is a technique which helps to design testable code. A good starting point is the JUnit documentation.