@@ -105,9 +105,10 @@ Libraries like Reactor or Spring Data provide null-safe APIs leveraging this fea
105
105
The JSR 305 checks can be configured by adding the `-Xjsr305` compiler flag with the following
106
106
options: `-Xjsr305={strict|warn|ignore}`.
107
107
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).
111
112
112
113
[NOTE]
113
114
====
@@ -375,6 +376,27 @@ example:
375
376
class Person(val name: String, val age: Int)
376
377
----
377
378
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
+
378
400
But some persistence technologies like JPA require a default constructor, preventing this
379
401
kind of design. Fortunately, there is now a workaround for this
380
402
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]
528
550
529
551
=== Testing
530
552
553
+ ==== Per class lifecycle
554
+
531
555
Kotlin allows one to specify meaningful test function names between backticks,
532
556
and as of JUnit 5 Kotlin test classes can use the `@TestInstance(TestInstance.Lifecycle.PER_CLASS)`
533
557
annotation to enable a single instantiation of test classes which allows the use of `@BeforeAll` and `@AfterAll`
@@ -567,6 +591,35 @@ class IntegrationTests {
567
591
}
568
592
----
569
593
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
+
570
623
[[getting-started]]
571
624
== Getting started
572
625
0 commit comments