如何测试,在与Android的SQLite数据库处理方法呢? [英] How to test methods that deal with SQLite database in android?

查看:179
本文介绍了如何测试,在与Android的SQLite数据库处理方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过开放与SQLite数据库交易,从检索数据,并插入数据,它的应用程序。现在我想测试我的方法。

所以,我有两个类,一个延伸 SQLiteOpenHelper 来打开,创建和升级 SQLiteHelper 数据库,另一个是数据源类,这使得一个 SQLiteDatabase 对象,包含我的所有方法的交易与数据库

所以调用任何方法,我需要调用在 SQLiteHelper 类的开放式方法,捕捉结果在 SQLiteDatabse 对象,然后进行数据源类的对象,最后调用方法(包含光标作为例如rawquery的结果)。

我知道这不会简单的JUnit测试工作,我读过有关Mock对象,但我还是不明白我怎么能在我的情况下使用它。


解决方案

  

我知道这不会简单的JUnit测试工作。


它将。只需拨打你的方法和往常一样,检查结果的正确性。

唯一重要的事情是,你应该preFER使用 IsolatedContext 为您创建数据库。在这种情况下,从应用程式的原始数据库文件将保持不变。所有的测试将可作为您希望修改或删除,甚至多次单独的测试数据库文件的工作。

您可以获取适当的 IsolatedContext 来自的 ProviderTestCase2 。 (你也可以看看 ProviderTestCase2 来源理解它是如何工作的。)

I have made an application that deals with SQLite database by opening, retrieving data from, and inserting data to it. Now I want to test my methods.

So , I have two classes, one "SQLiteHelper" which extends SQLiteOpenHelper to open,create,and upgrade the database, and the other is a DataSource class, that makes an SQLiteDatabase object,and contains all my methods that deals with the database.

So for calling any method I need to call the open method in the SQLiteHelper class, catch the result in an SQLiteDatabse object,then make an object of the DataSource class,and finally call the method( which contains a cursor as a result of a rawquery for example).

I know that it won't work with simple JUnit test, and I've read about Mock Objects, but I still don't understand how I can use it in my case.

解决方案

I know that it won't work with simple JUnit test

It will. Just call your methods as usual and check results for correctness.

The only important thing is that you should prefer to use IsolatedContext for your database creation. In this case your original database file from the app will remain unmodified. All tests will work with separate testing database file which can be modified or even deleted as many times as you want.

You can acquire proper IsolatedContext from ProviderTestCase2. (You can also look ProviderTestCase2 sources to understand how it works.)

这篇关于如何测试,在与Android的SQLite数据库处理方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆