1

This is my code:

@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);
    }

I get the following exception when I run the test:

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
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • What do you want to test? I think you are not in the right way – Héctor Dec 23 '15 at 07:13
  • I want to create some products, then product photos, relate them to each other, then delete the products and see whether the product photos are deleted along with them. For that I need the OrmLite DB Helper. – Kaloyan Roussev Dec 23 '15 at 07:32
  • Maybe this helps you http://stackoverflow.com/questions/2095695/android-unit-tests-requiring-context – Héctor Dec 23 '15 at 07:37
  • Wow Android is painful. Have to use the other type of tests that require an emulator or a device, which is slow. – Kaloyan Roussev Dec 23 '15 at 07:39
  • Emulator tests are a completely different level. You start with Unit tests for all pieces of code, then go up to integration tests and then you do tests on an emulator. Skipping one of these is only a way to painful learning. And yes, good testing is hard, but you'll get the hang of it. – Florian Schaetz Dec 23 '15 at 07:53
  • I meant I dont want to fire up the emulator just so that I can test some database stuff, and the answer linked to above says if I need a context, I need instrumentation tests with emulator... Anyway, thanks everyone for the help – Kaloyan Roussev Dec 23 '15 at 07:58

1 Answers1

2

You are trying to invoke a method (or similar) on an object which is null in the OrmLiteSqliteOpenHelper.java, line 310

As you didn't share that code, that's all we can say...

Caused by: java.lang.NullPointerException at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310)

Oh, and the line...

 mMockContext = Mockito.mock(Context.class);

...should be removed, since the @RunWith(MockitoJUnitRunner.class) will already inject a mock there, no need to create one manually.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • I think it is the context that is null, because the same line of code works in normal circumstances in my Android project, so that means I wasnt able to instantiate the Context properly? – Kaloyan Roussev Dec 23 '15 at 07:05
  • 1
    I have no idea, since I cannot see the code. The context that you are generating there is not null, so personally I would guess, there's a construct in there like `context.getXYZ().doSomething();` - which would lead to a NPE, since your mock context does not know what to return when `getXYZ()` is called, so it would return null - on which you then try to call `doSomething`. You probably need another mock in there and a line like `Mockito.when( context.getXYZ()).thenReturn( mockOfXYZ() );` – Florian Schaetz Dec 23 '15 at 07:50