안드로이드
JUnit Annotations with Example in Kotlin
devGeek
2022. 6. 24. 09:00
반응형
Android Unit Test란
Android Unit Test 개요 모바일 앱 개발을 하는데 있어, MVVM, Jetpack, KMM(Kotlin Multiplatform) 등 다양한 트렌드를 볼 수 있다. 그 중 테스트 기술은 요즘 중요하고 선호하는 것 중 하나이다. 또한 테스트 기..
devgeek.tistory.com
Android Unit Testing 기본 구현 in Kotlin
Android Unit Test란 개요 Unit Test Unit testing 은 개발자로 하여금 에러가 없는 좋은 코드를 만들 수 있도록 도와준다. 실제 앱을 바로 개발하기 보다는 개발 전에 Unit Test 들을 작성하기를 추천하는데,
devgeek.tistory.com
JUnit Annotation 이란
- JUnit Annotations는 메타 데이터를 나타내는 특별한 문법 형태이다.
- 코드의 가독성과 구조를 나타내기에 용이하다.
- 변수, 인자(파라미터), 패키지, 함수 그리고 클래스에 사용될 수 있다.
- 자바 코드의 가독성과 간소화를 위해
JUnit4
에 도입되었다. JUnit4
와JUnit3
의 큰 차이점은JUnit4
는 Annotation 기반이라는 것이다.
Annotation 정리
Annotations | Description |
---|---|
@Test | 실행할 테스트임을 명시한다. |
@Before | @Test 테스트 케이스가 실행되기 전에 실행됨을 명시한다. |
@BeforeClass | 클래스의 모든 테스트 케이스 전에 한 번 실행됨을 명시한다. |
@After | @Test 테스트 케이스가 종료된 후 실행됨을 명시한다. |
@AfterClass | 클래스의 모든 테스트 케이스가 실행된 후 한 번 실행됨을 명시한다. |
JUnit Annotations 예제
AnnotationTest.kt
import org.junit.*
class AnnotationTest {
private lateinit var list: ArrayList<String>
@Before
fun runBeforeTestMethod() {
list = arrayListOf("@Before", "run", "Before", "Test", "Method")
println("@Before - runBeforeTestMethod")
println("\t- executed before each test cases")
println("\t- create new list($list)")
}
@After
fun runAfterTestMethod() {
list.clear()
println("@After - runAfterTestMethod")
println("\t- executed after each test cases")
println("\t- clear list($list)")
println()
}
@Test
fun test_method_1() {
println("@Test - test_method_1")
}
@Test
fun test_method_2() {
println("@Test - test_method_2")
}
companion object {
@BeforeClass
@JvmStatic
fun runOnceBeforeClass() {
println("@BeforeClass - runOnceBeforeClass")
println("\t- executed before all test cases")
println()
}
@AfterClass
@JvmStatic
fun runOnceAfterClass() {
println("@AfterClass - runOnceAfterClass")
println("\t- executed after all test cases")
}
}
}
Output
@BeforeClass - runOnceBeforeClass
- executed before all test cases
@Before - runBeforeTestMethod
- executed before each test cases
- create new list([@Before, run, Before, Test, Method])
@Test - test_method_1
@After - runAfterTestMethod
- executed after each test cases
- clear list([])
@Before - runBeforeTestMethod
- executed before each test cases
- create new list([@Before, run, Before, Test, Method])
@Test - test_method_2
@After - runAfterTestMethod
- executed after each test cases
- clear list([])
@AfterClass - runOnceAfterClass
- executed after all test cases
참고
JUnit – Basic annotation examples - Mkyong.com
- JUnit – Basic annotation examples
mkyong.com
JUnit Annotations Tutorial with Example: What is @Test and @After
Annotations are introduced in Junit4. we will learn Annotations like @Before, @BeforeClass, @After, @AfterClass, @Test, @Ignores etc.
www.guru99.com
질문과 잘못된 점에 대해 말씀해주시는 건 언제나 환영입니다.
zero5.two4@gmail.com
반응형