0
0
JUnittesting~20 mins

Checking exception message in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Message Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check exception message with JUnit 5
Which JUnit 5 assertion correctly verifies that a method throws an IllegalArgumentException with the message "Invalid input"?
JUnit
public void testInvalidInput() {
    // methodUnderTest throws IllegalArgumentException with message "Invalid input" when input is negative
}
AassertThrows(IllegalArgumentException.class, () -> methodUnderTest(-1), "Invalid input");
B
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> methodUnderTest(-1));
assertEquals("Invalid input", thrown.getMessage());
CassertThrows(IllegalArgumentException.class, () -> methodUnderTest(-1)).getMessage().equals("Invalid input");
DassertThatThrownBy(() -> methodUnderTest(-1)).hasMessage("Invalid input");
Attempts:
2 left
💡 Hint
Use assertThrows to capture the exception, then check its message separately.
Predict Output
intermediate
2:00remaining
Output of JUnit test with exception message assertion
What is the result of running this JUnit 5 test method if methodUnderTest throws IllegalArgumentException with message "Wrong value" instead of "Invalid input"?
JUnit
import static org.junit.jupiter.api.Assertions.*;

@Test
void testExceptionMessage() {
    IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> methodUnderTest(-5));
    assertEquals("Invalid input", ex.getMessage());
}

void methodUnderTest(int x) {
    if (x < 0) throw new IllegalArgumentException("Wrong value");
}
ATest fails with IllegalArgumentException but no assertion error
BTest passes successfully
CTest fails with NullPointerException
DTest fails with AssertionError: expected 'Invalid input' but was 'Wrong value'
Attempts:
2 left
💡 Hint
Check the difference between expected and actual exception messages.
🔧 Debug
advanced
2:00remaining
Identify the error in exception message assertion
What is wrong with this JUnit 5 test code that tries to check the exception message?
JUnit
assertThrows(IllegalArgumentException.class, () -> methodUnderTest(-1), msg -> assertEquals("Invalid input", msg));
AassertThrows does not accept a third argument; this causes a runtime error.
BThe lambda parameter 'msg' is a String, but assertThrows expects a Consumer<Throwable>.
CThe third argument is a failure message string, not a lambda; this causes a compilation error.
DThe lambda should return a boolean, but assertEquals returns void, causing a type mismatch.
Attempts:
2 left
💡 Hint
Check the method signature of assertThrows in JUnit 5.
framework
advanced
2:00remaining
Best practice for checking exception messages in JUnit 5
Which approach is considered best practice for verifying exception messages in JUnit 5 tests?
AUse assertThrows to capture the exception, then assert the message with assertEquals separately.
BUse @Test(expected=Exception.class) annotation and check message in a separate test.
CUse assertThrows with a third argument as a lambda to check the message inline.
DUse try-catch block and fail() method to check exception message manually.
Attempts:
2 left
💡 Hint
Consider readability and clarity of test code.
🧠 Conceptual
expert
2:00remaining
Why check exception messages in tests?
Why is it important to verify the exception message in addition to the exception type in automated tests?
ABecause exception messages provide detailed context that distinguishes different error causes of the same exception type.
BBecause checking messages improves test execution speed significantly.
CBecause exception messages are required by the compiler to identify exceptions.
DBecause exception messages are always unique and guarantee no false positives.
Attempts:
2 left
💡 Hint
Think about why the same exception type might be thrown for different reasons.