Kotlin

📰 Dev.to · Vijay Hiremath

Learn how JUnit test isolation works in Kotlin and how to fix a common Gradle issue with tests

beginner Published 21 Jun 2026
Action Steps
  1. Write a JUnit test class in Kotlin and observe how instances are created for each test method
  2. Use the @TestInstance annotation to control the test instance lifecycle
  3. Move tests from src/main/kotlin to src/test/kotlin to fix the NO-SOURCE issue in Gradle
  4. Run tests using ./gradlew test -i to see println() output in the terminal
  5. Apply test isolation principles to prevent tests from sharing state and affecting each other
Who Needs to Know This

Software engineers and developers working with Kotlin and JUnit can benefit from understanding test isolation and troubleshooting common issues

Key Insight

💡 JUnit test isolation is crucial to prevent tests from sharing state and affecting each other

Share This
🚀 Did you know JUnit creates a new instance of your test class for every @test method by default? Learn how to use @TestInstance and fix common Gradle issues in Kotlin! #Kotlin #JUnit #Testing

Key Takeaways

Learn how JUnit test isolation works in Kotlin and how to fix a common Gradle issue with tests

Full Article

Title: Kotlin

URL Source: https://dev.to/vijayhiremath/kotlin-51he

Published Time: 2026-06-21T02:50:46Z

Markdown Content:
# Kotlin - DEV Community
[Skip to content](https://dev.to/vijayhiremath/kotlin-51he#main-content)

[![Image 1: DEV Community](https://media2.dev.to/dynamic/image/quality=100/https://dev-to-uploads.s3.amazonaws.com/uploads/logos/resized_logo_UQww2soKuUsjaOGNB38o.png)](https://dev.to/)

[Powered by Algolia](https://www.algolia.com/developers/?utm_source=devto&utm_medium=referral)

[Log in](https://dev.to/enter?signup_subforem=1)[Create account](https://dev.to/enter?signup_subforem=1&state=new-user)

## DEV Community

![Image 2](https://assets.dev.to/assets/heart-plus-active-9ea3b22f2bc311281db911d416166c5f430636e76b15cd5df6b3b841d830eefa.svg)0 Add reaction

![Image 3](https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg)0 Like ![Image 4](https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg)0 Unicorn ![Image 5](https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg)0 Exploding Head ![Image 6](https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg)0 Raised Hands ![Image 7](https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg)0 Fire

0 Jump to Comments 0 Save Boost

Copy link

Copied to Clipboard

[Share to X](https://twitter.com/intent/tweet?text=%22Kotlin%22%20by%20Vijay%20Hiremath%20%23DEVCommunity%20https%3A%2F%2Fdev.to%2Fvijayhiremath%2Fkotlin-51he)[Share to LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fdev.to%2Fvijayhiremath%2Fkotlin-51he&title=Kotlin&summary=%F0%9F%A7%AA%20JUnit%20Test%20Isolation%20%E2%80%94%20One%20Small%20Detail%20That%20Changed%20Everything%20%20Today%20I%20learned%20that%20JUnit%20creates...&source=DEV%20Community)[Share to Facebook](https://www.facebook.com/sharer.php?u=https%3A%2F%2Fdev.to%2Fvijayhiremath%2Fkotlin-51he)[Share to Mastodon](https://s2f.kytta.dev/?text=https%3A%2F%2Fdev.to%2Fvijayhiremath%2Fkotlin-51he)

[Share Post via...](https://dev.to/vijayhiremath/kotlin-51he#)[Report Abuse](https://dev.to/report-abuse)

[![Image 8: Vijay Hiremath](https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3991786%2Fc5f6b3b8-ae43-446a-a7c4-7290a83f4648.jpg)](https://dev.to/vijayhiremath)

[Vijay Hiremath](https://dev.to/vijayhiremath)
Posted on Jun 21

# Kotlin

[#tutorial](https://dev.to/t/tutorial)[#testing](https://dev.to/t/testing)[#programming](https://dev.to/t/programming)[#beginners](https://dev.to/t/beginners)

🧪 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](https://dev.to/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. 🚀

# [](https://dev.to/vijayhiremath/kotlin-51he#junit-kotlin-testing-gradle-java-androiddev) JUnit #Kotlin #Testing #Gradle #Java #AndroidDev

## Top comments (0)

Subscribe

![Image 9: pic](https://media2.dev.to/dynamic/image/width=256,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farti
Read full article → ← Back to Reads