【问题标题】:Android TestCase Junit3Android 测试用例 Junit3
【发布时间】:2013-04-25 22:47:43
【问题描述】:

我正在尝试测试下一个代码:

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

我正在测试的代码是

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

由于某种原因,当我对testscreateConnection 运行测试时,它似乎在contact = db.getContactByNumber(number); 的第一行以NullPointerException 失败。失败发生在函数SQLiteDatabase db = this.getReadableDatabase();的第一行

getContactByNumber 代码:

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

我应该如何解决这个问题? 这让我觉得数据库在测试版本中不存在?

【问题讨论】:

  • 嗨,你确定,你得到的上下文适合安卓吗?您是否尝试过使用 AndroidTestCase 而不是 Junit3
  • 尝试 AndroidTestCase 后我收到了 - android.database.sqlite.SQLiteException: 无法打开数据库文件
  • 将测试方法更新为 'ActivityTestCase' 并设置 - "mArg1= getInstrumentation().getTargetContext();"解决了。​​
  • 这仅仅意味着你有上下文问题。您的代码应该通过 AndroidTestCase。仅供参考,活动上下文和应用程序上下文不一样。我建议您了解两者并在此基础上更新代码。

标签: android sqlite nullpointerexception junit3


【解决方案1】:

必须更新测试用例中的第一行,将TestCase 更新为ActivityTestCase,并将上下文更新为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();
}

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多