Skip to content

Commit

Permalink
Use JUnit5 on tests
Browse files Browse the repository at this point in the history
1. Extract JUnit version to a constant, so all 3 testImplementations uses the same version
2. Add JUnitPlatform use to `build.gradle`
3. Fix imports to `kotlin.test.junit5.JUnit5Asserter`
4. Fix `AbstractFlashcardViewerTest.getSignalFromUrlTest`
- It is currently the only parameterized test that uses jupiter-params. It wasn't being discovered before, so it haven't raised any exception.
- Now that it's discovered, it needed to be fixed
  • Loading branch information
BrayanDSO authored and mikehardy committed May 2, 2022
1 parent 2458c6a commit 8f9f8c2
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 42 deletions.
7 changes: 4 additions & 3 deletions AnkiDroid/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
// Gradle plugin portal
// Gradle plugin portal
id 'com.github.triplet.play' version '3.7.0'
}

Expand Down Expand Up @@ -278,7 +278,8 @@ dependencies {
// A path for a testing library is typically under rsdroid-testing/assets
testImplementation "io.github.david-allison-1:anki-android-backend-testing:$ankidroid_backend_version"
// A path for a testing library which provide Parameterized Test
testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.2"
testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"

// May need a resolution strategy for support libs to our versions
implementation "ch.acra:acra-http:$acra_version"
Expand Down Expand Up @@ -310,7 +311,7 @@ dependencies {

api project(":api")

testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2'
testImplementation "org.junit.vintage:junit-vintage-engine:$junit_version"
testImplementation 'org.mockito:mockito-inline:4.5.1'
testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
testImplementation "org.hamcrest:hamcrest:$hamcrest_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ import com.ichi2.anki.AbstractFlashcardViewer.WebViewSignalParserUtils.RELINQUIS
import com.ichi2.anki.AbstractFlashcardViewer.WebViewSignalParserUtils.SHOW_ANSWER
import com.ichi2.anki.AbstractFlashcardViewer.WebViewSignalParserUtils.SIGNAL_NOOP
import com.ichi2.anki.AbstractFlashcardViewer.WebViewSignalParserUtils.TYPE_FOCUS
import com.ichi2.anki.AbstractFlashcardViewer.WebViewSignalParserUtils.getSignalFromUrl
import com.ichi2.anki.cardviewer.ViewerCommand
import com.ichi2.anki.reviewer.AutomaticAnswer
import com.ichi2.anki.reviewer.AutomaticAnswerAction
import com.ichi2.anki.reviewer.AutomaticAnswerSettings
import com.ichi2.anki.servicelayer.LanguageHintService
import com.ichi2.libanki.StdModels
import com.ichi2.libanki.Tuple
import com.ichi2.testutils.AnkiAssert.assertDoesNotThrow
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.Assert.*
import org.junit.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import org.junit.runner.RunWith
import org.mockito.Mockito.*
Expand Down Expand Up @@ -72,27 +73,10 @@ class AbstractFlashcardViewerTest : RobolectricTest() {
}
}

/***
* @param testcase A tuple with string and int as a testcase. The string is the input url and
* the int is the actual answer.
*/
@ParameterizedTest
@MethodSource("testcaseProvider")
fun getSignalFromUrlTest(testcase: Tuple<String, Int>) {
assertEquals(testcase.first, testcase.second)
}

fun testcaseProvider(): Stream<Tuple<String, Int>> {
return Stream.of(
Tuple("signal:show_answer", SHOW_ANSWER),
Tuple("signal:typefocus", TYPE_FOCUS),
Tuple("signal:relinquishFocus", RELINQUISH_FOCUS),
Tuple("signal:answer_ease1", ANSWER_ORDINAL_1),
Tuple("signal:answer_ease2", ANSWER_ORDINAL_2),
Tuple("signal:answer_ease3", ANSWER_ORDINAL_3),
Tuple("signal:answer_ease4", ANSWER_ORDINAL_4),
Tuple("signal:answer_ease0", SIGNAL_NOOP)
)
@MethodSource("getSignalFromUrlTest_args")
fun getSignalFromUrlTest(url: String, signal: Int) {
assertEquals(getSignalFromUrl(url), signal)
}

@Test
Expand Down Expand Up @@ -268,4 +252,19 @@ class AbstractFlashcardViewerTest : RobolectricTest() {
advanceRobolectricLooperWithSleep()
return multimediaController
}
companion object {
@JvmStatic
fun getSignalFromUrlTest_args(): Stream<Arguments> {
return Stream.of(
Arguments.of("signal:show_answer", SHOW_ANSWER),
Arguments.of("signal:typefocus", TYPE_FOCUS),
Arguments.of("signal:relinquishFocus", RELINQUISH_FOCUS),
Arguments.of("signal:answer_ease1", ANSWER_ORDINAL_1),
Arguments.of("signal:answer_ease2", ANSWER_ORDINAL_2),
Arguments.of("signal:answer_ease3", ANSWER_ORDINAL_3),
Arguments.of("signal:answer_ease4", ANSWER_ORDINAL_4),
Arguments.of("signal:answer_ease0", SIGNAL_NOOP)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.junit.JUnitAsserter.assertEquals
import kotlin.test.junit.JUnitAsserter.assertNotNull
import kotlin.test.junit.JUnitAsserter.assertNull
import kotlin.test.junit5.JUnit5Asserter.assertEquals
import kotlin.test.junit5.JUnit5Asserter.assertNotNull
import kotlin.test.junit5.JUnit5Asserter.assertNull

/**
* Test for [BackupManager] without [RobolectricTest]. For performance
Expand Down
10 changes: 5 additions & 5 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardTemplateEditorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import org.robolectric.Robolectric
import org.robolectric.Shadows.shadowOf
import org.robolectric.shadows.ShadowActivity
import timber.log.Timber
import kotlin.test.junit.JUnitAsserter.assertEquals
import kotlin.test.junit.JUnitAsserter.assertNotEquals
import kotlin.test.junit.JUnitAsserter.assertNotNull
import kotlin.test.junit.JUnitAsserter.assertNull
import kotlin.test.junit.JUnitAsserter.assertTrue
import kotlin.test.junit5.JUnit5Asserter.assertEquals
import kotlin.test.junit5.JUnit5Asserter.assertNotEquals
import kotlin.test.junit5.JUnit5Asserter.assertNotNull
import kotlin.test.junit5.JUnit5Asserter.assertNull
import kotlin.test.junit5.JUnit5Asserter.assertTrue

@RunWith(AndroidJUnit4::class)
class CardTemplateEditorTest : RobolectricTest() {
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner
import timber.log.Timber
import java.util.*
import kotlin.test.junit.JUnitAsserter.assertNotNull
import kotlin.test.junit.JUnitAsserter.assertNull
import kotlin.test.junit5.JUnit5Asserter.assertNotNull
import kotlin.test.junit5.JUnit5Asserter.assertNull

@KotlinCleanup("`is` -> equalTo()")
@KotlinCleanup("redundant `val col = col`")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.junit.runner.RunWith
import timber.log.Timber
import java.io.IOException
import java.io.Serializable
import kotlin.test.junit.JUnitAsserter.assertNotNull
import kotlin.test.junit5.JUnit5Asserter.assertNotNull

@RunWith(AndroidJUnit4::class)
class TemporaryModelTest : RobolectricTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.whenever
import kotlin.test.junit.JUnitAsserter.assertTrue
import kotlin.test.junit5.JUnit5Asserter.assertTrue

class CardAppearanceTest {

Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/test/java/com/ichi2/testutils/AnkiAssertKt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.ichi2.testutils

import org.junit.Assert
import kotlin.test.junit.JUnitAsserter
import kotlin.test.junit5.JUnit5Asserter

/** assertThrows, allowing for lambda shorthand
*
Expand Down Expand Up @@ -64,5 +64,5 @@ fun assertFalse(message: String? = null, actual: Boolean) {
// This exists in JUnit, but we want to avoid JUnit as their `assertNotNull` does not use contracts
// So, we want a method in a different namespace for `assertFalse`
// JUnitAsserter doesn't contain it, so we add it in
JUnitAsserter.assertTrue(message, !actual)
JUnit5Asserter.assertTrue(message, !actual)
}
2 changes: 1 addition & 1 deletion AnkiDroid/src/test/java/com/ichi2/utils/StringUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.hamcrest.Matchers.nullValue
import org.hamcrest.Matchers.sameInstance
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.junit.JUnitAsserter.assertNull
import kotlin.test.junit5.JUnit5Asserter.assertNull

class StringUtilTest {
@Test
Expand Down
3 changes: 2 additions & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ apply from: "../lint.gradle"
dependencies {
implementation 'androidx.annotation:annotation:1.3.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2'
testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
testImplementation "org.junit.vintage:junit-vintage-engine:$junit_version"
testImplementation 'org.robolectric:robolectric:4.7.3'

lintChecks project(":lint-rules")
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
ext.acra_version = '5.7.0'
ext.ankidroid_backend_version = '0.1.10'
ext.hamcrest_version = '2.2'
ext.junit_version = '5.8.2'

repositories {
google()
Expand Down Expand Up @@ -44,6 +45,7 @@ subprojects {
includeAndroidResources = true
}
project.android.testOptions.unitTests.all {
useJUnitPlatform()
testLogging {
events "failed", "skipped"
showStackTraces = true
Expand Down
3 changes: 2 additions & 1 deletion lint-rules/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dependencies {

testImplementation "org.hamcrest:hamcrest:$hamcrest_version"
testImplementation "org.hamcrest:hamcrest-library:$hamcrest_version"
testImplementation 'junit:junit:4.13.2'
testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
testImplementation "org.junit.vintage:junit-vintage-engine:$junit_version"
testImplementation "com.android.tools.lint:lint:$lint_version"
testImplementation "com.android.tools.lint:lint-api:$lint_version"
testImplementation "com.android.tools.lint:lint-tests:$lint_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class JUnitNullAssertionDetector : Detector(), SourceCodeScanner {
val ID = "LegacyNullAssertionDetector"

@VisibleForTesting
val DESCRIPTION = "Use kotlin.test.assert[Not]Null OR kotlin.test.junit.JUnitAsserter instead of JUnit in Kotlin"
val DESCRIPTION = "Use kotlin.test.assert[Not]Null OR kotlin.test.junit5.JUnit5Asserter instead of JUnit in Kotlin"
private const val EXPLANATION = "JUnitAsserter's methods use contracts, removing the need for `!!` " +
"afterwards. Use JUnitAsserter if passing in a message, kotlin.test top level functions otherwise"
private val implementation = Implementation(JUnitNullAssertionDetector::class.java, JAVA_FILE_SCOPE)
Expand Down

0 comments on commit 8f9f8c2

Please sign in to comment.