I'm using Room on Android. So I've got my data model class with the proper Room annotations and a single public constructor. It uses a builder pattern and tries to be immutable. It's here.
Then I've got my @Dao
-annotated interface with @Query
, @Update
, and @Insert
annotations next to all the method declarations as it should be. Right over here.
Finally, I've got my Dao test class, which uses Room.inMemoryDatabaseBuilder
. It is here.
So when I run the tests in GoalsDaoTest, they all pass except for two: One that uses the Dao method annotated @Update
, and another whose Dao method uses an SQL UPDATE
command inside its @Query
.
Here's my test set-up and tear-down, using Room.inMemoryDatabaseBuilder
:
@Before
public void initDb() {
mDatabase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
SelfCareDatabase.class).build();
}
@After
public void closeDb() {
mDatabase.close();
}
Here's the first failing test:
@Test
public void updateArchivedAndGetById() {
// When inserting a goal
mDatabase.goalDao().insertGoal(GOAL);
// When the goal is updated
mDatabase.goalDao().updateArchived(GOAL.getId(), true);
// When getting the goal by id from the database
Goal loaded = mDatabase.goalDao().getGoalById(ID);
// The loaded data contains the expected values
assertGoal(loaded, GOAL.getId(), GOAL.getTitle(), GOAL.getInterval(), GOAL.getPolarity(), true, GOAL.getTouched());
}
Here's the error it produces when I run it:
java.lang.NoSuchMethodError: No interface method updateArchived(Ljava/lang/String;Z)V in class Lcom/beatboxchad/android/selfcaredashboard/data/source/local/GoalsDao; or its super classes (declaration of 'com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDao' appears in /data/app/com.beatboxchad.android.selfcaredashboard-1/base.apk)
at com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDaoTest.updateArchivedAndGetById(GoalsDaoTest.java:155)
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)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
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.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)
And yet, that method is properly declared and annotated in the GoalsDao class:
/**
* Update the Archived status of a goal
*
* @param goalId id of the goal
* @param archived status to be updated
*/
@Query("UPDATE goals SET archived = :archived WHERE entryid = :goalId")
void updateArchived(String goalId, boolean archived);
Here's the other test:
@Test
public void updateGoalAndGetById() {
// When inserting a goal
mDatabase.goalDao().insertGoal(GOAL);
// When the goal is updated
Goal updatedGoal = new Goal.Builder(ID)
.setTitle(TITLE2)
.setPolarity(POLARITY2)
.setInterval(INTERVAL2)
.setArchived(ARCHIVED2)
.setTouched(TOUCHED2)
.build();
mDatabase.goalDao().updateGoal(updatedGoal);
// When getting the goal by id from the database
Goal loaded = mDatabase.goalDao().getGoalById(ID);
// The loaded data contains the expected values
assertGoal(loaded, ID, TITLE2, INTERVAL2, POLARITY2, ARCHIVED2, TOUCHED2);
}
Here's its error:
java.lang.NoSuchMethodError: No interface method updateGoal(Lcom/beatboxchad/android/selfcaredashboard/data/Goal;)I in class Lcom/beatboxchad/android/selfcaredashboard/data/source/local/GoalsDao; or its super classes (declaration of 'com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDao' appears in /data/app/com.beatboxchad.android.selfcaredashboard-1/base.apk)
at com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDaoTest.updateGoalAndGetById(GoalsDaoTest.java:140)
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)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
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.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)
And yet! Confound me! What madness causes the computer to lie? GoalsDao.java excerpt:
/**
* Update a goal.
*
* @param goal goal to be updated
* @return the number of goals updated. This should always be 1.
*/
@Update
int updateGoal(Goal goal);
I've exhausted all the low-hanging fruit on this one. Everything appears to conform to the docs (though I'm suspicious of my immutable builder'd Goal class), and I've attempted to follow advice given in the following places:
Why Room entities don't work with immutable properties in Android
Espresso Test Failing: No interface method trackUsage() in UsageTracker.java
Room Persistence Library run time exception when calling Rooms inMemoryBuilder method
But I'm not using Kotlin nor the @Relation
annotation, and even when I played with all kinds of different versions in my build.gradle files, it stayed broken. The bright side is, I've learned a lot about that boring part. tl;dr I'm stumped.
Just in case it's helpful context, my app is strongly based on the todo‑mvvm‑databinding example from https://github.com/googlesamples/android-architecture. I mention this because everything works in the sample, but I've added some complexity and perhaps made a mistake along the way that somebody more experienced will recognize. Particularly, I've mangled that @Entity
Goal class (Task in the example app), trying to keep it immutable and adding the builder pattern and several attributes. I'm not done mangling it either -- touched is about to be its own table.
I'm also unskilled in troubleshooting whatever turns an interface and a bunch of annotations into code. Any advice about where to look in resolving this is as useful as the answer it'll lead to. I'm new to Android Studio, Java, and the whole build process.
Thank you!
Invalidate caches and restart in android studio helped