New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a JUnit 4 rule #816
Merged
Merged
Added a JUnit 4 rule #816
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() } | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need
RunWith(JUnit4::class)
here for the tests to passThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated