-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test for repeated, test template and nested tests (#21)
- Loading branch information
Showing
5 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...rc/test/kotlin/tech/apter/junit/jupiter/robolectric/RobolectricExtensionNestedSelfTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tech.apter.junit.jupiter.robolectric | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertNotNull | ||
|
||
@ExtendWith(RobolectricExtension::class) | ||
class RobolectricExtensionNestedSelfTest { | ||
@Nested | ||
inner class NestedSelfTest { | ||
@Test | ||
fun `Given a test extended with robolectric when call a nested test then robolectric should be available`() { | ||
val application = assertDoesNotThrow { ApplicationProvider.getApplicationContext<Context>() } | ||
assertNotNull(application) | ||
assertIs<Application>(application, "application") | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../test/kotlin/tech/apter/junit/jupiter/robolectric/RobolectricExtensionRepeatedSelfTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package tech.apter.junit.jupiter.robolectric | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.RepeatedTest | ||
import org.junit.jupiter.api.RepetitionInfo | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertNotNull | ||
|
||
@ExtendWith(RobolectricExtension::class) | ||
class RobolectricExtensionRepeatedSelfTest { | ||
|
||
@RepeatedTest(REPEATED_TEST_COUNT) | ||
fun `Given a test extended with robolectric when call a repeated test then robolectric should be available`( | ||
testInfo: RepetitionInfo, | ||
) { | ||
testCallCount++ | ||
val application = assertDoesNotThrow { ApplicationProvider.getApplicationContext<Context>() } | ||
assertNotNull(application) | ||
assertIs<Application>(application, "application") | ||
assertEquals(REPEATED_TEST_COUNT, testInfo.totalRepetitions) | ||
} | ||
|
||
companion object { | ||
private const val REPEATED_TEST_COUNT = 3 | ||
private var testCallCount: Int = 0 | ||
|
||
@AfterAll | ||
@Throws(Exception::class) | ||
@JvmStatic | ||
fun `Repeated test should be called as much as REPEATED_TEST_COUNT`() { | ||
assertEquals(REPEATED_TEST_COUNT, testCallCount) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...t/kotlin/tech/apter/junit/jupiter/robolectric/RobolectricExtensionTestTemplateSelfTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package tech.apter.junit.jupiter.robolectric | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.TestTemplate | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContext | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider | ||
import java.util.stream.Stream | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertNotNull | ||
|
||
@ExtendWith(RobolectricExtension::class) | ||
class RobolectricExtensionTestTemplateSelfTest { | ||
|
||
@TestTemplate | ||
@ExtendWith(RobolectricTestTemplateInvocationContextProvider::class) | ||
fun `Given a test extended with robolectric when call a test template then robolectric should be available`() { | ||
testCallCount++ | ||
val application = assertDoesNotThrow { ApplicationProvider.getApplicationContext<Context>() } | ||
assertNotNull(application) | ||
assertIs<Application>(application, "application") | ||
} | ||
|
||
companion object { | ||
private var testCallCount: Int = 0 | ||
|
||
@AfterAll | ||
@Throws(Exception::class) | ||
@JvmStatic | ||
fun `Test template should be called as much as defined RobolectricTestTemplateInvocationContextProvider`() { | ||
assertEquals(2, testCallCount) | ||
} | ||
} | ||
} | ||
|
||
private fun noOpTestTemplateInvocationContext(): TestTemplateInvocationContext = | ||
object : TestTemplateInvocationContext {} | ||
|
||
private class RobolectricTestTemplateInvocationContextProvider : TestTemplateInvocationContextProvider { | ||
override fun supportsTestTemplate(context: ExtensionContext): Boolean = true | ||
|
||
override fun provideTestTemplateInvocationContexts(context: ExtensionContext): Stream<TestTemplateInvocationContext> { | ||
return Stream.of(noOpTestTemplateInvocationContext(), noOpTestTemplateInvocationContext()) | ||
} | ||
} |