I am writing junit test cases for an android project. I开发者_StackOverflow中文版 wrote junit for activity classes but I dont know how to write test cases for other classes that doesn't inherit from activity. Also, how can I link these classes (activity and non activity classes)?
examples:
public class A extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.someLayout);
B objectB = new B();
pbjectB.getString();
}
}
public class B{
public b(){
}
public String getString(){
return anyString;
}
}
In this example I am able to write junit test cases for class A but I am confused for class B.
For non-Activity classes, you can use standard junit 3 test classes. So make your test for B extend from junit.framework.TestCase
import junit.framework.TestCase;
public class BTest extends TestCase {
public void testGetStringIsNotNull() {
B subject = new B();
assertNotNull(subject.getString());
}
}
精彩评论