Which of the following reasons best explains why JUnit became the standard testing framework for Java?
Think about what makes a testing tool practical and popular among developers.
JUnit became standard because it integrates well with popular Java tools and IDEs, allowing easy writing, running, and automation of tests.
What will be the result of running this JUnit 5 test method?
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test void testAdd() { int result = 2 + 3; assertEquals(5, result); } }
Check if the assertion matches the calculation result.
The test adds 2 and 3, expecting 5. The assertion matches, so the test passes.
Which assertion method should you use to verify that a method throws a specific exception in JUnit 5?
Think about how to check for exceptions in tests.
assertThrows() is designed to check that a block of code throws a specific exception.
Which JUnit feature helps organize tests into groups that can be run selectively?
JUnit 5 introduced a way to label tests for selective execution.
The @Tag annotation allows labeling tests, so you can run groups of tests by tag.
What will be the result of running this JUnit 5 test method?
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class StringTest { @Test void testSubstring() { String s = "hello"; String sub = s.substring(1, 3); assertEquals("el", sub); } }
Remember how substring(start, end) works in Java: end index is exclusive.
"hello".substring(1, 3) returns "el" (indices 1 to 2 inclusive), matching the expected "el". The test passes successfully.