gpt4 book ai didi

java - 使用 Context.getApplicationContext() 进行 Android JUnit 测试

转载 作者:行者123 更新时间:2023-11-29 08:31:36 25 4
gpt4 key购买 nike

我目前正在开发 android Room Persistency数据库项目。我已经成功实现了所有功能,现在正尝试为这些功能编写单元测试。这些单元测试依赖于调用以下函数(单例构造函数;我已经用注释标记了两行重要的代码):

@Database(entities = {RippleModel.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

public abstract RippleModelDao rippleModelDao();

private static AppDatabase INSTANCE; //Used for the singleton

public static AppDatabase getDatabase(Context context) { //THIS LINE
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context.getApplicationContext(),
AppDatabase.class, "ripple_db"
).build(); //STACKTRACE SHOWS HERE
}
return INSTANCE;
}
}

我当前的单元测试如下所示:

@RunWith(MockitoJUnitRunner.class)
public class DatabaseUnitTest extends InstrumentationTestCase {

@Mock
Context mMockContext = new MockContext();


@Test
public void testImportDatabase() throws Exception {
System.out.println("Importing database..");
System.out.println(mMockContext);
System.out.println(mMockContext == null);
AppDatabase appDatabase = AppDatabase.getDatabase(mMockContext);
}

}

但是,当使用 Mockito 运行此单元测试时,出现以下错误:

java.lang.IllegalArgumentException:无法为数据库提供空上下文。

at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:441)
at io.turtlesquad.ripplebt.data.AppDatabase.getDatabase(AppDatabase.java:20)
at io.turtlesquad.ripplebt.DatabaseUnitTest.testImportDatabase(DatabaseUnitTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

我已经使用“== null”检查了 Mockito 上下文是否不为空,但事实并非如此(创建的上下文不为空)。

我正在尝试使用 Mockito 模拟上下文,但不确定我在这里做错了什么。 Mockito 不应该模拟上下文吗?有什么我想念的吗?

最佳答案

我通过将 context.getApplicationContext() 替换为 getDatabase(.....) 参数中的 context 解决了同样的问题AppDatabase 类的方法

用以下代码替换您的 AppDatabase java 文件:

@Database(entities = {RippleModel.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

public abstract RippleModelDao rippleModelDao();

private static AppDatabase INSTANCE; //Used for the singleton

public static AppDatabase getDatabase(Context context) { //THIS LINE
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context,
AppDatabase.class, "ripple_db"
).build(); //STACKTRACE SHOWS HERE
}
return INSTANCE;
}

关于java - 使用 Context.getApplicationContext() 进行 Android JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47661888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com