Bird
Raised Fist0
JUnittesting~20 mins

Unit testing concept in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
JUnit Unit Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
JUnit test method output
What will be the result of running this JUnit 5 test method?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class CalculatorTest {
    @Test
    void testAdd() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}
ATest fails with AssertionError
BRuntime NullPointerException
CCompilation error due to missing import
DTest passes successfully
Attempts:
2 left
πŸ’‘ Hint
Check the assertion and the calculation result.
❓ assertion
intermediate
2:00remaining
Correct assertion for exception testing
Which JUnit 5 assertion correctly tests that a method throws a NullPointerException?
JUnit
void methodThatThrows() {
    throw new NullPointerException();
}
AassertThrows(NullPointerException.class, () -> methodThatThrows());
BassertEquals(NullPointerException.class, methodThatThrows());
CassertTrue(methodThatThrows() instanceof NullPointerException);
DassertNull(methodThatThrows());
Attempts:
2 left
πŸ’‘ Hint
Look for the assertion designed to check exceptions.
πŸ”§ Debug
advanced
2:00remaining
Identify the cause of test failure
Given this JUnit 5 test, why does it fail?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class StringTest {
    @Test
    void testLength() {
        String s = null;
        assertEquals(4, s.length());
    }
}
ATest passes because null length is treated as 0
BAssertionError because length is not 4
CNullPointerException because s is null when calling length()
DCompilation error due to missing import
Attempts:
2 left
πŸ’‘ Hint
Think about calling a method on a null object.
🧠 Conceptual
advanced
1:30remaining
Purpose of unit testing
What is the main purpose of unit testing in software development?
ATo check the performance of the system under load
BTo verify that individual parts of code work correctly in isolation
CTo deploy the software to production automatically
DTo test the entire application’s user interface
Attempts:
2 left
πŸ’‘ Hint
Unit testing focuses on small pieces of code, not the whole system.
❓ framework
expert
2:30remaining
JUnit 5 lifecycle method order
What is the correct order of JUnit 5 lifecycle methods when running multiple tests in a class?
A1, 2, 3, 4, 5
B1, 3, 2, 4, 5
C2, 1, 3, 4, 5
D2, 3, 4, 1, 5
Attempts:
2 left
πŸ’‘ Hint
Remember which methods run once per class and which run before/after each test.

Practice

(1/5)
1. What is the main purpose of a unit test in JUnit?
easy
A. To check small parts of code for errors early
B. To test the entire application at once
C. To deploy the application automatically
D. To write documentation for the code

Solution

  1. Step 1: Understand unit test scope

    Unit tests focus on small parts or units of code, not the whole app.
  2. Step 2: Identify main goal

    The goal is to find errors early by testing these small parts individually.
  3. Final Answer:

    To check small parts of code for errors early -> Option A
  4. Quick Check:

    Unit test purpose = check small code parts early [OK]
Hint: Unit tests check small code parts, not whole app [OK]
Common Mistakes:
  • Confusing unit tests with integration or system tests
  • Thinking unit tests deploy or document code
  • Assuming unit tests cover the entire application
2. Which annotation is used in JUnit to mark a method as a test?
easy
A. @Run
B. @Assert
C. @Test
D. @Check

Solution

  1. Step 1: Recall JUnit test annotation

    JUnit uses @Test to mark methods as tests.
  2. Step 2: Verify other options

    @Run, @Check, and @Assert are not valid JUnit test annotations.
  3. Final Answer:

    @Test -> Option C
  4. Quick Check:

    JUnit test method = @Test annotation [OK]
Hint: JUnit test methods use @Test annotation [OK]
Common Mistakes:
  • Using @Run instead of @Test
  • Confusing @Assert with test annotation
  • Assuming @Check is a JUnit annotation
3. What will be the result of this JUnit test?
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class CalcTest {
    @Test
    void testSum() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}
medium
A. Test passes successfully
B. Test fails with assertion error
C. Compilation error due to missing import
D. Runtime exception occurs

Solution

  1. Step 1: Analyze the test logic

    The test calculates 2 + 3 and stores 5 in result.
  2. Step 2: Check assertion

    assertEquals(5, result) compares expected 5 with actual 5, so it matches.
  3. Final Answer:

    Test passes successfully -> Option A
  4. Quick Check:

    2+3=5 matches expected 5 [OK]
Hint: If expected equals actual, test passes [OK]
Common Mistakes:
  • Assuming test fails when values match
  • Thinking import is missing (it's present)
  • Expecting runtime errors without cause
4. Identify the error in this JUnit test code:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SampleTest {
    @Test
    public void testMultiply() {
        int result = 3 * 4
        assertEquals(12, result);
    }
}
medium
A. Incorrect import statement
B. Wrong assertion method used
C. Test method missing @Test annotation
D. Missing semicolon after multiplication statement

Solution

  1. Step 1: Check syntax in testMultiply method

    The line 'int result = 3 * 4' is missing a semicolon at the end.
  2. Step 2: Verify other parts

    @Test annotation is present, assertion method is correct, imports are correct.
  3. Final Answer:

    Missing semicolon after multiplication statement -> Option D
  4. Quick Check:

    Java statements need semicolons [OK]
Hint: Look for missing semicolons in Java test methods [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking @Test annotation is missing
  • Assuming wrong assertion method
5. You want to write a unit test that checks if a method throws an exception when given invalid input. Which JUnit 5 feature should you use?
hard
A. Use @BeforeEach to catch exceptions
B. assertThrows to verify the exception is thrown
C. assertEquals to compare exception messages
D. Use @Disabled to skip the test

Solution

  1. Step 1: Understand exception testing in JUnit 5

    JUnit 5 provides assertThrows to check if a specific exception is thrown by code.
  2. Step 2: Evaluate other options

    assertEquals compares values, not exceptions; @BeforeEach runs before tests; @Disabled skips tests.
  3. Final Answer:

    assertThrows to verify the exception is thrown -> Option B
  4. Quick Check:

    Use assertThrows for exception tests [OK]
Hint: Use assertThrows to test exceptions in JUnit 5 [OK]
Common Mistakes:
  • Using assertEquals to check exceptions
  • Misusing @BeforeEach for exception handling
  • Disabling tests instead of testing exceptions
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