How do I write an android JUnit test when my activ

2019-04-18 07:08发布

I am writing an android Junit test for a class that relies on extras passed to it through an Intent. I was able to get the class working properly, but I would still like to know how to write a unit test for such a class, as the test still fails.

public class AddClassEvent extends Activity{
 private String eventType;

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bundle extras = getIntent().getExtras();
  final String cNo = extras.getString("CourseNum");

  // create a model instance
  final StudentDBModel model = new StudentDBModel(this);

  setContentView(R.layout.add_class_event);
 .....
 .....
         }
     }

The test class looks like...

public class AddClassEventTest extends ActivityInstrumentationTestCase2<AddClassEvent>{
 private StudentDBModel model = null;
 private RenamingDelegatingContext context = null;

 public AddClassEventTest() {
  super("com.UI", AddClassEvent.class);
 }

 /**
  * This method is called before each test.
  */
 @Override
 public void setUp() {
  context = new RenamingDelegatingContext(getActivity(), "test_");
  model = new StudentDBModel(context);
 }

 /*
  * This function will test addNewClassEvent() from StudentDBModel
  */
 public void testAddNewClassEvent(){

  ContentValues courseValues = new ContentValues();
  courseValues.put("CourseId", "60-415");
  courseValues.put("CourseName", "Advanced Database Design");
  courseValues.put("Section", "1");
  courseValues.put("Location", "Erie");
  courseValues.put("Credit", "3");
  courseValues.put("ProfEmail", "rfortier@uwindsor.ca");
  courseValues.put("Website", "cs.uwindsor.ca");

  model.addNewCourses(courseValues);

  int numEventsBefore = model.getNumClassEvents();

  ContentValues values = new ContentValues();

  values.put("EventName", "Assignment 1");
  values.put("CourseId", "60-415");
  values.put("EventType", "Assignment");
  values.put("EventWeight", "8");
  values.put("DueDate", "10/20/2010");

  model.addNewClassEvent(values);

  int numEventsAfter = model.getNumClassEvents();

  assertEquals(numEventsBefore + 1, numEventsAfter);
 }
}

The problem is, the extra that I am passing to the class AddClassEvent is a PK for my DB that is created in another class and passed to AddClassEvent through an Intent. Whenever I run the test I get a NULL Pointer Exception on the on the line:

final String cNo = extras.getString("CourseNum");

How do I create the info from the extra in the Junit Test? Is there a way to get this test to work? I have searched extensively and can't find an answer. Is there some way to falsely create the extras in the Junit test so that it thinks it is being created by the other class? If so, could someone please show me how?


OK so I have tried to take your advice and I have changed my setUp function to:

@Override
public void setUp() {
    context = new RenamingDelegatingContext(getActivity(), "test_");
    model = new StudentDBModel(context);
    Intent addEvent = new Intent();
    addEvent.setClassName("com.UI", "com.UI.AddClassEvent");
    addEvent.putExtra("CourseNum", "60-415");
    setActivityIntent(addEvent);
    getActivity();
}

but I am still getting a NULL Pointer exception. Is my syntax wrong? Any suggestions?

2条回答
我想做一个坏孩纸
2楼-- · 2019-04-18 07:37

The class you inherit, ActivityInstrumentationTestCase2, allows you to mock Intents. From the documentation:

You can inject custom Intents into your Activity (see setActivityIntent(Intent)).

The documentation for setActivityIntent() further clarifies:

Call this method before the first call to getActivity() to inject a customized Intent into the Activity under test.

If you do not call this, the default intent will be provided. If you call this after your Activity has been started, it will have no effect.

So you should be able to place a call to this method inside your setUp() before your call to getActivity(). You can pass in a mocked Intent into setActivityIntent like you mentioned -- just build a fake Intent with extras that you'd expect the Activity to see.

查看更多
再贱就再见
3楼-- · 2019-04-18 07:38

OK, I figured out my mistake! The code for setUp was just in the wrong order. It should look like:

@Override
public void setUp() {
    Intent addEvent = new Intent();
    addEvent.setClassName("com.UI", "com.UI.AddClassEvent");
    addEvent.putExtra("CourseNum", "60-415");
    setActivityIntent(addEvent);
    context = new RenamingDelegatingContext(getActivity(), "test_");
    model = new StudentDBModel(context);
}

I was calling getActivity() twice and the first call was ahead of the Intent. By using the correct order, the test runs fine. Thanks for the help McStretch.

查看更多
登录 后发表回答