0

Im trying to test next code:

public class MessagerTest extends TestCase  {
Messager mClassToTest;
Context mArg1;
String mArg2;
protected void setUp() throws Exception {
     mClassToTest=new Messager();
     mArg1= getTestContext();
     mArg2= "5556";
    super.setUp();
}

protected void tearDown() throws Exception {
    super.tearDown();
}

 public void testscreateConnection(){
     assertTrue(mClassToTest.createConnection(mArg1, mArg2));
 }

 public void testsadd()
 {
     assertEquals(11, mClassToTest.add(5, 6));
 }

 /**
  * @return The {@link Context} of the test project.
  */
 private Context getTestContext()
 {
     try
     {
         Method getTestContext = ServiceTestCase.class.getMethod("getTestContext");
         return (Context) getTestContext.invoke(this);
     }
     catch (final Exception exception)
     {
         exception.printStackTrace();
         return null;
     }
 }
 }

and code i am testing is

public boolean createConnection(Context context, String number)
{
    DatabaseHandler db = new DatabaseHandler(context);

    contact = db.getContactByNumber(number);
    String id = String.valueOf(contact.getID());

    if(id.equals("0"))
    {
        // send message
        PendingIntent pi = PendingIntent.getActivity(context, 0,
                new Intent(context, CreateConnection.class), 0);

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm",
                Locale.UK);

        currentDateandTime = sdf.format(new Date());

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(number, null, "1|031|", pi, null);

        // add to db
        db.addContact(new Contact("", number, "", 
                "", 2, "40", currentDateandTime)); 

        return true;
    }else{
        return false;
    }
}

for some reason when I run test, for testscreateConnection, it seem to fail on the first line at contact = db.getContactByNumber(number); with a NullPointerException. Fail happens on the first line of the function SQLiteDatabase db = this.getReadableDatabase();

getContactByNumber code:

 public Contact getContactByNumber(String number)
    {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                KEY_NAME, KEY_PH_NO, KEY_PUB_KEY, KEY_IDENTIFIER, KEY_DELETE,
                KEY_STATUS, KEY_DATETIME }, KEY_PH_NO + "=?",
                new String[] { number }, null, null, null,
                null);

        if(cursor.getCount() > 0)
        {
            if(cursor != null)
            {
                cursor.moveToFirst();
            }

            Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getString(4), Integer.parseInt(cursor.getString(5)),
                    cursor.getString(6), cursor.getString(7));

            // return contact
            return contact;
        }else{
            Contact contact = new Contact();
            return contact;
        }
    }

How should I go about solving this issue? It kind of makes me think that database is not present at the test version?

D.J
  • 1,439
  • 1
  • 12
  • 23
  • Hi, are you sure that, the context you are getting is appropriate for android? Have you tried using `AndroidTestCase` instead of Junit3 – minhaz Apr 25 '13 at 23:38
  • After trying AndroidTestCase i have received - android.database.sqlite.SQLiteException: unable to open database file – user1652382 Apr 26 '13 at 00:51
  • Updating testing method to 'ActivityTestCase' and setting - "mArg1= getInstrumentation().getTargetContext();" solved it. – user1652382 Apr 26 '13 at 00:55
  • This simply means that you have issue with context. Your code should pass with AndroidTestCase. FYI, activity context and application context is not same. i would recommend you to understand both and update code based on that. – minhaz Apr 26 '13 at 01:08

1 Answers1

0

Had to update first lines in the test case, the TestCase to ActivityTestCase and context to mArg1= getInstrumentation().getTargetContext();

public class MessagerTest extends ActivityTestCase  {
Messager mClassToTest;
Context mArg1;
String mArg2;
protected void setUp() throws Exception {
     mClassToTest=new Messager();
     mArg1= getInstrumentation().getTargetContext();
     mArg2= "5558";
    super.setUp();
}
D.J
  • 1,439
  • 1
  • 12
  • 23