Skip to content

Commit c8d16ac

Browse files
committed
KTLN-547: Improvements in unit tests
Signed-off-by: Diego Torres <[email protected]>
1 parent 579f45c commit c8d16ac

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/listofdates/SortingListOfStringDatesUnitTest.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package com.baeldung.listofdates
22

33
import org.junit.jupiter.api.Assertions.assertEquals
44
import org.junit.jupiter.api.Test
5+
import java.text.SimpleDateFormat
56
import java.time.LocalDate
67
import java.time.format.DateTimeFormatter
78
import kotlin.test.assertNotNull
89

910
class SortingListOfStringDatesUnitTest {
1011

1112
@Test
12-
fun `given a list of string dates when sorting with sortedDescending then a sorted list is generated`() {
13+
fun `given a list of string dates when sorting with DateTimeFormatter then a sorted list is generated`() {
1314
val dates = listOf("31-12-2023", "01-01-2023", "15-06-2023")
1415

15-
val formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
16-
val sortedDates = dates.map { LocalDate.parse(it, formatter) }
17-
.sortedDescending()
18-
.map { it.format(formatter) }
16+
val dateFormat = SimpleDateFormat("dd-MM-yyyy")
17+
18+
val sortedDates = dates.sortedByDescending { dateFormat.parse(it) }
1919

2020
val expectedSortedDates = listOf("31-12-2023", "15-06-2023", "01-01-2023")
2121

@@ -24,14 +24,11 @@ class SortingListOfStringDatesUnitTest {
2424
}
2525

2626
@Test
27-
fun `given a list of string dates when sorting with sortedByDescending then a sorted list is generated`() {
27+
fun `given a list of string dates when sorting with LocalDate then a sorted list is generated`() {
2828
val dates = listOf("31-12-2023", "01-01-2023", "15-06-2023")
2929

30-
val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
31-
32-
val sortedDates = dates.sortedByDescending {
33-
LocalDate.parse(it, dateTimeFormatter)
34-
}
30+
val dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
31+
val sortedDates = dates.sortedByDescending { LocalDate.parse(it, dateFormatter) }
3532

3633
val expectedSortedDates = listOf("31-12-2023", "15-06-2023", "01-01-2023")
3734

0 commit comments

Comments
 (0)