Choose the best description of what a code quality gate does in the context of JUnit tests.
Think about how quality gates help maintain code standards before merging.
Code quality gates enforce rules like passing tests and minimum coverage before code merges, preventing bad code from entering the main branch.
Given the following JUnit test class, what will be the outcome if a quality gate requires 80% coverage but only 60% is achieved?
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; } }
Passing tests do not guarantee quality gate success if coverage is low.
Even if all tests pass, the quality gate can fail if coverage thresholds are not met, as in this case where subtract is untested.
Choose the correct JUnit 5 assertion to test that calling divide(5, 0) throws ArithmeticException.
public class MathUtils { public static int divide(int a, int b) { return a / b; } }
Look for the method that expects an exception to be thrown.
assertThrows checks that the specified exception is thrown by the code block. Other options either check wrong things or expect no exception.
Given this test suite, why does the quality gate fail despite all tests passing?
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); } }
Think about what code is not covered by tests.
All tests pass but the quality gate fails because the isNotEmpty method is never tested, lowering coverage below the threshold.
Which approach correctly integrates JUnit test results and coverage reports to enforce a quality gate in a CI pipeline?
Think about automation and tools that measure coverage in CI.
Automated CI pipelines run JUnit tests and use tools like JaCoCo to measure coverage. The build fails if coverage is below the set threshold, enforcing the quality gate.