0
0
JUnittesting~20 mins

Code quality gates in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.