[BAEL-16649] split kotlin-libraries module

This commit is contained in:
Sjmillington
2019-10-17 16:23:02 +01:00
parent 468c8e0875
commit ad6fe27389
10 changed files with 23 additions and 23 deletions

View File

@@ -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)
}
}

View File

@@ -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")
}
}

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!"
}
}