Post
KO

Junit Test 작성하기

https://www.baeldung.com/kotlin/junit-5-kotlin

[*](https://www.baeldung.com/kotlin/junit-5-kotlin) > [**JUnit 5 for Kotlin Developers | Baeldung on Kotlin**](https://www.baeldung.com/kotlin/junit-5-kotlin) > > Learn how to write JUnit 5 tests in the Kotlin language.

parameterize test외에도 많이 있었구나..

테스트를 짜기 위해 노력하지 않은 나를 반성하게 된다.

4.2. Parameterized Tests

There are *experimental extensions to JUnit 5 **to allow easier ways to write parameterized tests. These are done using the @ParameterizedTest *annotation from the *org.junit.jupiter:junit-jupiter-params dependency:

<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.0.0</version> </dependency>

The latest version can be found on Maven Central.

The @MethodSource annotation allows us to produce test parameters by calling a static function that resides in the same class as the test. This is possible but not obvious in Kotlin. We have to use the **@JvmStatic** annotation inside a **companion object:**

@ParameterizedTest @MethodSource(“squares”) fun testSquares(input: Int, expected: Int) { Assertions.assertEquals(expected, input * input) } companion object { @JvmStatic fun squares() = listOf( Arguments.of(1, 1), Arguments.of(2, 4) ) }

This also means that the methods used to produce parameters must all be together since we can only have a single **companion object** per class.

All of the other ways of using parameterized tests work exactly the same in Kotlin as they do in Java. @CsvSource is of special note here, since we can use that instead of @MethodSource for simple test data most of the time to make our tests more readable:

parameterizeTest 에서 methodSource로 입력되는 부분을 test list에서 확인하고 싶으면 아래 링크를 참고하면 좋다.

https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing-parameterized-tests/

@ParameterizedTest(name = "{index} => message=''{0}''")

이렇게 설정하면 아래와 같이 확인이 가능하다.

![](/assets/images/posts/222833597456/a26f849e45a0.png?type=w580)

This article is licensed under CC BY 4.0 by the author.