OrmLiteSqliteOpenHelper

OrmLiteSqliteOpenHelper

这是我的代码:

@RunWith(MockitoJUnitRunner.class)
public class OrmLiteTest {

    OrmLiteDatabaseHelper databaseHelper;

    @Mock
    Context mMockContext;

    @Before
    public void setup() {
        mMockContext = Mockito.mock(Context.class);
        databaseHelper = OpenHelperManager.getHelper(mMockContext,
                OrmLiteDatabaseHelper.class);
    }


运行测试时,出现以下异常:

java.lang.IllegalStateException: Could not construct instance of helper class class com.example.myapp.application.OrmLiteDatabaseHelper
    at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:222)
    at com.j256.ormlite.android.apptools.OpenHelperManager.loadHelper(OpenHelperManager.java:170)
    at com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(OpenHelperManager.java:78)
    at com.bulatsa.despark.OrmLiteTest.setup(OrmLiteTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    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.RunBefores.evaluate(RunBefores.java:24)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:220)
    ... 31 more
Caused by: java.lang.NullPointerException
    at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310)
    at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.<init>(OrmLiteSqliteOpenHelper.java:76)
    at com.bulatsa.despark.application.OrmLiteDatabaseHelper.<init>(OrmLiteDatabaseHelper.java:28)
    ... 36 more

最佳答案

您正在尝试在OrmLiteSqliteOpenHelper.java,第310行中为空的对象上调用方法(或类似方法)

因为您没有共享该代码,所以我们只能说...


  造成原因:java.lang.NullPointerException
      在com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310)


哦,那线...

 mMockContext = Mockito.mock(Context.class);


...应删除,因为@RunWith(MockitoJUnitRunner.class)已经在其中注入了一个模拟,无需手动创建一个。

10-04 18:24