0
0
JUnittesting~20 mins

Single assertion per test debate in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do some developers prefer a single assertion per test?

In JUnit testing, some developers argue that each test method should contain only one assertion. What is the main reason for this preference?

AIt makes it easier to identify the exact cause of failure when a test fails.
BIt reduces the total number of test methods needed in the project.
CIt improves the performance of the test suite by running fewer assertions.
DIt allows multiple failures to be reported in a single test run.
Attempts:
2 left
💡 Hint

Think about how test failures are reported and how debugging is simplified.

assertion
intermediate
2:00remaining
What is the result of this JUnit test with multiple assertions?

Consider the following JUnit test method:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class SampleTest {
    @Test
    void testMultipleAssertions() {
        assertEquals(5, 2 + 3);
        assertEquals(4, 2 * 2);
        assertEquals(6, 2 + 2);
    }
}

What will be the outcome when this test runs?

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class SampleTest {
    @Test
    void testMultipleAssertions() {
        assertEquals(5, 2 + 3);
        assertEquals(4, 2 * 2);
        assertEquals(6, 2 + 2);
    }
}
AThe test fails at the second assertion and continues to the third.
BThe test passes because the first two assertions are correct.
CThe test fails at the first assertion and stops executing further assertions.
DThe test fails at the third assertion with an AssertionError.
Attempts:
2 left
💡 Hint

JUnit stops executing a test method when an assertion fails.

🔧 Debug
advanced
2:00remaining
Identify the problem with multiple assertions in one test

Given this JUnit test method:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class CalculatorTest {
    @Test
    void testAddAndSubtract() {
        assertEquals(7, Calculator.add(3, 4));
        assertEquals(1, Calculator.subtract(5, 4));
        assertEquals(10, Calculator.add(5, 5));
    }
}

If the second assertion fails, what is the main issue with having all assertions in one test method?

AThe test stops at the failing assertion, so the third assertion is never checked.
BJUnit will report all assertion failures in the test method at once.
CThe test will pass if any one assertion passes.
DThe test method will run slower because of multiple assertions.
Attempts:
2 left
💡 Hint

Think about how JUnit handles assertion failures inside a test method.

framework
advanced
2:00remaining
Which JUnit feature helps test multiple assertions without stopping at first failure?

JUnit 5 introduced a feature that allows multiple assertions to be checked in a single test method, reporting all failures at once instead of stopping at the first failure. What is this feature called?

AassertGroup
BassertMultiple
CassertAll
DassertBatch
Attempts:
2 left
💡 Hint

It groups assertions and reports all failures together.

Predict Output
expert
2:00remaining
What is the output of this JUnit test using assertAll?

Analyze the following JUnit 5 test method:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class MathTest {
    @Test
    void testMultipleFailures() {
        assertAll("Multiple checks",
            () -> assertEquals(10, 5 + 5),
            () -> assertEquals(3, 1 + 1),
            () -> assertTrue(2 > 3)
        );
    }
}

What will be the result when this test runs?

AThe test fails reporting only the second assertion failure.
BThe test fails reporting two assertion failures: second and third assertions.
CThe test fails reporting only the third assertion failure.
DThe test passes because the first assertion is correct.
Attempts:
2 left
💡 Hint

assertAll runs all assertions and reports all failures together.