user484155
user484155

Reputation:

Android Junit test

I am writing junit test cases for an android project. I 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.

Upvotes: 0

Views: 697

Answers (1)

avh4
avh4

Reputation: 2655

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());
  }
}

Upvotes: 1

Related Questions