In JUnit testing, why does having a clear test structure help the testing process?
Think about how organized tests help developers understand and fix problems.
A clear test structure separates setup, execution, and verification steps. This makes tests easier to read and maintain, so developers can quickly find and fix issues.
Consider this JUnit test class. What will be the test result when running it?
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class SampleTest { @Test void testSum() { int result = 2 + 3; assertEquals(5, result); } @Test void testFail() { assertTrue(false); } }
Look at the assertions in each test method.
The first test checks if 2 + 3 equals 5, which is true, so it passes. The second test asserts true is false, which fails.
In JUnit, which assertion correctly verifies that a list has exactly 3 elements?
Think about how to check the exact number of elements in a list.
assertEquals(3, list.size()) checks that the list size is exactly 3. Other options check different conditions.
What is wrong with this JUnit test method?
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class DebugTest { @Test void testDivide() { int result = 10 / 0; assertEquals(0, result); } }
What happens when dividing by zero in Java?
Dividing by zero causes an ArithmeticException at runtime, so the test will error out before the assertion.
What is the main benefit of using the @BeforeEach annotation in JUnit test classes?
Think about how to avoid repeating setup code in every test method.
@BeforeEach runs setup code before every test method, so common setup is written once, making tests clearer and easier to maintain.