Skip to content

Commit

Permalink
Ktln 346 array2list (Baeldung#260)
Browse files Browse the repository at this point in the history
* KTLN-336

* KTLN-346 Array -> List
  • Loading branch information
sk1418 authored Jun 18, 2022
1 parent d74aaf2 commit 3f60293
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.arrayToList

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.util.Arrays
import org.junit.jupiter.api.Test


class ConvertArrayToListUnitTest {
private val givenArray = arrayOf("one", "two", "three", "four", "five")
private val expectedList = listOf("one", "two", "three", "four", "five")


@Test
fun `Given an array, when call Java Arrays' asList() method, should get the expected list`() {
val myList = Arrays.asList(givenArray)
assertThat(myList).isEqualTo(expectedList)
}

@Test
fun `Given an array, when call listOf() method, should get the expected list`() {
val myList = listOf(*givenArray)
assertThat(myList).isEqualTo(expectedList)
}

@Test
fun `Given an array, when call array's toList() function should get the expected list`() {
val myList = givenArray.toList()
assertThat(myList).isEqualTo(expectedList)
}

@Test
fun `Given an array, when call array's toCollection(destination) function should get the expected list`() {
val myList = givenArray.toCollection(ArrayList())
assertThat(myList).isEqualTo(expectedList)

val existingList = mutableListOf("I have an element already.")
val appendedList = givenArray.toCollection(existingList)
assertThat(appendedList).isEqualTo(mutableListOf("I have an element already.", *givenArray))
}

}

0 comments on commit 3f60293

Please sign in to comment.