Bird
Raised Fist0
JUnittesting~5 mins

Gradle dependency setup in JUnit - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
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:
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
Click to reveal answer
beginner
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
How do you specify the JUnit 5 dependency in Gradle?
AtestImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
Bimplementation 'junit:junit:4.12'
CcompileOnly 'org.junit.jupiter:junit-jupiter:5.9.3'
DruntimeOnly 'org.junit.jupiter:junit-jupiter:5.9.3'
What happens if you omit the version number when adding a dependency in Gradle?
AGradle uses a default version which may cause unexpected issues
BGradle will fail to build the project
CGradle picks the latest version automatically
DGradle ignores the dependency
Which command runs tests in a Gradle project?
A./gradlew clean
B./gradlew build
C./gradlew run
D./gradlew test
Where do you add dependencies in a Gradle project?
AIn the gradle.properties file
BIn the settings.gradle file
CInside the <code>dependencies</code> block in build.gradle
DIn the src folder
Explain how to add JUnit 5 as a test dependency in a Gradle project and why it is important.
Think about the build.gradle file and the role of test dependencies.
You got /4 concepts.
    Describe the difference between implementation and testImplementation in Gradle dependencies.
    Consider when each dependency is needed during the project lifecycle.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the correct Gradle configuration to add JUnit 5 as a test-only dependency?
      easy
      A. compile 'org.junit.jupiter:junit-jupiter:5.9.3'
      B. implementation 'org.junit.jupiter:junit-jupiter:5.9.3'
      C. runtimeOnly 'org.junit.jupiter:junit-jupiter:5.9.3'
      D. testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'

      Solution

      1. Step 1: Understand Gradle dependency scopes

        For testing libraries like JUnit, Gradle uses testImplementation to include dependencies only in the test source set.
      2. Step 2: Identify the correct dependency declaration

        The correct syntax is testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3' to add JUnit 5 for tests only.
      3. Final Answer:

        testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3' -> Option D
      4. Quick Check:

        JUnit test dependency = testImplementation [OK]
      Hint: Use testImplementation for test-only libraries like JUnit [OK]
      Common Mistakes:
      • Using implementation instead of testImplementation
      • Using compile which is deprecated
      • Using runtimeOnly which excludes compile-time access
      2. Which of the following is the correct syntax to add JUnit 5 dependency in Gradle's build.gradle file using the Kotlin DSL?
      easy
      A. testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
      B. implementation("org.junit.jupiter:junit-jupiter:5.9.3")
      C. testImplementation("org.junit.jupiter:junit-jupiter:5.9.3")
      D. compile 'org.junit.jupiter:junit-jupiter:5.9.3'

      Solution

      1. Step 1: Recognize Kotlin DSL syntax

        Gradle Kotlin DSL uses parentheses and double quotes for dependencies, e.g., testImplementation("group:name:version").
      2. 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.
      3. Final Answer:

        testImplementation("org.junit.jupiter:junit-jupiter:5.9.3") -> Option C
      4. Quick Check:

        Kotlin DSL uses parentheses and quotes [OK]
      Hint: Kotlin DSL uses parentheses and quotes for dependencies [OK]
      Common Mistakes:
      • Using single quotes in Kotlin DSL
      • Using compile which is deprecated
      • Mixing Groovy and Kotlin syntax
      3. Given this Gradle snippet in Groovy DSL:
      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?
      medium
      A. Guava is available only during test runtime.
      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

      1. 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.
      2. 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.
      3. Final Answer:

        JUnit 5 is available only during test compilation and runtime. -> Option B
      4. Quick Check:

        testImplementation = test-only scope [OK]
      Hint: testImplementation means test-only availability [OK]
      Common Mistakes:
      • Confusing implementation with testImplementation
      • Assuming testImplementation affects main code
      • Mixing runtimeOnly with implementation
      4. You added this line to your Gradle build file:
      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?
      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

      1. Step 1: Check plugin application

        Gradle needs the 'java' or 'java-library' plugin to recognize source sets and dependencies properly.
      2. Step 2: Understand impact on dependencies

        Without the plugin, Gradle won't configure test source sets, causing ClassNotFoundException even if dependencies are declared.
      3. Final Answer:

        You forgot to apply the 'java' or 'java-library' plugin in build.gradle. -> Option A
      4. 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

      1. 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.
      2. 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.
      3. Final Answer:

        Use testImplementation for both JUnit and Mockito dependencies. -> Option A
      4. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard