Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'?

后端 未结 19 1801
长情又很酷
长情又很酷 2020-12-05 06:35

I am migrating my app to androidx, I can\'t seem to get my unit tests working. I took example from Google\'s AndroidJunitRunnerSample, which has been updated to use the new

相关标签:
19条回答
  • 2020-12-05 06:54

    In my case, I skipped annotate test case with @Test. It is not your case. This answer is for others like me.

    0 讨论(0)
  • 2020-12-05 06:55

    While testing for activity make sure ActivityTestRule variable is public :

    @Rule
    public ActivityTestRule<YourActivity> activityTestRule = new ActivityTestRule<>(YourActivity.class); 
    
    0 讨论(0)
  • 2020-12-05 06:59

    The error message displayed when I delete the @Test method.

    You can try to put a @Test method and run

    @Test
    public void signInTest() {
    
    
    }
    
    0 讨论(0)
  • 2020-12-05 06:59

    In my case, I fixed it by explicitly declaring all @Test, @Before, and @After methods aspublic, and declaring @BeforeClass and @AfterClass as public static

    If you leave any of those methods as implicitly/explicitly protected, or explicitly private; you'll encounter the following exception

    java.lang.RuntimeException: Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded.
    

    So, the correct thing is to use

    @Before
    public void setUp() {
    
    }
    
    @BeforeClass
    public static void init() {
    
    }
    

    Instead of:

    @Before
    void setUp() {
    
    }
    
    @Before
    protected void setUp() {
    
    }
    
    @Before
    private void setUp() {
    
    }
    
    @BeforeClass
    public void init() {
    
    }
    
    0 讨论(0)
  • 2020-12-05 07:01

    The testing framework actually give too little information. I encountered the same issue and dug into the stack trace, and found these validation:

    So you must declare your @BeforeClass method static.

    0 讨论(0)
  • 2020-12-05 07:03

    There are 2 ways that you can try to resolve this problem:

    Solution 1:

    Build --> Clean Project. Then Build --> Rebuild Project

    Solution 2:

    There are 2 packages that has of AndroidJUnit4

    1. androidx.test.runner (deprecated)
    2. androidx.test.ext.junit.runners

    Make sure your build.gradle (app) has this line

    android {
        defaultConfig {
        ....
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ....
        }
    }
    

    and make sure you use (1) (which is deprecated) instead of (2) (new but not stable yet) in @RunWith(AndroidJUnit4.class) before your test class.

    0 讨论(0)
提交回复
热议问题