android / android/android-test

null cannot be used as parameter value in parameterized tests

Aperta
#837 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
1.2k
Fork
342
Merge medio
11h 29m
PR unite (30g)
2

Descrizione

**Describe the bug**
Using `null` as a parameter value for parameterized tests does not work properly. The exact errors vary - sometimes it appears to get confused about the type of `null`:
```
java.lang.IllegalArgumentException: method com.example.BrokenTest. argument 1 has type java.lang.Integer, got java.lang.String
at java.lang.reflect.Constructor.newInstance(Native Method)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.createTestUsingConstructorInjection(BlockJUnit4ClassRunnerWithParameters.java:56)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.createTest(BlockJUnit4ClassRunnerWithParameters.java:46)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260)
...
```

Other times it seems to get very confused about the class name of the test:
```
java.lang.ClassNotFoundException: Invalid name: positions=[0.0])]
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:324)
at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:72)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:105)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:804)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:575)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:393)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
```

(In this case, `positions=[0.0])` is the tail end of a `toString` for one of the other non-null parameter values.)

**To Reproduce**
The first case can be easily reproduced:
```kotlin
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import kotlin.test.assertEquals

@RunWith(Parameterized::class)
class BrokenTest(private val testValue: Int?) {

@Test fun test() {
if (testValue != null) {
assertEquals(0, testValue % 2)
}
}

companion object {

@[JvmStatic Parameterized.Parameters(name = "{0}")]
fun parameters(): List = listOf(null, 0, 2, 4)
}
}

@RunWith(Parameterized::class)
class WorkingTest(private val testValue: Int?) {

@Test fun test() {
if (testValue != null) {
assertEquals(0, testValue % 2)
}
}

companion object {

@[JvmStatic Parameterized.Parameters(name = "{0}")]
fun parameters(): List = listOf(0, 2, 4)
}
}
```

I'm not yet 100% sure what causes the second but can investigate a minimal repro sample if needed (especially if that issue appears to be unrelated to the first).

**Expected behavior**
The above tests should all pass, as they do when run locally.

**Details (please complete the following information):**
> Have you tested on the latest Flank snapshot?

No

> Post the output of ` flank --version`.

20.09.3

Link: https://github.com/Flank/flank/issues/1374

We tracked this issue down to a limitation in orchestrator itself.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia riproducendo BrokenTest e WorkingTest con il JUnit Parameterized runner, quindi leggi BlockJUnit4ClassRunnerWithParameters e la gestione dei nomi dei parametri mostrata nel report. Confronta il caso null con i casi non null e controlla il Flank issue collegato, in cui la limitazione è stata ricondotta all’orchestratore; il lavoro è completato quando i test parametrizzati accettano valori null senza gli errori di tipo o di nome della classe segnalati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java, kotlin
Ambito
mobile, testing
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.