我想对我创建的 DatabaseService 类进行单元测试(修改了我在网上找到的 WishList 应用程序)包 com.test.db;
import java.sql.SQLException;
import java.util.List;
import android.content.Context;
import com.test.model.WishList;
public class WishListService
{
private static WishListService instance;
public static void init(Context ctx)
{
if (null == instance)
{
instance = new WishListService(ctx);
}
}
static public WishListService getInstance()
{
return instance;
}
private DatabaseHelper helper;
private WishListService(Context ctx)
{
helper = DatabaseHelper.getInstance(ctx);
}
public List<WishList> getAllWishLists()
{
List<WishList> wishLists = null;
try
{
wishLists = helper.getWishListDao().queryForAll();
} catch (SQLException e)
{
e.printStackTrace();
}
return wishLists;
}
public void addWishList(WishList l)
{
try
{
helper.getWishListDao().create(l);
} catch (SQLException e)
{
e.printStackTrace();
}
}
public WishList getWishListWithId(int wishListId)
{
WishList wishList = null;
try
{
wishList = helper.getWishListDao().queryForId(wishListId);
} catch (SQLException e)
{
e.printStackTrace();
}
return wishList;
}
public void deleteWishList(WishList wishList)
{
try
{
helper.getWishListDao().delete(wishList);
} catch (SQLException e)
{
e.printStackTrace();
}
}
public void updateWishList(WishList wishList)
{
try
{
helper.getWishListDao().update(wishList);
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
我的问题是有没有办法实例化 DatabaseHelper 而不必创建测试活动并将其作为 DatabaseHelper 需要的上下文传递?理想情况下,我想将此类作为标准 JUnit 测试进行单元测试,而不是 Android JUnit 测试