Bird
Raised Fist0
JUnittesting~20 mins

Gradle test task in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Gradle Test Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Gradle test task with failing test
Given the following JUnit test class, what will be the output status of the Gradle test task when executed?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class SampleTest {
    @Test
    void testSuccess() {
        assertEquals(2, 1 + 1);
    }

    @Test
    void testFailure() {
        assertTrue(false);
    }
}
AThe Gradle test task will fail and report 1 failed test.
BThe Gradle test task will pass with all tests successful.
CThe Gradle test task will fail with a compilation error.
DThe Gradle test task will be skipped due to no tests found.
Attempts:
2 left
💡 Hint
Remember that a failing assertion causes the test to fail and the Gradle test task to fail.
❓ assertion
intermediate
1:30remaining
Correct assertion to verify test task execution
Which JUnit assertion correctly verifies that the Gradle test task executed exactly 5 tests successfully?
AassertFalse(result.wasSuccessful());
BassertNull(result.getTestCount());
CassertTrue(result.getFailedTests() == 5);
DassertEquals(5, result.getTestCount());
Attempts:
2 left
💡 Hint
You want to check the total number of tests run, not failures or null values.
🔧 Debug
advanced
2:00remaining
Debugging Gradle test task skipping tests
You run gradle test but no tests are executed. Which of the following is the most likely cause?
AThe <code>build.gradle</code> file is missing the <code>apply plugin: 'java'</code> line.
BTest classes are not named according to Gradle's default pattern (e.g., *Test.java).
CJUnit dependency is declared with <code>testImplementation</code> instead of <code>implementation</code>.
DThe <code>src/main/java</code> directory contains the test classes.
Attempts:
2 left
💡 Hint
Gradle looks for test classes matching specific name patterns by default.
❓ framework
advanced
1:30remaining
Configuring Gradle test task for JUnit 5
Which Gradle configuration snippet correctly enables JUnit 5 platform for the test task?
A
test {
    useTestNG()
}
B
test {
    useJUnit()
}
C
test {
    useJUnitPlatform()
}
D
test {
    useJUnit4()
}
Attempts:
2 left
💡 Hint
JUnit 5 requires a specific platform configuration in Gradle.
🧠 Conceptual
expert
2:30remaining
Impact of test task parallel execution on test results
If you configure the Gradle test task to run tests in parallel, which of the following is a potential risk?
ATests that share mutable static state may produce flaky or inconsistent results.
BTests will always run faster without any side effects.
CGradle will ignore test failures and mark the build as successful.
DJUnit will automatically serialize all tests to avoid conflicts.
Attempts:
2 left
💡 Hint
Think about what happens when tests run at the same time and share data.

Practice

(1/5)
1. What is the primary purpose of the Gradle test task in a JUnit project?
easy
A. To compile the Java source code into bytecode
B. To run automated tests and check if the code works correctly
C. To deploy the application to a server
D. To clean the build directory before building

Solution

  1. Step 1: Understand the Gradle test task role

    The test task in Gradle is designed to run automated tests, such as those written with JUnit, to verify code correctness.
  2. Step 2: Differentiate from other Gradle tasks

    Tasks like compileJava compile code, clean removes build files, and deployment is separate. Only test runs tests.
  3. Final Answer:

    To run automated tests and check if the code works correctly -> Option B
  4. Quick Check:

    Gradle test = run tests [OK]
Hint: Remember: 'gradle test' runs your tests [OK]
Common Mistakes:
  • Confusing test task with compile or clean tasks
  • Thinking test deploys the app
  • Assuming test cleans build files
2. Which of the following is the correct command to run JUnit tests using Gradle from the terminal?
easy
A. gradle checkTests
B. gradle runTests
C. gradle executeTests
D. gradle test

Solution

  1. Step 1: Recall the standard Gradle test command

    The official Gradle command to run tests is gradle test. This triggers the test task which runs JUnit tests.
  2. Step 2: Verify other options are invalid

    Commands like runTests, executeTests, and checkTests are not standard Gradle commands and will cause errors.
  3. Final Answer:

    gradle test -> Option D
  4. Quick Check:

    Run tests with gradle test [OK]
Hint: Use exactly 'gradle test' to run tests [OK]
Common Mistakes:
  • Adding extra words like run or execute
  • Using non-existent Gradle commands
  • Confusing test with build or run
3. Given this snippet in build.gradle:
test {
    useJUnitPlatform()
    testLogging {
        events "passed", "failed"
    }
}
What will be the output when you run gradle test and all tests pass?
medium
A. Both passed and failed test events are shown in the console
B. Only failed tests are shown in the output
C. No output because logging is disabled
D. Only passed tests are shown, failed tests are ignored

Solution

  1. Step 1: Understand the testLogging configuration

    The testLogging block with events "passed", "failed" means Gradle will log passed and failed test events when they occur.
  2. Step 2: Predict output when all tests pass

    Since all tests pass, passed test events are shown. Failed events do not occur, so they are not shown. Therefore, only passed test events appear in the console.
  3. Final Answer:

    Both passed and failed test events are shown in the console -> Option A
  4. Quick Check:

    Passed and failed events configured to show; failed events absent if none fail [OK]
Hint: Check testLogging events to know what shows [OK]
Common Mistakes:
  • Assuming no output if tests pass
  • Thinking only failed tests show by default
  • Ignoring testLogging configuration
4. You added this to your build.gradle:
test {
    useJUnitPlatform()
    testLogging {
        events "passed", "failed"
    }
    include '**/MyTests.java'
}
But running gradle test runs no tests. What is the likely problem?
medium
A. The include pattern should use forward slashes and match class files, not source files
B. You forgot to add useJUnit() instead of useJUnitPlatform()
C. The testLogging block disables test execution
D. Gradle test task cannot filter tests by include patterns

Solution

  1. Step 1: Understand the include pattern usage

    The include pattern filters test classes by their compiled class file names, not source files. It should match **/MyTests.class or use package style with forward slashes.
  2. Step 2: Identify why no tests run

    Using **/MyTests.java does not match any compiled test classes, so no tests run.
  3. Final Answer:

    The include pattern should use forward slashes and match class files, not source files -> Option A
  4. Quick Check:

    Include matches class files, not .java [OK]
Hint: Include filters need class file patterns, not source files [OK]
Common Mistakes:
  • Using .java instead of .class in include patterns
  • Confusing useJUnitPlatform with test execution
  • Thinking testLogging disables tests
5. You want to customize the Gradle test task to run only tests tagged with @Tag("fast") in JUnit 5. Which build.gradle snippet correctly configures this?
hard
A. test { includeTags 'fast' }
B. test { useJUnitPlatform() includeTags 'fast' }
C. test { useJUnitPlatform { includeTags 'fast' } }
D. test { useJUnitPlatform() testLogging { includeTags 'fast' } }

Solution

  1. Step 1: Recall how to filter tests by tags in Gradle

    Gradle's useJUnitPlatform accepts a configuration block where you can specify includeTags to run only tests with certain tags.
  2. Step 2: Check each option for correct syntax

    test { useJUnitPlatform { includeTags 'fast' } } correctly places includeTags 'fast' inside the useJUnitPlatform block. Other options place it incorrectly or omit useJUnitPlatform.
  3. Final Answer:

    test { useJUnitPlatform { includeTags 'fast' } } -> Option C
  4. Quick Check:

    Tag filtering goes inside useJUnitPlatform block [OK]
Hint: Put includeTags inside useJUnitPlatform block [OK]
Common Mistakes:
  • Placing includeTags outside useJUnitPlatform
  • Adding includeTags inside testLogging
  • Omitting useJUnitPlatform call
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