Skip to content
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

Embedding springmockk 4.0.2 into mockk #1218

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 0 additions & 29 deletions MAINTAINING.md

This file was deleted.

323 changes: 0 additions & 323 deletions MATRIX.md

This file was deleted.

1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinPluginVersion")

implementation("org.jetbrains.kotlinx.kover:org.jetbrains.kotlinx.kover.gradle.plugin:$kotlinxKover")
implementation("org.jetbrains.kotlin.plugin.spring:org.jetbrains.kotlin.plugin.spring.gradle.plugin:$kotlinPluginVersion")

implementation("com.android.tools.build:gradle:$androidGradle")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:$dokka")
Expand Down
16 changes: 15 additions & 1 deletion buildSrc/src/main/kotlin/buildsrc/config/Deps.kt
Expand Up @@ -16,8 +16,14 @@ object Deps {
const val coroutines = "1.6.4"
const val slfj = "2.0.5"
const val logback = "1.4.5"
const val junitJupiter = "5.8.2"
const val junitJupiter = "5.10.2"
const val junit4 = "4.13.2"
const val assertj = "3.25.3"

const val kotlinReflect = "1.7.21"

const val springBoot = "3.2.2"
const val spring = "6.1.3"

const val byteBuddy = "1.14.6"
const val objenesis = "3.3"
Expand All @@ -40,6 +46,14 @@ object Deps {

const val junit4 = "junit:junit:${Versions.junit4}"
const val junitJupiter = "org.junit.jupiter:junit-jupiter:${Versions.junitJupiter}"
const val junitJupiterParams = "org.junit.jupiter:junit-jupiter-params:${Versions.junitJupiter}"
const val assertj = "org.assertj:assertj-core:${Versions.assertj}"

const val kotlinReflect = "org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlinReflect}"

const val springBootTest = "org.springframework.boot:spring-boot-test:${Versions.springBoot}"
const val springTest = "org.springframework:spring-test:${Versions.spring}"
const val springContext = "org.springframework:spring-context:${Versions.spring}"

const val kotlinCoroutinesBom = "org.jetbrains.kotlinx:kotlinx-coroutines-bom:${Versions.coroutines}"
const val kotlinCoroutinesCore = "org.jetbrains.kotlinx:kotlinx-coroutines-core"
Expand Down
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/buildsrc/config/publishing.kt
Expand Up @@ -30,6 +30,12 @@ fun MavenPublication.createMockKPom(
name.set("Mattia Tommasone")
email.set("raibaz@gmail.com")
}
developer {
// springmockk author
id.set("jnizet")
name.set("Jean-Baptiste Nizet")
email.set("jb@ninja-squad.com")
}
}

licenses {
Expand Down
@@ -0,0 +1,23 @@
package buildsrc.convention

import buildsrc.config.Deps
import org.gradle.api.JavaVersion
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("buildsrc.convention.kotlin-jvm")
id("org.jetbrains.kotlin.plugin.spring")
}

tasks.withType<JavaCompile>().configureEach {
sourceCompatibility = JavaVersion.VERSION_17.toString()
targetCompatibility = JavaVersion.VERSION_17.toString()
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
}
Expand Up @@ -14,7 +14,7 @@ plugins {
}

java {
withJavadocJar()
// withJavadocJar()
withSourcesJar()
}

Expand All @@ -31,6 +31,7 @@ tasks.withType<KotlinCompile>().configureEach {
}
}

tasks.named<Jar>("javadocJar") {
val javadocJar by tasks.registering(Jar::class) {
from(tasks.dokkaJavadoc)
}
archiveClassifier.set("javadoc")
}
135 changes: 135 additions & 0 deletions modules/springmockk/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also update root README as it points to old repository.

Specifically I mean this line:
https://github.com/mockk/mockk/blob/master/README.md?plain=1#L88

@@ -0,0 +1,135 @@
# Inclusion of springmockk into mockk

It was agreed that further maintenance of springmockk is done by mockk community
and project springmockk codebase is included in mockk.

Codebase is based on version springmockk 4.0.2 dcbe643 and tuned by
kkurczewski in https://github.com/kkurczewski/springmockk/tree/migration

Comment on lines +1 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this note make more sense in commit message. Let say a year from now it won't be that much important for readers to include it in very top of README.

# SpringMockK

Support for Spring Boot integration tests written in Kotlin using [MockK](https://mockk.io/) instead of Mockito.

Spring Boot provides `@MockBean` and `@SpyBean` annotations for integration tests, which create mock/spy beans using Mockito.

This project provides equivalent annotations `MockkBean` and `SpykBean` to do the exact same thing with MockK.

## Principle

All the Mockito-specific classes of the spring-boot-test library, including the automated tests, have been cloned, translated to Kotlin, and adapted to MockK.

This library thus provides the same functionality as the standard Mockito-based Spring Boot mock beans.

For example (using JUnit 5, but you can of course also use JUnit 4):

```kotlin
@ExtendWith(SpringExtension::class)
@WebMvcTest
class GreetingControllerTest {
@MockkBean
private lateinit var greetingService: GreetingService

@Autowired
private lateinit var controller: GreetingController

@Test
fun `should greet by delegating to the greeting service`() {
every { greetingService.greet("John") } returns "Hi John"

assertThat(controller.greet("John")).isEqualTo("Hi John")
verify { greetingService.greet("John") }
}
}
```

## Usage

### Gradle (Kotlin DSL)

Add this to your dependencies:
```kotlin
testImplementation("io.mockk:springmockk:4.0.2")
```

If you want to make sure Mockito (and the standard `MockBean` and `SpyBean` annotations) is not used, you can also exclude the mockito dependency:
```kotlin
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(module = "mockito-core")
}
```

### Maven

Add this to your dependencies:
```xml
<dependency>
<groupId>io.mockk</groupId>
<artifactId>springmockk</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
```

## Differences with Mockito

- the MockK defaults are used, which means that mocks created by the annotations are strict (i.e. not relaxed) by default. But [you can configure MockK](https://mockk.io/#settings-file) to use different defaults globally, or you can use `@MockkBean(relaxed = true)` or `@MockkBean(relaxUnitFun = true)`.
- the created mocks can't be serializable as they can be with Mockito (AFAIK, MockK doesn't support that feature)

## Gotchas

In some situations, the beans that need to be spied are JDK proxies. In recent versions of Java (Java 16+ AFAIK),
MockK can't spy JDK proxies unless you pass the argument `--add-opens java.base/java.lang.reflect=ALL-UNNAMED`
to the JVM running the tests.

Not doing that and trying to spy on a JDK proxy will lead to an error such as

```
java.lang.IllegalAccessException: class io.mockk.impl.InternalPlatform cannot access a member of class java.lang.reflect.Proxy (in module java.base) with modifiers "protected"
```

To pass that option to the test JVM with Gradle, configure the test task with

```kotlin
tasks.test {
// ...
jvmArgs(
"--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED"
)
}
```

For Maven users:

```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--add-opens java.base/java.lang.reflect=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
````

## Limitations
- the [issue 5837](https://github.com/spring-projects/spring-boot/issues/5837), which has been fixed for Mockito spies using Mockito-specific features, also exists with MockK, and hasn't been fixed yet.
If you have a good idea, please tell!
- [this is not an official Spring Boot project](https://github.com/spring-projects/spring-boot/issues/15749), so it might not work out of the box for newest versions if backwards incompatible changes are introduced in Spring Boot.
Please file issues if you find problems.
- annotations are looked up on fields, and not on properties (for now).
This doesn't matter much until you use a custom qualifier annotation.
In that case, make sure that it targets fields and not properties, or use `@field:YourQualifier` to apply it on your beans.

## Versions compatibility

- Version 4.x of SpringMockK: compatible with Spring Boot 3.x, Java 17+
- Version 3.x of SpringMockK: compatible with Spring Boot 2.4.x, 2.5.x and 2.6.x, Java 8+
- Version 2.x of SpringMockK: compatible with Spring Boot 2.2.x and 2.3.x, Java 8+
- Version 1.x of SpringMockK: compatible with Spring Boot 2.1.x, Java 8+

## How to build

```
./gradlew build
```