-
-
Notifications
You must be signed in to change notification settings - Fork 331
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 #34 from FredDeschenes/mockk-junit5
Mockk junit5
- Loading branch information
Showing
6 changed files
with
294 additions
and
3 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,95 @@ | ||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.3" | ||
} | ||
} | ||
plugins { | ||
id "jacoco" | ||
} | ||
apply plugin: "java" | ||
apply plugin: "kotlin-platform-jvm" | ||
apply plugin: "org.junit.platform.gradle.plugin" | ||
|
||
ext.junitJupiterVersion = '5.0.3' | ||
|
||
dependencies { | ||
compileOnly project(":mockk") | ||
|
||
compileOnly "org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion" | ||
|
||
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
compileOnly "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" | ||
|
||
testCompile project(":mockk") | ||
|
||
testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" | ||
|
||
testCompile "org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion" | ||
testCompile "org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion" | ||
} | ||
|
||
task sourcesJar(type: Jar) { | ||
classifier = 'sources' | ||
from sourceSets.main.allSource | ||
} | ||
|
||
artifacts { | ||
archives sourcesJar | ||
} | ||
|
||
jacocoTestReport { | ||
reports { | ||
xml.enabled true | ||
html.enabled true | ||
} | ||
} | ||
check.dependsOn jacocoTestReport | ||
|
||
uploadArchives { | ||
repositories { | ||
mavenDeployer { | ||
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } | ||
|
||
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { | ||
authentication(userName: rootProject.properties['ossrhUsername'] ?: '', password: rootProject.properties['ossrhPassword'] ?: '') | ||
} | ||
|
||
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { | ||
authentication(userName: rootProject.properties['ossrhUsername'] ?: '', password: rootProject.properties['ossrhPassword'] ?: '') | ||
} | ||
|
||
pom.project { | ||
name 'MockK' | ||
packaging 'jar' | ||
description 'mocking library for Kotlin' | ||
url 'http://mockk.io' | ||
|
||
scm { | ||
connection 'scm:git:git@github.com:oleksiyp/mockk.git' | ||
developerConnection 'scm:git:git@github.com:oleksiyp/mockk.git' | ||
url 'http://www.github.com/oleksiyp/mockk/' | ||
} | ||
|
||
licenses { | ||
license { | ||
name 'Apache License, Version 2.0' | ||
url 'http://www.apache.org/licenses/LICENSE-2.0' | ||
} | ||
} | ||
|
||
developers { | ||
developer { | ||
id 'oleksiyp' | ||
name 'Oleksii Pylypenko' | ||
email 'oleksiy.pylypenko@gmail.com' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
62 changes: 62 additions & 0 deletions
62
mockk-junit5/src/main/kotlin/io/mockk/junit5/MockKJUnit5Extension.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,62 @@ | ||
package io.mockk.junit5 | ||
|
||
import io.mockk.MockKAnnotations | ||
import io.mockk.MockKGateway | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.impl.annotations.RelaxedMockK | ||
import io.mockk.impl.annotations.SpyK | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.api.extension.ParameterContext | ||
import org.junit.jupiter.api.extension.ParameterResolver | ||
import org.junit.jupiter.api.extension.TestInstancePostProcessor | ||
import java.lang.reflect.Parameter | ||
|
||
/** | ||
* JUnit5 extension based on the Mockito extension found in JUnit5's samples. | ||
* Allows using the [MockK] and [RelaxedMockK] on class properties and test function parameters, | ||
* as well as [SpyK] on class properties. | ||
*/ | ||
class MockKJUnit5Extension : TestInstancePostProcessor, ParameterResolver { | ||
override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Boolean { | ||
val parameter = parameterContext.parameter | ||
return parameter.isAnnotationPresent(MockK::class.java) | ||
|| parameter.isAnnotationPresent(RelaxedMockK::class.java) | ||
} | ||
|
||
override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any? { | ||
return getMock(parameterContext.parameter, extensionContext) | ||
} | ||
|
||
private fun getMock(parameter: Parameter, extensionContext: ExtensionContext): Any? { | ||
val mockType = parameter.type | ||
|
||
return getMockKAnnotation(parameter)?.let { annotation -> | ||
val mockName = getMockName(parameter, mockType, annotation) | ||
io.mockk.MockK.useImpl { | ||
MockKGateway.implementation().mockFactory.mockk(mockType.kotlin, mockName, annotation is RelaxedMockK, arrayOf()) | ||
} | ||
} | ||
} | ||
|
||
private fun getMockKAnnotation(parameter: Parameter): Any? { | ||
return parameter.getAnnotation(MockK::class.java) ?: parameter.getAnnotation(RelaxedMockK::class.java) | ||
} | ||
|
||
private fun getMockName(parameter: Parameter, mockType: Class<*>, annotation: Any): String { | ||
val fromAnnotation = when (annotation) { | ||
is MockK -> annotation.name.trim() | ||
is RelaxedMockK -> annotation.name.trim() | ||
else -> "" | ||
} | ||
|
||
return when { | ||
fromAnnotation.isNotEmpty() -> fromAnnotation | ||
parameter.isNamePresent -> parameter.name | ||
else -> mockType.canonicalName | ||
} | ||
} | ||
|
||
override fun postProcessTestInstance(testInstance: Any, context: ExtensionContext) { | ||
MockKAnnotations.init(testInstance) | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
mockk-junit5/src/test/kotlin/io/mockk/junit5/MockKJUnit5ExtensionTest.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,103 @@ | ||
package io.mockk.junit5 | ||
|
||
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.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
enum class Direction { | ||
NORTH, | ||
SOUTH, | ||
EAST, | ||
WEST | ||
} | ||
|
||
enum class Outcome { | ||
FAILURE, | ||
RECORDED | ||
} | ||
|
||
class RelaxedOutcome | ||
|
||
class Car { | ||
@Suppress("UNUSED_PARAMETER") | ||
fun recordTelemetry(speed: Int, direction: Direction, lat: Double, long: Double): Outcome { | ||
println("REAL METHOD CALLED") | ||
return Outcome.FAILURE | ||
} | ||
|
||
fun relaxedTest(): RelaxedOutcome? { | ||
return null | ||
} | ||
} | ||
|
||
@ExtendWith(MockKJUnit5Extension::class) | ||
class MockKJUnit5ExtensionTest { | ||
@MockK | ||
private lateinit var car2: Car | ||
|
||
@RelaxedMockK | ||
private lateinit var relaxedCar: Car | ||
|
||
@SpyK | ||
private var carSpy = Car() | ||
|
||
@Test | ||
fun injectsValidMockInMethods(@MockK car: Car) { | ||
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) | ||
|
||
Assertions.assertEquals(Outcome.RECORDED, result) | ||
} | ||
|
||
@Test | ||
fun injectsValidMockInClass() { | ||
every { | ||
car2.recordTelemetry( | ||
speed = more(50), | ||
direction = Direction.NORTH, | ||
lat = any(), | ||
long = any() | ||
) | ||
} returns Outcome.RECORDED | ||
|
||
val result = car2.recordTelemetry(51, Direction.NORTH, 1.0, 2.0) | ||
|
||
Assertions.assertEquals(Outcome.RECORDED, result) | ||
} | ||
|
||
@Test | ||
fun injectsValidRelaxedMockInMethods(@RelaxedMockK car: Car) { | ||
val result = car.relaxedTest() | ||
|
||
Assertions.assertTrue(result is RelaxedOutcome) | ||
} | ||
|
||
@Test | ||
fun injectsValidRelaxedMockInClass() { | ||
val result = relaxedCar.relaxedTest() | ||
|
||
Assertions.assertTrue(result is RelaxedOutcome) | ||
} | ||
|
||
@Test | ||
fun testInjectsValidSpyInClass() { | ||
val result = carSpy.relaxedTest() | ||
|
||
Assertions.assertNull(result) | ||
|
||
verify { carSpy.relaxedTest() } | ||
} | ||
} |
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