【发布时间】:2017-03-18 09:33:53
【问题描述】:
我正在尝试创建一个极简测试类来测试我的一些 CouchBase Lite Mobile 类。我曾尝试模拟应用程序上下文,但没有成功。
问题在于 CouchBase 管理器正在访问 MockContext 类上的一些未实现的方法。
以下是我目前的方法:
public class ClassToTest extends TestCase {
@Test
public void testGetAllDocumentsBundle() throws Exception {
try {
Manager manager = new Manager(
new AndroidContext(new MockContext()),
Manager.DEFAULT_OPTIONS);
}
catch (Exception e) {
Log.e(TAG, "Exception: " + e);
assertTrue(false);
}
}
还有:
@RunWith(AndroidJUnit4.class)
public class ItemReaderTest {
private android.content.Context instrumentationCtx;
@Before
public void setUp() throws Exception {
instrumentationCtx = InstrumentationRegistry.getContext();
}
...
@Test
public void testGetAllDocumentsBundle() throws Exception {
Database database = null;
String databaseName = "test";
try {
Context context = new AndroidContext(instrumentationCtx);
Assert.assertTrue(context != null);
Manager manager = new Manager(context, Manager.DEFAULT_OPTIONS); <--- throws exception because context.getFilesDir(); return null in Couchbase Manager.java constructor
...
}
}
有人能做到吗?我是否绝对必须使用实际的 Activity(即创建一个实际的移动应用程序并使用它的上下文来测试 Couchbase 移动设备?
【问题讨论】:
标签: java android unit-testing android-studio couchbase-lite