BAEL-2769 Added MockK examples

This commit is contained in:
zbikenson 2019-03-26 19:16:49 +00:00
parent fda8a29eb6
commit 0b70193fd8
5 changed files with 208 additions and 0 deletions

View File

@ -140,6 +140,32 @@
<artifactId>kotlinx-collections-immutable</artifactId>
<version>0.1</version>
</dependency>
<!-- MockK dependencies-->
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>${mockk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.8.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.8.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
@ -154,6 +180,7 @@
<assertj.version>3.10.0</assertj.version>
<h2database.version>1.4.197</h2database.version>
<exposed.version>0.10.4</exposed.version>
<mockk.version>1.9.3</mockk.version>
</properties>
</project>

View File

@ -0,0 +1,44 @@
package com.baeldung.mockk
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class InjectTestService {
lateinit var service1: TestableService
lateinit var service2: TestableService
fun invokeService1(): String {
return service1.getDataFromDb("Test Param")
}
}
class AnnotationMockKUnitTest {
@MockK
lateinit var service1: TestableService
@MockK
lateinit var service2: TestableService
@InjectMockKs
var objectUnderTest = InjectTestService()
@BeforeEach
fun setUp() = MockKAnnotations.init(this)
@Test
fun givenServiceMock_whenCallingMockedMethod_thenCorrectlyVerified() {
// given
every { service1.getDataFromDb("Test Param") } returns "No"
// when
val result = objectUnderTest.invokeService1()
// then
assertEquals("No", result)
}
}

View File

@ -0,0 +1,92 @@
package com.baeldung.mockk
import io.mockk.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class BasicMockKUnitTest {
@Test
fun givenServiceMock_whenCallingMockedMethod_thenCorrectlyVerified() {
// given
val service = mockk<TestableService>()
every { service.getDataFromDb("Expected Param") } returns "Expected Output"
// when
val result = service.getDataFromDb("Expected Param")
// then
verify { service.getDataFromDb("Expected Param") }
assertEquals("Expected Output", result)
}
@Test
fun givenServiceSpy_whenMockingOnlyOneMethod_thenOtherMethodsShouldBehaveAsOriginalObject() {
// given
val service = spyk<TestableService>()
every { service.getDataFromDb(any()) } returns "Mocked Output"
// when checking mocked method
val firstResult = service.getDataFromDb("Any Param")
// then
assertEquals("Mocked Output", firstResult)
// when checking not mocked method
val secondResult = service.doSomethingElse("Any Param")
// then
assertEquals("I don't want to!", secondResult)
}
@Test
fun givenRelaxedMock_whenCallingNotMockedMethod_thenReturnDefaultValue() {
// given
val service = mockk<TestableService>(relaxed = true)
// when
val result = service.getDataFromDb("Any Param")
// then
assertEquals("", result)
}
@Test
fun givenObject_whenMockingIt_thenMockedMethodShouldReturnProperValue() {
// given
val service = TestableService()
mockkObject(service)
// when calling not mocked method
val firstResult = service.getDataFromDb("Any Param")
// then return real response
assertEquals("Value from DB", firstResult)
// when calling mocked method
every { service.getDataFromDb(any()) } returns "Mocked Output"
val secondResult = service.getDataFromDb("Any Param")
// then return mocked response
assertEquals("Mocked Output", secondResult)
}
@Test
fun givenMock_whenCapturingParamValue_thenProperValueShouldBeCaptured() {
// given
val service = mockk<TestableService>()
val slot = slot<String>()
every { service.getDataFromDb(capture(slot)) } returns "Expected Output"
// when
service.getDataFromDb("Expected Param")
// then
assertEquals("Expected Param", slot.captured)
}
@Test
fun givenMock_whenCapturingParamsValues_thenProperValuesShouldBeCaptured() {
// given
val service = mockk<TestableService>()
val list = mutableListOf<String>()
every { service.getDataFromDb(capture(list)) } returns "Expected Output"
// when
service.getDataFromDb("Expected Param 1")
service.getDataFromDb("Expected Param 2")
// then
assertEquals(2, list.size)
assertEquals("Expected Param 1", list[0])
assertEquals("Expected Param 2", list[1])
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.mockk
import io.mockk.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class Foo {
lateinit var name: String
lateinit var bar: Bar
}
class Bar {
lateinit var nickname: String
}
class HierarchicalMockKUnitTest {
@Test
fun givenHierarchicalClass_whenMockingIt_thenReturnProperValue() {
// given
val foo = mockk<Foo> {
every { name } returns "Karol"
every { bar } returns mockk {
every { nickname } returns "Tomato"
}
}
// when
val result = foo.bar.nickname
// then
assertEquals("Tomato", result)
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.mockk
class TestableService {
fun getDataFromDb(testParameter: String): String {
// query database and return matching value
return "Value from DB"
}
fun doSomethingElse(testParameter: String): String {
return "I don't want to!"
}
}