Bird
Raised Fist0
JUnittesting~20 mins

assertDoesNotThrow in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit assertDoesNotThrow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the result of this JUnit test using assertDoesNotThrow?
Consider the following JUnit test method. What will be the test execution result?
JUnit
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import org.junit.jupiter.api.Test;

public class SampleTest {
    @Test
    void testNoException() {
        assertDoesNotThrow(() -> {
            int a = 5;
            int b = 10;
            int c = a + b;
        });
    }
}
ATest passes because no exception is thrown inside the lambda.
BTest fails due to a compilation error in the lambda expression.
CTest fails because an exception is thrown inside the lambda.
DTest passes but with a warning about unused variables.
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects the lambda to run without exceptions.
❓ assertion
intermediate
2:00remaining
Which assertion correctly uses assertDoesNotThrow to check a method that might throw?
You have a method that might throw an IOException. Which assertion correctly tests that it does NOT throw any exception?
JUnit
void riskyMethod() throws IOException {
    // might throw IOException
}
AassertDoesNotThrow(riskyMethod());
BassertDoesNotThrow(() -> riskyMethod());
CassertDoesNotThrow(() -> { riskyMethod(); return null; });
DassertDoesNotThrow(() -> riskyMethod() throws IOException);
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects a lambda expression or executable.
🔧 Debug
advanced
2:00remaining
Why does this assertDoesNotThrow test fail?
Examine the following test code and identify why the test fails.
JUnit
assertDoesNotThrow(() -> {
    throw new Exception("Error");
});
AThe syntax of the lambda is incorrect; it needs a return statement.
BassertDoesNotThrow cannot accept lambdas that throw any exceptions.
CThe lambda throws a checked Exception, causing the test to fail.
DThe Exception class is not imported, causing a compile error.
Attempts:
2 left
💡 Hint
assertDoesNotThrow fails if the lambda throws an exception.
🧠 Conceptual
advanced
2:00remaining
What is the main purpose of assertDoesNotThrow in JUnit tests?
Choose the best description of what assertDoesNotThrow verifies in a test.
AIt verifies that the tested code runs without throwing any exceptions.
BIt verifies that the tested code throws a specific exception.
CIt verifies that the tested code returns a non-null value.
DIt verifies that the tested code completes within a time limit.
Attempts:
2 left
💡 Hint
Think about what 'does not throw' means.
❓ framework
expert
2:00remaining
Which option correctly captures the return value when using assertDoesNotThrow?
You want to test a method that returns a value and ensure it does not throw an exception. How do you capture the return value using assertDoesNotThrow?
JUnit
public String getData() throws Exception {
    return "data";
}
AassertDoesNotThrow(() -> { return getData(); });
BString result = assertDoesNotThrow(getData());
CString result = assertDoesNotThrow(() -> { getData(); });
DString result = assertDoesNotThrow(() -> getData());
Attempts:
2 left
💡 Hint
assertDoesNotThrow returns the value from the lambda if no exception occurs.

Practice

(1/5)
1. What is the main purpose of assertDoesNotThrow in JUnit testing?
easy
A. To assert that a method returns a specific value
B. To check that a block of code runs without throwing any exceptions
C. To verify that a specific exception is thrown by the code
D. To measure the execution time of a code block

Solution

  1. Step 1: Understand the purpose of assertDoesNotThrow

    This assertion is designed to confirm that no exceptions occur when running a code block.
  2. Step 2: Compare with other assertions

    Unlike assertions that check for exceptions or return values, assertDoesNotThrow ensures smooth execution without errors.
  3. Final Answer:

    To check that a block of code runs without throwing any exceptions -> Option B
  4. Quick Check:

    assertDoesNotThrow means no exceptions thrown [OK]
Hint: Remember: assertDoesNotThrow means code runs without errors [OK]
Common Mistakes:
  • Confusing assertDoesNotThrow with assertThrows
  • Thinking it checks return values
  • Assuming it measures performance
2. Which of the following is the correct syntax to use assertDoesNotThrow in JUnit 5?
easy
A. assertDoesNotThrow(methodToTest());
B. assertDoesNotThrow { methodToTest() };
C. assertDoesNotThrow(() -> methodToTest());
D. assertDoesNotThrow(methodToTest);

Solution

  1. Step 1: Recall the lambda syntax for assertDoesNotThrow

    assertDoesNotThrow expects a lambda expression or executable block, written as () -> methodToTest().
  2. Step 2: Identify incorrect options

    Options A, C, and D use incorrect syntax: A calls the method directly, C uses braces like other languages, D passes method reference without lambda.
  3. Final Answer:

    assertDoesNotThrow(() -> methodToTest()); -> Option C
  4. Quick Check:

    Use lambda () -> method() with assertDoesNotThrow [OK]
Hint: Use lambda syntax () -> method() inside assertDoesNotThrow [OK]
Common Mistakes:
  • Calling the method directly inside assertDoesNotThrow
  • Using curly braces instead of lambda arrows
  • Passing method name without parentheses or lambda
3. What will be the result of this JUnit test?
assertDoesNotThrow(() -> { int x = 5 / 1; });
medium
A. Test fails due to syntax error
B. Test fails due to ArithmeticException
C. Test fails due to NullPointerException
D. Test passes because no exception is thrown

Solution

  1. Step 1: Analyze the code inside assertDoesNotThrow

    The code divides 5 by 1, which is valid and does not throw any exception.
  2. Step 2: Understand assertDoesNotThrow behavior

    Since no exception occurs, the assertion passes and the test succeeds.
  3. Final Answer:

    Test passes because no exception is thrown -> Option D
  4. Quick Check:

    Division by 1 is safe, so no exception [OK]
Hint: Check if code inside lambda throws exception or not [OK]
Common Mistakes:
  • Assuming division always throws exception
  • Confusing ArithmeticException with other exceptions
  • Thinking assertDoesNotThrow checks return values
4. Identify the error in this JUnit test using assertDoesNotThrow:
assertDoesNotThrow(() -> { int y = 10 / 0; });
medium
A. ArithmeticException will cause test to fail
B. No error; test will pass
C. Syntax error due to missing semicolon
D. NullPointerException will cause test to fail

Solution

  1. Step 1: Examine the code inside assertDoesNotThrow

    The code divides 10 by 0, which causes an ArithmeticException at runtime.
  2. Step 2: Understand assertDoesNotThrow behavior on exceptions

    assertDoesNotThrow expects no exceptions; if one occurs, the test fails.
  3. Final Answer:

    ArithmeticException will cause test to fail -> Option A
  4. Quick Check:

    Division by zero throws ArithmeticException [OK]
Hint: Division by zero throws exception, so test fails [OK]
Common Mistakes:
  • Thinking test passes despite exception
  • Confusing exception types
  • Ignoring runtime exceptions inside lambda
5. You want to test a method processData() that may throw a checked exception. How do you use assertDoesNotThrow to verify it runs without exceptions?
hard
A. assertDoesNotThrow(() -> processData());
B. assertDoesNotThrow(processData());
C. assertDoesNotThrow(() -> { try { processData(); } catch(Exception e) {} });
D. assertDoesNotThrow(() -> { processData() });

Solution

  1. Step 1: Understand handling checked exceptions in lambdas

    assertDoesNotThrow accepts an Executable, whose execute() method throws Throwable, so checked exceptions are permitted in the lambda.
  2. Step 2: Identify the correct lambda syntax

    The standard syntax is a concise lambda: () -> processData().
  3. Step 3: Evaluate options

    A: correct single-expression lambda. B: direct method call without lambda. C: catches exceptions inside lambda, hiding potential failures. D: syntax error due to missing semicolon after processData() in the block lambda.
  4. Final Answer:

    assertDoesNotThrow(() -> processData()); -> Option A
  5. Quick Check:

    Lambda () -> processData() handles checked exceptions correctly [OK]
Hint: Use lambda syntax () -> processData() inside assertDoesNotThrow [OK]
Common Mistakes:
  • Calling method directly without lambda
  • Syntax errors like missing semicolon in block lambdas
  • Catching exceptions inside lambda hiding test failures
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard