question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Delegate runner 'org.robolectric.RobolectricTestRunner' for AndroidJUnit4 could not be loaded.

See original GitHub issue

Description

I am trying to use ActivityScenarioRule in my jUnit tests. However, if I add it to my test it gives me the exception as shown below.

Steps to Reproduce

  • Create a new project with Android Studio 3.4
  • Update the build.gradle file with the following dependencies:
 dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    testImplementation "androidx.test:core:1.1.1"
    testImplementation "androidx.test.ext:junit:1.1.1"
    testImplementation "org.robolectric:robolectric:4.2.1"
    androidTestImplementation "androidx.test:runner:1.1.1"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
  • Update the existing unit test like this:
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleUnitTest {

    @Rule
    ActivityScenarioRule<MainActivity> activityScenarioRule = new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
    }
}
  • Run the test

Expected Results

The test should pass

Actual Results

The following exception is thrown:

java.lang.RuntimeException: Delegate runner 'org.robolectric.RobolectricTestRunner' for AndroidJUnit4 could not be loaded.

	at androidx.test.ext.junit.runners.AndroidJUnit4.throwInitializationError(AndroidJUnit4.java:92)
	at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:82)
	at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:51)
	at androidx.test.ext.junit.runners.AndroidJUnit4.<init>(AndroidJUnit4.java:46)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
	at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:72)
	... 17 more
Caused by: org.junit.runners.model.InitializationError
	at org.junit.runners.ParentRunner.validate(ParentRunner.java:418)
	at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
	at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
	at org.robolectric.internal.SandboxTestRunner.<init>(SandboxTestRunner.java:75)
	at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:100)
	at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:95)
	... 22 more

AndroidX Test and Android OS Versions

    testImplementation 'junit:junit:4.12'
    testImplementation "androidx.test:core:1.1.1"
    testImplementation "androidx.test.ext:junit:1.1.1"
    testImplementation "org.robolectric:robolectric:4.2.1"
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    Android Studio 3.4
    Windows 10

Link to a public git repo demonstrating the problem:

DemoActivityScenarioRule

If I comment the ActivityScenarioRule, the test pass without problems.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

3reactions
ajharry69commented, Jan 31, 2020

Make sure you have at least one test method in your class before trying to run the test class(Could also be relevant in some scenario)

1reaction
rajBopchecommented, Feb 17, 2020

I encountered the same error when my setUp() method annotated with @Before was private. The fix was to remove the private modifier.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Attempted to use AndroidJUnit4 with standard JUnit runner ...
Attempted to use AndroidJUnit4 with standard JUnit runner and delegate runner 'org.robolectric.RobolectricTestRunner'could not be loaded · Ask ...
Read more >
java.lang.RuntimeException: Delegate runner 'org.robolectric ...
RuntimeException : Delegate runner 'org.robolectric.RobolectricTestRunner' for AndroidJUnit4 could not be loaded. 769 views. Skip to first unread message.
Read more >
AndroidX Test - Robolectric
Using the AndroidX Test APIs reduces the cognitive load for you as a developer, ... It is now possible to use the AndroidX...
Read more >
I am getting this exception. Please help me with this. - Medium
Attempted to use AndroidJUnit4 with standard JUnit runner and delegate runner 'org.robolectric.RobolectricTestRunner'could not be loaded.
Read more >
Source Code for AndroidJUnit4.java - AndroidX Tech
getProperty("android.junit.runner", "org.robolectric. ... to use AndroidJUnit4 with standard JUnit runner and delegate runner '%s'" + "could not be loaded.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found