The Gradle test task runs the unit tests in a project, typically using frameworks like JUnit. It compiles the test code and executes tests to check if the code works as expected.
Click to reveal answer
intermediate
How do you run only tests with a specific name pattern using the Gradle test task?
You can configure the test task in build.gradle with filter { includeTestsMatching 'pattern' } to run only tests matching that pattern.
Click to reveal answer
beginner
What file does Gradle use to configure the test task?
Gradle uses the build.gradle file to configure the test task and other build settings.
Click to reveal answer
beginner
How can you generate a test report after running the Gradle test task?
Gradle automatically generates a test report in build/reports/tests/test/index.html after running the test task. You can open this file in a browser to see test results.
Click to reveal answer
intermediate
What is the default testing framework used by Gradle's test task when using JUnit?
Gradle supports JUnit 4 and JUnit 5 by default. You need to add the correct dependencies in build.gradle to use the desired JUnit version.
Click to reveal answer
What command runs the tests in a Gradle project?
Agradle test
Bgradle build
Cgradle run
Dgradle clean
✗ Incorrect
The gradle test command runs the unit tests in the project.
Where does Gradle place the test reports by default?
Areports/tests/index.html
Bsrc/test/reports/index.html
Cbuild/test-results/index.html
Dbuild/reports/tests/test/index.html
✗ Incorrect
Gradle generates test reports in build/reports/tests/test/index.html by default.
How can you include only tests matching a pattern in Gradle?
Afilter { includeTestsMatching 'pattern' }
BincludeTests('pattern')
CtestPattern('pattern')
DonlyTests('pattern')
✗ Incorrect
You use filter { includeTestsMatching 'pattern' } inside the test task configuration.
Which file configures the Gradle test task?
Atest.gradle
Bbuild.gradle
Cgradle.properties
Dsettings.gradle
✗ Incorrect
The build.gradle file is used to configure the test task.
To use JUnit 5 in Gradle, what must you do?
AUse a special Gradle plugin
BNothing, JUnit 5 is included by default
CAdd JUnit 5 dependencies in build.gradle
DRename test files to end with J5
✗ Incorrect
You must add the JUnit 5 dependencies explicitly in build.gradle to use it.
Explain how the Gradle test task works and how you can customize which tests run.
Think about running tests and filtering them.
You got /4 concepts.
Describe where Gradle stores test reports and how you can view them.
Reports are saved as HTML files.
You got /3 concepts.
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
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.
Step 2: Differentiate from other Gradle tasks
Tasks like compileJava compile code, clean removes build files, and deployment is separate. Only test runs tests.
Final Answer:
To run automated tests and check if the code works correctly -> Option B
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
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.
Step 2: Verify other options are invalid
Commands like runTests, executeTests, and checkTests are not standard Gradle commands and will cause errors.
Final Answer:
gradle test -> Option D
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
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.
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.
Final Answer:
Both passed and failed test events are shown in the console -> Option A
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
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.
Step 2: Identify why no tests run
Using **/MyTests.java does not match any compiled test classes, so no tests run.
Final Answer:
The include pattern should use forward slashes and match class files, not source files -> Option A
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
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.
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.
Final Answer:
test {
useJUnitPlatform {
includeTags 'fast'
}
} -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.