1

Can anyone explain why am i getting this error while running below code ? get same error with other annotations also (e.g. @MethodSource , @CsvSource)

class TestRunningOnJUnit5 {

@ParameterizedTest
@CsvSource("test,TEST", "tEst,TEST", "Java,JAVA")
fun toUpperCase_ShouldGenerateTheExpectedUppercaseValue(
    input: String,
    expected: String?
) {
    val actualValue = input.toUpperCase()
    assertEquals(expected, actualValue)
}
}

here is my build.gradle file and also attached screenshot of error. i do not have reference of junit 4 in build.gradle why it is throwing error for AndridjUnit4ClassRunner

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0
implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'


// Android Testing Support Libraries's runner and rules
androidTestUtil 'androidx.test:orchestrator:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
// Espresso UI Testing
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' 
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

// Android Instrumentation Tests wth JUnit 5
testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0"

androidTestImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
androidTestImplementation "org.junit.jupiter:junit-jupiter-engine:5.7.0"
androidTestImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0"

androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.0"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.0"

enter image description here

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
Shah
  • 553
  • 5
  • 12
  • 1
    Note that you're using Junit4, not Junit5! Take a look at your stack trace. – Adam Arold Oct 17 '20 at 07:24
  • I believe `AndroidJUnit4ClassRunner` comes with some other dependency (possibly 'androidx.test:rules:1.3.0' or 'androidx.test.ext:junit:1.1.1' ). I don't see any mentions of JUnit5 availability on android platform in [official docs](https://developer.android.com/training/testing/junit-runner). Maybe it's not possible yet and best option is to use JUnit4? – Михаил Нафталь Oct 17 '20 at 18:23

1 Answers1

1

You can take a look at one of the JUnit 5 samples, which uses Gradle and Kotlin. Summary of the relevant code:

build.gradle.kts

dependencies {
    // [...]

    testImplementation(platform("org.junit:junit-bom:5.7.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}

Calculator.kt

class Calculator {
    fun add(a: Int, b: Int): Int = a + b
}

CalculatorTests.kt

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource

class CalculatorTests {
    @ParameterizedTest(name = "{0} + {1} = {2}")
    @CsvSource(
        "1,    2,   3",
        "1,  100, 101"
    )
    fun add(first: Int, second: Int, expectedResult: Int) {
        val calculator = Calculator()
        assertEquals(expectedResult, calculator.add(first, second)) {
            "$first + $second should equal $expectedResult"
        }
    }
}
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28