Kotlin
📰 Dev.to · Vijay Hiremath
Learn how JUnit test isolation works in Kotlin and how to fix a common Gradle issue with tests
Action Steps
- Write a JUnit test class in Kotlin and observe how instances are created for each test method
- Use the @TestInstance annotation to control the test instance lifecycle
- Move tests from src/main/kotlin to src/test/kotlin to fix the NO-SOURCE issue in Gradle
- Run tests using ./gradlew test -i to see println() output in the terminal
- 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)
[](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
0 Add reaction
0 Like 0 Unicorn 0 Exploding Head 0 Raised Hands 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)
[](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

[](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
0 Add reaction
0 Like 0 Unicorn 0 Exploding Head 0 Raised Hands 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)
[](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
DeepCamp AI