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", "[email protected]");
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?
unit tests can be written under test java package, and instrumental tests can be written under androidTest package. You can then access UI widgets using Espresso ViewMatchers and then you can apply actions on them using ViewActions . Check documentation for further help. Show activity on this post.
Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.
gradle file. In your test, you start by making a call to init() from Intents. This will start recording the fired Intents, and is required before each test where you want to verify Intent activity. For each time you call init() , you must follow it up with a call to release() at the end of the test.
The class you inherit, ActivityInstrumentationTestCase2
, allows you to mock Intent
s. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With