ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - Ninja-Squad/springmockk: SpringMockK: MockBean and SpyBean, but for MockK instead of Mockito
SpringMockK: MockBean and SpyBean, but for MockK instead of Mockito - Ninja-Squad/springmockk
Visit Site

GitHub - Ninja-Squad/springmockk: SpringMockK: MockBean and SpyBean, but for MockK instead of Mockito

GitHub - Ninja-Squad/springmockk: SpringMockK: MockBean and SpyBean, but for MockK instead of Mockito

SpringMockK

CircleCI

Support for Spring Boot integration tests written in Kotlin using MockK 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):

@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:

testImplementation("com.ninja-squad: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:

testImplementation("org.springframework.boot:spring-boot-starter-test") {
    exclude(module = "mockito-core")
}

Maven

Add this to your dependencies:

<dependency>
  <groupId>com.ninja-squad</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 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

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

For Maven users:

<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, 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, 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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory