0

I'm trying to follow Android Developer Docs to write an Instrumented Unit Test, but, of course, it doesn't work. I am getting the error:

Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

when ever I run my example test:

package com.devetry.ytp

import android.content.Context
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

import androidx.test.runner.AndroidJUnitRunner
import org.junit.Assert
import org.junit.Rule

@RunWith(AndroidJUnit4::class)
class ExampleAndroidTest {

    /**
     * VARIABLES
     */



    /**
     * LIFE CYCLE
     */


    /**
     * Example Android Test
     *
     * An example android test
     */
    @Test
    fun exampleAndroidTest() {
        val context = InstrumentationRegistry.getTargetContext()
        Assert.assertEquals("com.devetry.ytp", context.packageName)
    }

}

I have tried tried multiple solutions to this specific error I found online, but like most things Android, the solution was either outdated or just simply didn't work. Unfortunately, of all of the solutions, I was unable to even recognize a common theme, thus leaving me stranded.

How can I resolve the error and just get an Instrumented Unit Tests to run?

BlondeSwan
  • 772
  • 5
  • 26

1 Answers1

0
package com.devetry.ytp

import android.content.Context
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

// AndroidJunitRunner and AndroidJUnit4 are not from the same pacakge
import androidx.test.runner.AndroidJUnitRunner
import org.junit.Assert
import org.junit.Rule

@RunWith(AndroidJUnit4::class)
class ExampleAndroidTest {

    /**
     * VARIABLES
     */



    /**
     * LIFE CYCLE
     */
...
}

You are not using the classes from the same package, make sure they are consistent. The support test package is conflict with androidx package.

ggaier
  • 331
  • 2
  • 6
  • Please consider copying, pasting and properly formatting any code that you include in your example, rather than use images of character data. – chb Jun 09 '19 at 04:23