What is the purpose of adding dependencies in a Gradle build file?
Dependencies tell Gradle which external libraries or tools your project needs to work correctly. They are like ingredients in a recipe that your project requires to run or test.
Click to reveal answer
beginner
How do you add JUnit 5 as a test dependency in a Gradle project?
You add the JUnit 5 dependency inside the dependencies block in your build.gradle file like this:
What does the testImplementation configuration mean in Gradle?
It means the dependency is only needed when running tests. It won't be included in the final app or production code, just during testing.
Click to reveal answer
beginner
Why is it important to specify the version number when adding a dependency?
Specifying the version ensures your project uses a known, stable version of the library. Without it, Gradle might pick an unexpected version, causing errors or inconsistent behavior.
Click to reveal answer
beginner
What Gradle task do you run to execute tests after setting up JUnit dependency?
You run ./gradlew test in your terminal. This runs all tests using the dependencies you set up, like JUnit.
Click to reveal answer
Which Gradle configuration is used to add dependencies needed only for testing?
AcompileOnly
BtestImplementation
Cimplementation
DruntimeOnly
✗ Incorrect
The testImplementation configuration is specifically for dependencies used only during testing.
How do you specify the JUnit 5 dependency in Gradle?
B. JUnit 5 is available only during test compilation and runtime.
C. JUnit 5 is available during main source compilation.
D. Guava is available only during test compilation.
Solution
Step 1: Understand dependency scopes
testImplementation means the dependency is available only for compiling and running tests, not main code. implementation is for main code and runtime.
Step 2: Apply to given dependencies
JUnit 5 is declared with testImplementation, so it is only for test compilation and runtime. Guava is declared with implementation, 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 B
Quick Check:
testImplementation = test-only scope [OK]
Hint: testImplementation means test-only availability [OK]
But your tests fail to run with a "ClassNotFoundException" for JUnit classes. What is the most likely cause?
medium
A. You forgot to apply the 'java' or 'java-library' plugin in build.gradle.
B. You used 'testImplementation' instead of 'implementation'.
C. The JUnit version 5.9.3 does not exist.
D. You need to add 'runtimeOnly' for JUnit.
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 A
Quick Check:
Java plugin required for test dependencies [OK]
Hint: Always apply 'java' plugin to enable test dependencies [OK]
Common Mistakes:
Using implementation instead of testImplementation
Assuming dependency version is wrong without checking
Adding runtimeOnly unnecessarily
5. You want to add JUnit 5 and Mockito for testing in your Gradle project. Which is the best way to declare these dependencies to keep your build file organized and efficient?
hard
A. Use testImplementation for both JUnit and Mockito dependencies.
B. Use implementation for JUnit and testImplementation for Mockito.
C. Use runtimeOnly for both JUnit and Mockito.
D. Use compileOnly for JUnit and implementation for Mockito.
Solution
Step 1: Understand dependency scopes for testing libraries
Both JUnit and Mockito are used only during testing, so they should be added with testImplementation to avoid including them in production code.
Step 2: Choose consistent and clear dependency declarations
Using testImplementation for 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 A
Quick Check:
Test libraries = testImplementation [OK]
Hint: Put all test libs under testImplementation for clarity [OK]
Common Mistakes:
Mixing implementation and testImplementation for test libs
Using runtimeOnly which excludes compile-time access
Using compileOnly which excludes runtime
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.