Skip to content

Commit cf86f9b

Browse files
committed
Polish Kotlin reference documentation
1 parent 657dc96 commit cf86f9b

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

src/docs/asciidoc/kotlin.adoc

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ Libraries like Reactor or Spring Data provide null-safe APIs leveraging this fea
105105
The JSR 305 checks can be configured by adding the `-Xjsr305` compiler flag with the following
106106
options: `-Xjsr305={strict|warn|ignore}`.
107107

108-
For kotlin versions 1.1.50+, the default behavior is the same to `-Xjsr305=warn`. The
109-
`strict` value should be considered experimental (Spring API nullability declaration could
110-
evolve even between minor releases and more checks may be added in the future).
108+
For kotlin versions 1.1.50+, the default behavior is the same to `-Xjsr305=warn`.
109+
The `strict` value is required to have Spring Framework API full null-safety taken in account
110+
but should be considered experimental since Spring API nullability declaration could evolve
111+
even between minor releases and more checks may be added in the future).
111112

112113
[NOTE]
113114
====
@@ -375,6 +376,27 @@ example:
375376
class Person(val name: String, val age: Int)
376377
----
377378

379+
You can optionally add https://kotlinlang.org/docs/reference/data-classes.html[the `data` keyword]
380+
to make the compiler automatically derives the following members from all properties
381+
declared in the primary constructor:
382+
383+
* equals()/hashCode() pair
384+
* toString() of the form "User(name=John, age=42)"
385+
* componentN() functions corresponding to the properties in their order of declaration
386+
* copy() function
387+
388+
This allows to change easily just one of the properties even if `User` properties are read-only:
389+
his allows us to write:
390+
391+
392+
[source,kotlin]
393+
----
394+
data class Person(val name: String, val age: Int)
395+
396+
val jack = User(name = "Jack", age = 1)
397+
val olderJack = jack.copy(age = 2)
398+
----
399+
378400
But some persistence technologies like JPA require a default constructor, preventing this
379401
kind of design. Fortunately, there is now a workaround for this
380402
https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell["default constructor hell"]
@@ -528,6 +550,8 @@ https://youtrack.jetbrains.com/issue/KT-11235[this Kotlin language design issue]
528550

529551
=== Testing
530552

553+
==== Per class lifecycle
554+
531555
Kotlin allows one to specify meaningful test function names between backticks,
532556
and as of JUnit 5 Kotlin test classes can use the `@TestInstance(TestInstance.Lifecycle.PER_CLASS)`
533557
annotation to enable a single instantiation of test classes which allows the use of `@BeforeAll` and `@AfterAll`
@@ -567,6 +591,35 @@ class IntegrationTests {
567591
}
568592
----
569593

594+
==== Specification-like tests
595+
596+
It is possible to create specification-like tests with JUnit 5 and Kotlin.
597+
598+
[source]
599+
----
600+
class SpecificationLikeTests {
601+
602+
@Nested
603+
@DisplayName("a calculator")
604+
inner class Calculator {
605+
val calculator = SampleCalculator()
606+
607+
@Test
608+
fun `should return the result of adding the first number to the second number`() {
609+
val sum = calculator.sum(2, 4)
610+
assertEquals(6, sum)
611+
}
612+
613+
@Test
614+
fun `should return the result of subtracting the second number from the first number`() {
615+
val subtract = calculator.subtract(4, 2)
616+
assertEquals(2, subtract)
617+
}
618+
}
619+
}
620+
----
621+
622+
570623
[[getting-started]]
571624
== Getting started
572625

0 commit comments

Comments
 (0)