Skip to content

Commit 4a55426

Browse files
nnhai1991jzheaux
authored andcommitted
BAEL-2230 Equivalent of Java static final in Kotlin (eugenp#5848)
* constant unit test * kotlin const
1 parent 2bec145 commit 4a55426

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import com.baeldung.kotlin.constant.TestKotlinConstantClass
2+
import com.baeldung.kotlin.constant.TestKotlinConstantObject
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
class ConstantUnitTest {
7+
8+
@Test
9+
fun givenConstant_whenCompareWithActualValue_thenReturnTrue() {
10+
assertEquals(10, TestKotlinConstantObject.COMPILE_TIME_CONST)
11+
assertEquals(30, TestKotlinConstantObject.RUN_TIME_CONST)
12+
assertEquals(20, TestKotlinConstantObject.JAVA_STATIC_FINAL_FIELD)
13+
14+
assertEquals(40, TestKotlinConstantClass.COMPANION_OBJECT_NUMBER)
15+
}
16+
}
17+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.kotlin.constant
2+
3+
4+
class TestKotlinConstantClass {
5+
companion object {
6+
const val COMPANION_OBJECT_NUMBER = 40
7+
}
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.kotlin.constant
2+
3+
4+
object TestKotlinConstantObject {
5+
const val COMPILE_TIME_CONST = 10
6+
7+
val RUN_TIME_CONST: Int
8+
9+
@JvmField
10+
val JAVA_STATIC_FINAL_FIELD = 20
11+
12+
init {
13+
RUN_TIME_CONST = TestKotlinConstantObject.COMPILE_TIME_CONST + 20;
14+
}
15+
}

0 commit comments

Comments
 (0)