@Test
void testSum() {
assertEquals(5, sum(2, 3));
}
@Test
void testSumNegative() {
assertEquals(-1, sum(-2, 1));
} splits checks into two separate test methods, each with one assertion.
Step 2: Compare with other options
Options B, C, and D have multiple assertions in one test method, which breaks the single assertion rule.
Final Answer:
Separate tests with one assertion each. -> Option D
Quick Check:
One assertion per test method = @Test
void testSum() {
assertEquals(5, sum(2, 3));
}
@Test
void testSumNegative() {
assertEquals(-1, sum(-2, 1));
} [OK]
Hint: One test, one assertion, split tests if needed [OK]
Common Mistakes:
Putting multiple assertions in one test method
Not splitting tests logically
Confusing assertTrue with assertEquals usage
3. Given the following JUnit test, what will be the test result if sum(2, 3) returns 6?
A. The test method is missing the @Before annotation.
B. The assertEquals methods are used incorrectly with wrong parameter order.
C. The test method has multiple assertions, making it harder to find failures.
D. The test method should use assertTrue instead of assertEquals.
Solution
Step 1: Check number of assertions
The test has two assertEquals calls, violating the single assertion rule.
Step 2: Understand impact of multiple assertions
Multiple assertions make it unclear which check failed if the test fails.
Final Answer:
The test method has multiple assertions, making it harder to find failures. -> Option C
Quick Check:
Multiple assertions = unclear failure [OK]
Hint: One assertion per test avoids confusion [OK]
Common Mistakes:
Thinking multiple assertions are always better
Confusing @Before with test assertions
Misusing assertEquals parameter order
5. You have a method calculateDiscount that returns a discount percentage based on user type and purchase amount. How should you write JUnit tests following the single assertion per test debate best practice?
hard
A. Write separate test methods for each user type and purchase amount combination, each with one assertion.
B. Write tests only for the most common user type and skip others.
C. Write one test method that asserts the discount is never negative, ignoring user types.
D. Write one test method with multiple assertions checking all user types and amounts.
Solution
Step 1: Understand test coverage needs
Each user type and purchase amount combination can behave differently and needs separate testing.
Step 2: Apply single assertion per test
Writing separate test methods with one assertion each keeps tests clear and failures easy to locate.
Final Answer:
Write separate test methods for each user type and purchase amount combination, each with one assertion. -> Option A
Quick Check:
Separate focused tests = Write separate test methods for each user type and purchase amount combination, each with one assertion. [OK]
Hint: Split tests by case, one assertion each [OK]
Common Mistakes:
Combining many assertions in one test
Ignoring edge cases for coverage
Skipping tests for less common inputs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.