I am trying to use Robolectric in order to unit test some classes in an Android application without using an emulator or a device. I would like, for example, to unit test a Dao class but I always have the following exception :
Caused by: java.lang.ClassNotFoundException: android.support.v4.app.NotificationCompat$Style
Here the fullstackstrace :
java.lang.NoClassDefFoundError: android/support/v4/app/NotificationCompat$Style at com.pcg.ape.AudiencePlus.a(Unknown Source) at com.pcg.ape.AudiencePlus.initialize(Unknown Source) at com.company.app.AppApplication.onCreate(AppApplication.kt:360) at org.robolectric.android.internal.AndroidEnvironment.lambda$installAndCreateApplication$0(AndroidEnvironment.java:253) at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:75) at org.robolectric.android.internal.AndroidEnvironment.installAndCreateApplication(AndroidEnvironment.java:253) at org.robolectric.android.internal.AndroidEnvironment.setUpApplicationState(AndroidEnvironment.java:149) at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:298) at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:247) at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.ClassNotFoundException: android.support.v4.app.NotificationCompat$Style at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:164) at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:119) at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass$0(SandboxClassLoader.java:112) at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53) at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:111) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 16 more
Note that the AudiencePlus library is a library in order to received push notifications (like OneSignal).
The project uses androidX. Here some configuration elements :
The gradle.properties
file contains these lines :
android.enableJetifier=true
android.useAndroidX=true
android.enableUnitTestBinaryResources=true
The build.gradle
file of the app module contains these lines :
android
{
defaultConfig
{
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testOptions
{
unitTests
{
includeAndroidResources = true
}
}
}
}
dependencies
{
testImplementation "junit:junit:4.12"
testImplementation "androidx.test:runner:1.1.1"
testImplementation "androidx.test.ext:junit-ktx:1.1.0"
testImplementation "org.robolectric:robolectric:4.2"
}
Here the class I am trying to test :
package com.company.app.room.ecat.dao
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MyDaoTest
{
@Test
fun myMethodTest()
{
}
}
Note that I try all the "Shorten command line" options into the the "Run/Debug Configurations" menu.
The "none" one does not compile (too long arguments) and all other work with the ClassNotFoundException
.
Anyone can help me ? Why does it try to load a non-androidx class ?