Merge branch 'master' into master
This commit is contained in:
@@ -10,5 +10,5 @@
|
||||
- [Introduction to Arrow in Kotlin](https://www.baeldung.com/kotlin-arrow)
|
||||
- [Kotlin with Ktor](https://www.baeldung.com/kotlin-ktor)
|
||||
- [REST API With Kotlin and Kovert](https://www.baeldung.com/kotlin-kovert)
|
||||
- [Kotlin Immutable Collections](https://www.baeldung.com/kotlin-immutable-collections)
|
||||
- [MockK: A Mocking Library for Kotlin](https://www.baeldung.com/kotlin-mockk)
|
||||
- [Kotlin Immutable Collections](https://www.baeldung.com/kotlin-immutable-collections)
|
||||
@@ -19,6 +19,14 @@
|
||||
<name>exposed</name>
|
||||
<url>https://dl.bintray.com/kotlin/exposed</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<id>kotlinx</id>
|
||||
<name>bintray</name>
|
||||
<url>https://dl.bintray.com/kotlin/kotlinx</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
@@ -112,9 +120,56 @@
|
||||
<version>3.3.0</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>27.1-jre</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-collections-immutable -->
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
<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>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
@@ -125,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>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.kotlin.immutable
|
||||
|
||||
import junit.framework.Assert.assertEquals
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.immutableListOf
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
|
||||
class KotlinxImmutablesUnitTest{
|
||||
|
||||
|
||||
@Rule
|
||||
@JvmField
|
||||
var ee : ExpectedException = ExpectedException.none()
|
||||
|
||||
@Test
|
||||
fun givenKICLList_whenAddTried_checkExceptionThrown(){
|
||||
|
||||
val list: ImmutableList<String> = immutableListOf("I", "am", "immutable")
|
||||
|
||||
list.add("My new item")
|
||||
|
||||
assertEquals(listOf("I", "am", "immutable"), list)
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.kotlin.immutable
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import junit.framework.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
|
||||
class ReadOnlyUnitTest{
|
||||
|
||||
@Test
|
||||
fun givenReadOnlyList_whenCastToMutableList_checkNewElementsAdded(){
|
||||
|
||||
val list: List<String> = listOf("This", "Is", "Totally", "Immutable")
|
||||
|
||||
(list as MutableList<String>)[2] = "Not"
|
||||
|
||||
assertEquals(listOf("This", "Is", "Not", "Immutable"), list)
|
||||
|
||||
}
|
||||
|
||||
@Rule
|
||||
@JvmField
|
||||
var ee : ExpectedException = ExpectedException.none()
|
||||
|
||||
@Test
|
||||
fun givenImmutableList_whenAddTried_checkExceptionThrown(){
|
||||
|
||||
val list: List<String> = ImmutableList.of("I", "am", "actually", "immutable")
|
||||
|
||||
ee.expect(UnsupportedOperationException::class.java)
|
||||
|
||||
(list as MutableList<String>).add("Oops")
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenMutableList_whenCopiedAndAddTried_checkExceptionThrown(){
|
||||
|
||||
val mutableList : List<String> = listOf("I", "Am", "Definitely", "Immutable")
|
||||
|
||||
(mutableList as MutableList<String>)[2] = "100% Not"
|
||||
|
||||
assertEquals(listOf("I", "Am", "100% Not", "Immutable"), mutableList)
|
||||
|
||||
val list: List<String> = ImmutableList.copyOf(mutableList)
|
||||
|
||||
ee.expect(UnsupportedOperationException::class.java)
|
||||
|
||||
(list as MutableList<String>)[2] = "Really?"
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenImmutableSetBuilder_whenAddTried_checkExceptionThrown(){
|
||||
|
||||
val mutableList : List<String> = listOf("Hello", "Baeldung")
|
||||
val set: ImmutableSet<String> = ImmutableSet.builder<String>()
|
||||
.add("I","am","immutable")
|
||||
.addAll(mutableList)
|
||||
.build()
|
||||
|
||||
assertEquals(setOf("Hello", "Baeldung", "I", "am", "immutable"), set)
|
||||
|
||||
ee.expect(UnsupportedOperationException::class.java)
|
||||
|
||||
(set as MutableSet<String>).add("Oops")
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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])
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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!"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user