Kotlin

์ž‘์„ฑ์ž

์นดํ…Œ๊ณ ๋ฆฌ:

โ† ํ”ผ๋“œ๋กœ
DEV Community ยท Vijay Hiremath ยท 2026-06-21 ๊ฐœ๋ฐœ(SW)

Vijay Hiremath

๐Ÿงช JUnit Test Isolation โ€” One Small Detail That Changed Everything

Today I learned that JUnit creates a brand-new instance of your test class for every “@test” method by default.

“@TestInstance(TestInstance.Lifecycle.PER_METHOD)”

What happens?

  • “test1()” runs โ†’ counter becomes “1”
  • JUnit destroys that object
  • “test2()” gets a fresh instance โ†’ counter starts at “0” again and becomes “2”

This prevents tests from accidentally sharing state and affecting each other.

๐Ÿ“ Bonus Gradle Gotcha:

If your tests are inside “src/main/kotlin”, Gradle ignores them and shows:

“Task :test NO-SOURCE”

Move them to:

“src/test/kotlin”

and your tests run correctly. Use:

“./gradlew test -i”

to see “println()” output in the terminal.

Small detail, big debugging lesson. ๐Ÿš€

JUnit #Kotlin #Testing #Gradle #Java #AndroidDev

์›๋ฌธ์—์„œ ๊ณ„์† โ†—

์ถ”์ถœ ๋ณธ๋ฌธ ยท ์ถœ์ฒ˜: dev.to ยท https://dev.to/vijayhiremath/kotlin-51he

์ฝ”๋ฉ˜ํŠธ

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค