Skip to content

Commit b4d7a40

Browse files
authored
KTLN-836: Introduction to Kluent Assertion Library (#1087)
* KTLN-836: Introduction to Kluent Assertion Library * Some updates from review feedback
1 parent 9344541 commit b4d7a40

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

kotlin-testing/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
<scope>test</scope>
106106
</dependency>
107107

108+
<dependency>
109+
<groupId>org.amshove.kluent</groupId>
110+
<artifactId>kluent</artifactId>
111+
<version>${kluent.version}</version>
112+
<scope>test</scope>
113+
</dependency>
114+
108115
</dependencies>
109116

110117
<build>
@@ -135,6 +142,7 @@
135142
<testcontainers.version>1.20.1</testcontainers.version>
136143
<postgresql.version>42.7.3</postgresql.version>
137144
<mockk.version>1.12.0</mockk.version>
145+
<kluent.version>1.73</kluent.version>
138146
</properties>
139147

140148
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.kluent
2+
3+
import org.amshove.kluent.*
4+
import org.junit.jupiter.api.Disabled
5+
import org.junit.jupiter.api.Test
6+
7+
class EquivalencyUnitTest {
8+
@Test
9+
fun `when asserting equality then the equals method should be used`() {
10+
val actual = NotAllFieldsChecked("123", "Baeldung", 42)
11+
12+
actual shouldBeEqualTo NotAllFieldsChecked("123", "Other", 100)
13+
}
14+
15+
@OptIn(ExperimentalStdlibApi::class)
16+
@Test
17+
@Disabled // We expect this to fail
18+
fun `when asserting equivalency then the individual fields should be checked`() {
19+
val actual = NotAllFieldsChecked("123", "Baeldung", 42)
20+
21+
actual.shouldBeEquivalentTo(NotAllFieldsChecked("123", "Other", 100))
22+
}
23+
24+
@OptIn(ExperimentalStdlibApi::class)
25+
@Test
26+
fun `when asserting equivalency specifying which fields to exclude then only the other fields should be checked`() {
27+
val actual = NotAllFieldsChecked("123", "Baeldung", 42)
28+
29+
actual.shouldBeEquivalentTo(NotAllFieldsChecked("123", "Other", 42)) {
30+
it.excluding(NotAllFieldsChecked::name)
31+
}
32+
}
33+
34+
class NotAllFieldsChecked(val id: String, val name: String, val age: Int) {
35+
override fun equals(other: Any?): Boolean {
36+
if (this === other) return true
37+
if (javaClass != other?.javaClass) return false
38+
39+
other as NotAllFieldsChecked
40+
41+
return id == other.id
42+
}
43+
44+
override fun hashCode(): Int {
45+
return id.hashCode()
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.kluent
2+
3+
import kotlinx.coroutines.test.runTest
4+
import org.amshove.kluent.coInvoking
5+
import org.amshove.kluent.invoking
6+
import org.amshove.kluent.shouldThrow
7+
import org.amshove.kluent.withMessage
8+
import org.junit.jupiter.api.Test
9+
import java.util.concurrent.TimeoutException
10+
11+
class ExceptionsUnitTest {
12+
@Test
13+
fun `when a block throws then we should be able to assert the correct exception`() {
14+
invoking { 1 / 0 } shouldThrow ArithmeticException::class
15+
}
16+
17+
@Test
18+
fun `when a block throws then we should be able to assert the correct exception and message`() {
19+
invoking { 1 / 0 } shouldThrow ArithmeticException::class withMessage "/ by zero"
20+
}
21+
22+
@Test
23+
fun `when a suspend block throws then we should be able to assert the correct exception`() {
24+
runTest {
25+
coInvoking { suspend { throw TimeoutException() }() } shouldThrow TimeoutException::class
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.kluent
2+
3+
import org.amshove.kluent.*
4+
import org.junit.jupiter.api.Disabled
5+
import org.junit.jupiter.api.Test
6+
7+
class SimpleAssertionsUnitTest {
8+
@Test
9+
@Disabled // We expect this to fail
10+
fun `when comparing two strings they they should not be equal`() {
11+
val value = "Hello"
12+
13+
value.shouldBeEqualTo("Expected")
14+
value shouldBeEqualTo "Expected"
15+
value `should be equal to` "Expected"
16+
}
17+
18+
@Test
19+
fun `when comparing two strings in a case inseneitive manner then they should be equal`() {
20+
val value = "Hello"
21+
22+
value shouldBeEqualToIgnoringCase "Hello"
23+
value shouldBeEqualToIgnoringCase "HELLO"
24+
value shouldBeEqualToIgnoringCase "hello"
25+
}
26+
27+
@Test
28+
@Disabled // We expect this to fail
29+
fun `when performing multiple related assertions then they should all run and report together`() {
30+
val set = setOf("a", "b", "c")
31+
32+
assertSoftly {
33+
set shouldHaveSize 4
34+
set shouldContain "a"
35+
set shouldContain "b"
36+
set shouldContain "c"
37+
set shouldContain "d"
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)