Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #816 from jonapoul/master
Added a JUnit 4 rule
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 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
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,44 @@ | ||
package io.mockk.junit4 | ||
|
||
import io.mockk.MockKAnnotations | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.impl.annotations.RelaxedMockK | ||
import io.mockk.unmockkAll | ||
import org.junit.rules.TestRule | ||
import org.junit.rules.TestWatcher | ||
import org.junit.runner.Description | ||
|
||
/** | ||
* A simple JUnit 4 rule which handles the setting up and tearing down of mock objects using the | ||
* [MockK] or [RelaxedMockK] annotations at the beginning and end of each test respectively - | ||
* without needing to manually call [unmockkAll] or [MockKAnnotations.init]. | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* class ExampleTest { | ||
* @get:Rule | ||
* val mockkRule = MockKRule(this) | ||
* | ||
* @MockK | ||
* private lateinit var car: Car | ||
* | ||
* @Test | ||
* fun something() { | ||
* every { car.drive() } just runs | ||
* ... | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
class MockKRule(private val testSubject: Any) : TestWatcher(), TestRule { | ||
override fun starting(description: Description?) { | ||
super.starting(description) | ||
MockKAnnotations.init(testSubject) | ||
} | ||
|
||
override fun finished(description: Description?) { | ||
super.finished(description) | ||
unmockkAll() | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
mockk/jvm/src/test/kotlin/io/mockk/junit4/MockKRuleTest.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,87 @@ | ||
@file:Suppress("UNUSED_PARAMETER") | ||
|
||
package io.mockk.junit4 | ||
|
||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.impl.annotations.RelaxedMockK | ||
import io.mockk.impl.annotations.SpyK | ||
import io.mockk.verify | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
class MockKRuleTest { | ||
|
||
@get:Rule | ||
val mockkRule = MockKRule(this) | ||
|
||
enum class Direction { | ||
NORTH, | ||
SOUTH, | ||
EAST, | ||
WEST | ||
} | ||
|
||
enum class Outcome { | ||
FAILURE, | ||
RECORDED | ||
} | ||
|
||
class RelaxedOutcome | ||
|
||
class Car { | ||
fun recordTelemetry(speed: Int, direction: Direction, lat: Double, long: Double): Outcome { | ||
return Outcome.FAILURE | ||
} | ||
|
||
fun relaxedTest(): RelaxedOutcome? { | ||
return null | ||
} | ||
} | ||
|
||
@MockK | ||
private lateinit var car: Car | ||
|
||
@RelaxedMockK | ||
private lateinit var relaxedCar: Car | ||
|
||
@SpyK | ||
private var carSpy = Car() | ||
|
||
@Test | ||
fun injectsValidMockInClass() { | ||
every { | ||
car.recordTelemetry( | ||
speed = more(50), | ||
direction = Direction.NORTH, | ||
lat = any(), | ||
long = any() | ||
) | ||
} returns Outcome.RECORDED | ||
|
||
val result = car.recordTelemetry(51, Direction.NORTH, 1.0, 2.0) | ||
|
||
assertEquals(Outcome.RECORDED, result) | ||
} | ||
|
||
@Test | ||
fun injectsValidRelaxedMockInClass() { | ||
val result = relaxedCar.relaxedTest() | ||
|
||
assertTrue(result is RelaxedOutcome) | ||
} | ||
|
||
@Test | ||
fun testInjectsValidSpyInClass() { | ||
val result = carSpy.relaxedTest() | ||
|
||
assertNull(result) | ||
|
||
verify { carSpy.relaxedTest() } | ||
} | ||
} |