Bird
Raised Fist0
JUnittesting~20 mins

Code quality gates in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Code Quality Gate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of a code quality gate in JUnit testing?

Choose the best description of what a code quality gate does in the context of JUnit tests.

AIt automatically fixes failing tests during the build process.
BIt blocks code from merging if tests fail or coverage is below a threshold.
CIt generates test data for JUnit tests to improve coverage.
DIt runs tests only on the developer's local machine before commit.
Attempts:
2 left
💡 Hint

Think about how quality gates help maintain code standards before merging.

❓ Predict Output
intermediate
2:00remaining
JUnit test result with quality gate failure

Given the following JUnit test class, what will be the outcome if a quality gate requires 80% coverage but only 60% is achieved?

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {
    @Test
    void testAdd() {
        assertEquals(5, 2 + 3);
    }
    // No test for subtract method
}

class Calculator {
    int add(int a, int b) { return a + b; }
    int subtract(int a, int b) { return a - b; }
}
ATests pass but quality gate fails due to insufficient coverage.
BBuild fails due to syntax errors in test class.
CQuality gate passes because all tests pass.
DTests fail because subtract method is not tested.
Attempts:
2 left
💡 Hint

Passing tests do not guarantee quality gate success if coverage is low.

❓ assertion
advanced
2:00remaining
Which assertion correctly verifies a method throws an exception in JUnit 5?

Choose the correct JUnit 5 assertion to test that calling divide(5, 0) throws ArithmeticException.

JUnit
public class MathUtils {
    public static int divide(int a, int b) {
        return a / b;
    }
}
AassertDoesNotThrow(() -> MathUtils.divide(5, 0));
BassertEquals(ArithmeticException.class, MathUtils.divide(5, 0));
CassertTrue(MathUtils.divide(5, 0) instanceof ArithmeticException);
DassertThrows(ArithmeticException.class, () -> MathUtils.divide(5, 0));
Attempts:
2 left
💡 Hint

Look for the method that expects an exception to be thrown.

🔧 Debug
advanced
2:00remaining
Identify the cause of quality gate failure in this JUnit test suite

Given this test suite, why does the quality gate fail despite all tests passing?

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class StringUtilsTest {
    @Test
    void testIsEmpty() {
        assertTrue(StringUtils.isEmpty(""));
    }
    // Missing tests for isNotEmpty method
}

class StringUtils {
    static boolean isEmpty(String s) { return s == null || s.isEmpty(); }
    static boolean isNotEmpty(String s) { return !isEmpty(s); }
}
ACoverage is low because isNotEmpty method is not tested.
BTests fail due to NullPointerException in isEmpty method.
CQuality gate fails because test method name is incorrect.
DBuild fails due to missing import statements.
Attempts:
2 left
💡 Hint

Think about what code is not covered by tests.

❓ framework
expert
3:00remaining
How to integrate a code quality gate with JUnit tests in a CI pipeline?

Which approach correctly integrates JUnit test results and coverage reports to enforce a quality gate in a CI pipeline?

AUse JUnit to generate coverage report without additional tools, then merge code regardless of results.
BRun JUnit tests only locally, then manually check coverage before merging.
CRun JUnit tests, generate coverage report with JaCoCo, then fail build if coverage < threshold.
DSkip tests and rely on manual code review to enforce quality gate.
Attempts:
2 left
💡 Hint

Think about automation and tools that measure coverage in CI.

Practice

(1/5)
1. What is the main purpose of a code quality gate in a JUnit testing process?
easy
A. To check if the code meets predefined quality standards before merging
B. To automatically deploy code to production
C. To write new JUnit test cases
D. To format the source code automatically

Solution

  1. Step 1: Understand the role of quality gates

    Quality gates are checkpoints that verify if code meets certain quality criteria before it is merged into the main branch.
  2. Step 2: Relate to JUnit testing

    JUnit tests provide results that quality gates use to decide if the code passes or fails the quality standards.
  3. Final Answer:

    To check if the code meets predefined quality standards before merging -> Option A
  4. Quick Check:

    Quality gate purpose = Check code quality before merge [OK]
Hint: Quality gates stop bad code before merge [OK]
Common Mistakes:
  • Confusing quality gates with deployment steps
  • Thinking quality gates write tests automatically
  • Assuming quality gates only format code
2. Which of the following is the correct way to assert that two values are equal in a JUnit 5 test?
easy
A. assertEqual(expected, actual);
B. assertEquals(expected, actual);
C. assertEquals(actual, expected);
D. assertEqual(actual, expected);

Solution

  1. Step 1: Recall JUnit 5 assertion syntax

    The correct method to check equality is assertEquals(expected, actual); with the expected value first.
  2. Step 2: Check method names and parameter order

    Method name must be assertEquals (not assertEqual) and parameters order matters: expected first, actual second.
  3. Final Answer:

    assertEquals(expected, actual); -> Option B
  4. Quick Check:

    assertEquals(expected, actual) is correct [OK]
Hint: Use assertEquals(expected, actual) exactly [OK]
Common Mistakes:
  • Using assertEqual instead of assertEquals
  • Swapping expected and actual parameters
  • Missing parentheses or semicolons
3. Given the following JUnit test snippet, what will be the test result if calculateSum(2, 3) returns 6?
import static org.junit.jupiter.api.Assertions.assertEquals;

@Test
void testSum() {
    assertEquals(5, calculateSum(2, 3));
}
medium
A. Test fails with assertion error
B. Test is skipped
C. Test throws a compilation error
D. Test passes

Solution

  1. Step 1: Understand the assertion

    The test expects calculateSum(2, 3) to return 5, but it returns 6 instead.
  2. Step 2: Determine test outcome

    Since the actual value (6) does not match the expected value (5), the assertion fails and the test fails with an assertion error.
  3. Final Answer:

    Test fails with assertion error -> Option A
  4. Quick Check:

    Expected != Actual causes failure [OK]
Hint: Mismatch expected vs actual fails test [OK]
Common Mistakes:
  • Assuming test passes if values differ
  • Thinking compilation error occurs
  • Believing test is skipped automatically
4. Identify the error in this JUnit 5 test method that prevents the quality gate from passing:
@Test
void testIsPositive() {
    int number = -1;
    assertTrue(number > 0);
}
medium
A. The test method name must start with 'test'
B. The test method is missing the @BeforeEach annotation
C. The assertion condition is incorrect for the test data
D. assertTrue is not a valid JUnit assertion

Solution

  1. Step 1: Analyze the assertion condition

    The test asserts that number > 0 but number is -1, so the condition is false.
  2. Step 2: Understand test failure impact on quality gate

    Because the assertion fails, the test fails and the quality gate will not pass.
  3. Final Answer:

    The assertion condition is incorrect for the test data -> Option C
  4. Quick Check:

    Assertion false causes test failure [OK]
Hint: Check if assertion matches test data truth [OK]
Common Mistakes:
  • Thinking @BeforeEach is required for this test
  • Believing method name must start with 'test'
  • Assuming assertTrue is invalid in JUnit
5. In a continuous integration pipeline using JUnit tests and a code quality gate, which combination best ensures that only well-tested code is merged?
  • 1. All tests must pass
  • 2. Code coverage must be above 80%
  • 3. No critical bugs reported by static analysis
  • 4. Code formatting checks are ignored
hard
A. 2 and 4 only
B. 1 and 4 only
C. All 1, 2, 3, and 4
D. 1, 2, and 3 only

Solution

  1. Step 1: Identify essential quality gate criteria

    Tests passing, high code coverage, and no critical bugs are key to ensuring code quality before merge.
  2. Step 2: Evaluate code formatting importance

    Ignoring code formatting (point 4) can reduce code quality and maintainability, so it should not be ignored.
  3. Final Answer:

    1, 2, and 3 only -> Option D
  4. Quick Check:

    Pass tests + coverage + no bugs = good quality gate [OK]
Hint: Include tests, coverage, and bug checks for quality [OK]
Common Mistakes:
  • Ignoring code formatting importance
  • Assuming only tests passing is enough
  • Thinking code coverage below 80% is acceptable
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