We add dependencies in Gradle to tell our project which external libraries it needs. This helps us use tools like JUnit for testing without manually downloading files.
Gradle dependency setup in JUnit
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
}dependencies block lists all libraries your project needs.
testImplementation means this library is only for testing, not for the main app.
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
}dependencies {
implementation 'com.google.guava:guava:31.1-jre'
}dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
}This Gradle build file sets up JUnit 5.9.3 for testing a Java project. The testImplementation line adds JUnit as a test library. The test block tells Gradle to use JUnit Platform to run tests.
plugins {
id 'java'
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
}
test {
useJUnitPlatform()
}Always use testImplementation for test libraries to keep main code clean.
Use the latest stable version of JUnit for best features and fixes.
Run ./gradlew test in terminal to execute tests after setup.
Gradle dependencies tell your project which libraries to use.
Use testImplementation to add JUnit for testing only.
Keep your build file organized to easily manage libraries.
Practice
Solution
Step 1: Understand Gradle dependency scopes
For testing libraries like JUnit, Gradle usestestImplementationto include dependencies only in the test source set.Step 2: Identify the correct dependency declaration
The correct syntax istestImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'to add JUnit 5 for tests only.Final Answer:
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3' -> Option DQuick Check:
JUnit test dependency = testImplementation [OK]
- Using implementation instead of testImplementation
- Using compile which is deprecated
- Using runtimeOnly which excludes compile-time access
build.gradle file using the Kotlin DSL?Solution
Step 1: Recognize Kotlin DSL syntax
Gradle Kotlin DSL uses parentheses and double quotes for dependencies, e.g.,testImplementation("group:name:version").Step 2: Identify the correct test dependency syntax
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3") uses the correct Kotlin DSL syntax for test dependencies.Final Answer:
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3") -> Option CQuick Check:
Kotlin DSL uses parentheses and quotes [OK]
- Using single quotes in Kotlin DSL
- Using compile which is deprecated
- Mixing Groovy and Kotlin syntax
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
implementation 'com.google.guava:guava:31.1-jre'
}Which statement is true when running tests?
Solution
Step 1: Understand dependency scopes
testImplementationmeans the dependency is available only for compiling and running tests, not main code.implementationis for main code and runtime.Step 2: Apply to given dependencies
JUnit 5 is declared withtestImplementation, so it is only for test compilation and runtime. Guava is declared withimplementation, so it is available in main code and runtime, not limited to tests.Final Answer:
JUnit 5 is available only during test compilation and runtime. -> Option BQuick Check:
testImplementation = test-only scope [OK]
- Confusing implementation with testImplementation
- Assuming testImplementation affects main code
- Mixing runtimeOnly with implementation
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
But your tests fail to run with a "ClassNotFoundException" for JUnit classes. What is the most likely cause?
Solution
Step 1: Check plugin application
Gradle needs the 'java' or 'java-library' plugin to recognize source sets and dependencies properly.Step 2: Understand impact on dependencies
Without the plugin, Gradle won't configure test source sets, causing ClassNotFoundException even if dependencies are declared.Final Answer:
You forgot to apply the 'java' or 'java-library' plugin in build.gradle. -> Option AQuick Check:
Java plugin required for test dependencies [OK]
- Using implementation instead of testImplementation
- Assuming dependency version is wrong without checking
- Adding runtimeOnly unnecessarily
Solution
Step 1: Understand dependency scopes for testing libraries
Both JUnit and Mockito are used only during testing, so they should be added withtestImplementationto avoid including them in production code.Step 2: Choose consistent and clear dependency declarations
UsingtestImplementationfor both keeps the build file organized and ensures dependencies are available only for tests.Final Answer:
Use testImplementation for both JUnit and Mockito dependencies. -> Option AQuick Check:
Test libraries = testImplementation [OK]
- Mixing implementation and testImplementation for test libs
- Using runtimeOnly which excludes compile-time access
- Using compileOnly which excludes runtime
Ready to prove it?
Pass the test and this pattern moves unknown
