lundi 25 mars 2019

Trying to write some tests for my Room database but keeps getting this errors

I am trying to exercise some good coding and I am following the google android component testing but they test one field. I have 4 and I keep getting this error please guide me. I am very new in Room database testing.

I believe I can use this example for future understanding here Is my code I am struggling with asserts.

LIVEDATA CLASS

class LiveDataTest {

    static <T> T getValue(final LiveData<T> tLiveData) throws InterruptedException{

        final  Object[] data = new Object[1];
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        Observer<T> observer = new Observer<T>() {
            @Override
            public void onChanged(@Nullable T t) {
                data[0] = t;
                countDownLatch.countDown();
                tLiveData.removeObserver(this);
            }
        };

        tLiveData.observeForever(observer);
        countDownLatch.await(2, TimeUnit.SECONDS);


        return (T) data[0];

    }
}


@Test
    public void insertAndGetItems() throws InterruptedException {

        TaskEntry taskEntry = new TaskEntry(1,"item 1",1,date);
        taskDao.insertTask(taskEntry);

       // List<TaskEntry> allEntries = LiveDataTest.getValue(taskDao.loadAllTasks());
      //  assertEquals(allEntries.get(0).getId(),taskEntry.getDescription());
    }



@Test
    public void deleteAll() throws InterruptedException {
        TaskEntry taskEntry = new TaskEntry(1,"item 1",1,date);
        taskDao.insertTask(taskEntry);

        TaskEntry taskEntry1 = new TaskEntry(2,"item 2",3,date);
        taskDao.insertTask(taskEntry1);

        taskDao.deleteTask(taskEntry);

        List<TaskEntry> entries = LiveDataTest.getValue(taskDao.loadAllTasks());
        assertTrue(entries.isEmpty());
    }

ERROR:


  interface method 'boolean java.util.List.isEmpty()' on a null object reference
at DaoTest.TaskDaoTest.deleteAll(TaskDaoTest.java:74)
at java.lang.reflect.Method.invoke(Native Method)
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)

Aucun commentaire:

Enregistrer un commentaire