-
-
Notifications
You must be signed in to change notification settings - Fork 331
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
oleksiyp
wants to merge
5
commits into
master
Choose a base branch
from
springmockk
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 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
23 changes: 23 additions & 0 deletions
23
buildSrc/src/main/kotlin/buildsrc/convention/kotlin-jvm-spring.gradle.kts
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,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() | ||
} | ||
} |
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,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
``` |
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,146 @@ | ||
public final class io/mockk/springmockk/ClearMocksTestExecutionListener : org/springframework/test/context/support/AbstractTestExecutionListener { | ||
public fun <init> ()V | ||
public fun afterTestMethod (Lorg/springframework/test/context/TestContext;)V | ||
public fun beforeTestMethod (Lorg/springframework/test/context/TestContext;)V | ||
public fun getOrder ()I | ||
} | ||
|
||
public class io/mockk/springmockk/Definition { | ||
public fun <init> (Ljava/lang/String;Lio/mockk/springmockk/MockkClear;Lio/mockk/springmockk/QualifierDefinition;)V | ||
public fun equals (Ljava/lang/Object;)Z | ||
public final fun getClear ()Lio/mockk/springmockk/MockkClear; | ||
public final fun getName ()Ljava/lang/String; | ||
public final fun getQualifier ()Lio/mockk/springmockk/QualifierDefinition; | ||
public fun hashCode ()I | ||
} | ||
|
||
public final class io/mockk/springmockk/DefinitionsParser { | ||
public fun <init> ()V | ||
public fun <init> (Ljava/util/Collection;)V | ||
public synthetic fun <init> (Ljava/util/Collection;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public final fun getField (Lio/mockk/springmockk/Definition;)Ljava/lang/reflect/Field; | ||
public final fun getParsedDefinitions ()Ljava/util/Set; | ||
public final fun parse (Ljava/lang/Class;)V | ||
} | ||
|
||
public abstract interface annotation class io/mockk/springmockk/MockkBean : java/lang/annotation/Annotation { | ||
public abstract fun classes ()[Ljava/lang/Class; | ||
public abstract fun clear ()Lio/mockk/springmockk/MockkClear; | ||
public abstract fun extraInterfaces ()[Ljava/lang/Class; | ||
public abstract fun name ()Ljava/lang/String; | ||
public abstract fun relaxUnitFun ()Z | ||
public abstract fun relaxed ()Z | ||
public abstract fun value ()[Ljava/lang/Class; | ||
} | ||
|
||
public abstract interface annotation class io/mockk/springmockk/MockkBeans : java/lang/annotation/Annotation { | ||
public abstract fun value ()[Lio/mockk/springmockk/MockkBean; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkClear : java/lang/Enum { | ||
public static final field AFTER Lio/mockk/springmockk/MockkClear; | ||
public static final field BEFORE Lio/mockk/springmockk/MockkClear; | ||
public static final field Companion Lio/mockk/springmockk/MockkClear$Companion; | ||
public static final field NONE Lio/mockk/springmockk/MockkClear; | ||
public static fun valueOf (Ljava/lang/String;)Lio/mockk/springmockk/MockkClear; | ||
public static fun values ()[Lio/mockk/springmockk/MockkClear; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkClear$Companion { | ||
public final fun get (Ljava/lang/Object;)Lio/mockk/springmockk/MockkClear; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkClearKt { | ||
public static final fun clear (Ljava/lang/Object;Lio/mockk/springmockk/MockkClear;)Ljava/lang/Object; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkContextCustomizer : org/springframework/test/context/ContextCustomizer { | ||
public fun <init> (Ljava/util/Set;)V | ||
public final fun copy (Ljava/util/Set;)Lio/mockk/springmockk/MockkContextCustomizer; | ||
public static synthetic fun copy$default (Lio/mockk/springmockk/MockkContextCustomizer;Ljava/util/Set;ILjava/lang/Object;)Lio/mockk/springmockk/MockkContextCustomizer; | ||
public fun customizeContext (Lorg/springframework/context/ConfigurableApplicationContext;Lorg/springframework/test/context/MergedContextConfiguration;)V | ||
public fun equals (Ljava/lang/Object;)Z | ||
public fun hashCode ()I | ||
public fun toString ()Ljava/lang/String; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkContextCustomizerFactory : org/springframework/test/context/ContextCustomizerFactory { | ||
public fun <init> ()V | ||
public fun createContextCustomizer (Ljava/lang/Class;Ljava/util/List;)Lorg/springframework/test/context/ContextCustomizer; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkDefinition : io/mockk/springmockk/Definition { | ||
public fun <init> (Ljava/lang/String;Lorg/springframework/core/ResolvableType;[Lkotlin/reflect/KClass;Lio/mockk/springmockk/MockkClear;ZZLio/mockk/springmockk/QualifierDefinition;)V | ||
public synthetic fun <init> (Ljava/lang/String;Lorg/springframework/core/ResolvableType;[Lkotlin/reflect/KClass;Lio/mockk/springmockk/MockkClear;ZZLio/mockk/springmockk/QualifierDefinition;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public final fun createMock ()Ljava/lang/Object; | ||
public final fun createMock (Ljava/lang/String;)Ljava/lang/Object; | ||
public fun equals (Ljava/lang/Object;)Z | ||
public final fun getExtraInterfaces ()Ljava/util/Set; | ||
public final fun getRelaxUnitFun ()Z | ||
public final fun getRelaxed ()Z | ||
public final fun getTypeToMock ()Lorg/springframework/core/ResolvableType; | ||
public fun hashCode ()I | ||
public fun toString ()Ljava/lang/String; | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkFunctionsKt { | ||
public static final fun isMock (Ljava/lang/Object;)Z | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkPostProcessor : org/springframework/beans/factory/BeanClassLoaderAware, org/springframework/beans/factory/BeanFactoryAware, org/springframework/beans/factory/config/BeanFactoryPostProcessor, org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor, org/springframework/core/Ordered { | ||
public static final field Companion Lio/mockk/springmockk/MockkPostProcessor$Companion; | ||
public fun <init> (Ljava/util/Set;)V | ||
public fun getOrder ()I | ||
public fun postProcessBeanFactory (Lorg/springframework/beans/factory/config/ConfigurableListableBeanFactory;)V | ||
public fun postProcessProperties (Lorg/springframework/beans/PropertyValues;Ljava/lang/Object;Ljava/lang/String;)Lorg/springframework/beans/PropertyValues; | ||
public fun setBeanClassLoader (Ljava/lang/ClassLoader;)V | ||
public fun setBeanFactory (Lorg/springframework/beans/factory/BeanFactory;)V | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkPostProcessor$Companion { | ||
public final fun register (Lorg/springframework/beans/factory/support/BeanDefinitionRegistry;Ljava/lang/Class;Ljava/util/Set;)V | ||
public static synthetic fun register$default (Lio/mockk/springmockk/MockkPostProcessor$Companion;Lorg/springframework/beans/factory/support/BeanDefinitionRegistry;Ljava/lang/Class;Ljava/util/Set;ILjava/lang/Object;)V | ||
} | ||
|
||
public final class io/mockk/springmockk/MockkTestExecutionListener : org/springframework/test/context/support/AbstractTestExecutionListener { | ||
public fun <init> ()V | ||
public fun beforeTestMethod (Lorg/springframework/test/context/TestContext;)V | ||
public fun getOrder ()I | ||
public fun prepareTestInstance (Lorg/springframework/test/context/TestContext;)V | ||
} | ||
|
||
public final class io/mockk/springmockk/QualifierDefinition { | ||
public static final field Companion Lio/mockk/springmockk/QualifierDefinition$Companion; | ||
public fun <init> (Ljava/lang/reflect/Field;Ljava/util/Set;)V | ||
public final fun applyTo (Lorg/springframework/beans/factory/support/RootBeanDefinition;)V | ||
public fun equals (Ljava/lang/Object;)Z | ||
public fun hashCode ()I | ||
public final fun matches (Lorg/springframework/beans/factory/config/ConfigurableListableBeanFactory;Ljava/lang/String;)Z | ||
} | ||
|
||
public final class io/mockk/springmockk/QualifierDefinition$Companion { | ||
public final fun forElement (Ljava/lang/reflect/AnnotatedElement;)Lio/mockk/springmockk/QualifierDefinition; | ||
} | ||
|
||
public abstract interface annotation class io/mockk/springmockk/SpykBean : java/lang/annotation/Annotation { | ||
public abstract fun classes ()[Ljava/lang/Class; | ||
public abstract fun clear ()Lio/mockk/springmockk/MockkClear; | ||
public abstract fun name ()Ljava/lang/String; | ||
public abstract fun value ()[Ljava/lang/Class; | ||
} | ||
|
||
public abstract interface annotation class io/mockk/springmockk/SpykBeans : java/lang/annotation/Annotation { | ||
public abstract fun value ()[Lio/mockk/springmockk/SpykBean; | ||
} | ||
|
||
public final class io/mockk/springmockk/SpykDefinition : io/mockk/springmockk/Definition { | ||
public fun <init> (Ljava/lang/String;Lorg/springframework/core/ResolvableType;Lio/mockk/springmockk/MockkClear;Lio/mockk/springmockk/QualifierDefinition;)V | ||
public synthetic fun <init> (Ljava/lang/String;Lorg/springframework/core/ResolvableType;Lio/mockk/springmockk/MockkClear;Lio/mockk/springmockk/QualifierDefinition;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public final fun createSpy (Ljava/lang/Object;)Ljava/lang/Object; | ||
public final fun createSpy (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; | ||
public fun equals (Ljava/lang/Object;)Z | ||
public final fun getTypeToSpy ()Lorg/springframework/core/ResolvableType; | ||
public fun hashCode ()I | ||
public fun toString ()Ljava/lang/String; | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
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