BAEL-2230 Equivalent of Java static final in Kotlin (#5848)

* constant unit test

* kotlin const
This commit is contained in:
Hai Nguyen 2018-12-18 00:39:41 +08:00 committed by Josh Cummings
parent 2bec14597a
commit 4a55426727
3 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import com.baeldung.kotlin.constant.TestKotlinConstantClass
import com.baeldung.kotlin.constant.TestKotlinConstantObject
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class ConstantUnitTest {
@Test
fun givenConstant_whenCompareWithActualValue_thenReturnTrue() {
assertEquals(10, TestKotlinConstantObject.COMPILE_TIME_CONST)
assertEquals(30, TestKotlinConstantObject.RUN_TIME_CONST)
assertEquals(20, TestKotlinConstantObject.JAVA_STATIC_FINAL_FIELD)
assertEquals(40, TestKotlinConstantClass.COMPANION_OBJECT_NUMBER)
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.kotlin.constant
class TestKotlinConstantClass {
companion object {
const val COMPANION_OBJECT_NUMBER = 40
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.kotlin.constant
object TestKotlinConstantObject {
const val COMPILE_TIME_CONST = 10
val RUN_TIME_CONST: Int
@JvmField
val JAVA_STATIC_FINAL_FIELD = 20
init {
RUN_TIME_CONST = TestKotlinConstantObject.COMPILE_TIME_CONST + 20;
}
}