Bird
Raised Fist0
JUnittesting~20 mins

@Test annotation in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit @Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple JUnit test with @Test
What will be the result of running this JUnit test class?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class SampleTest {
    @Test
    void testSum() {
        assertEquals(5, 2 + 3);
    }
}
ACompilation error due to missing main method.
BTest fails with an AssertionError.
CTest passes successfully with no errors.
DRuntime exception due to null pointer.
Attempts:
2 left
💡 Hint
Remember that @Test marks a method as a test and assertEquals checks expected vs actual values.
❓ assertion
intermediate
2:00remaining
Identify the failing assertion in a JUnit test
Given this test method, which assertion will cause the test to fail?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class AssertionTest {
    @Test
    void testValues() {
        assertTrue(3 > 2);
        assertFalse(5 < 1);
        assertEquals("hello", "hello");
        assertNotNull(null);
    }
}
AassertNotNull(null);
BassertFalse(5 < 1);
CassertEquals("hello", "hello");
DassertTrue(3 > 2);
Attempts:
2 left
💡 Hint
Check which assertion conditions are false or invalid.
🧠 Conceptual
advanced
1:30remaining
Purpose of the @Test annotation in JUnit
What is the main purpose of the @Test annotation in a JUnit test class?
ATo ignore a test method during execution.
BTo specify the order in which tests should run.
CTo provide setup code before each test method.
DTo mark a method as a test method that JUnit should run.
Attempts:
2 left
💡 Hint
Think about how JUnit knows which methods to execute as tests.
🔧 Debug
advanced
2:00remaining
Identify the error in this JUnit test method
What error will occur when running this test class?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class DebugTest {
    @Test
    void testDivision() {
        int result = 10 / 0;
        assertEquals(0, result);
    }
}
ACompilation error due to division by zero.
BArithmeticException: division by zero at runtime.
CAssertionError because result is not zero.
DTest passes successfully.
Attempts:
2 left
💡 Hint
Division by zero is not allowed in Java and causes an exception.
❓ framework
expert
2:30remaining
Behavior of @Test with expected exception in JUnit 5
Consider this test method in JUnit 5. What is the test result?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ExceptionTest {
    @Test
    void testException() {
        assertThrows(IllegalArgumentException.class, () -> {
            throw new IllegalArgumentException("Invalid argument");
        });
    }
}
ATest passes because the expected exception is thrown.
BTest fails because no exception is thrown.
CTest fails due to a compilation error in assertThrows usage.
DTest fails because a different exception is thrown.
Attempts:
2 left
💡 Hint
assertThrows expects the specified exception to be thrown inside the lambda.

Practice

(1/5)
1. What is the main purpose of the @Test annotation in JUnit?
easy
A. To mark a method as a test case that JUnit will run
B. To declare a method as private
C. To specify the return type of a method
D. To import JUnit libraries

Solution

  1. Step 1: Understand the role of @Test

    The @Test annotation tells JUnit which methods are test cases to execute.
  2. Step 2: Eliminate unrelated options

    Options B, C, and D do not relate to marking test methods.
  3. Final Answer:

    To mark a method as a test case that JUnit will run -> Option A
  4. Quick Check:

    @Test marks test methods [OK]
Hint: Remember: @Test means 'run this method as test' [OK]
Common Mistakes:
  • Thinking @Test changes method visibility
  • Confusing @Test with import statements
  • Assuming @Test sets return type
2. Which of the following is the correct syntax for a JUnit test method using @Test?
easy
A. public void testMethod(int x) {}
B. private int testMethod() {}
C. public void testMethod() {}
D. public String testMethod() {}

Solution

  1. Step 1: Recall JUnit test method rules

    Test methods must be public, return void, and take no parameters.
  2. Step 2: Check each option

    public void testMethod() {} matches all rules. Options B, C, and D violate visibility, return type, or parameters.
  3. Final Answer:

    public void testMethod() {} -> Option C
  4. Quick Check:

    Public, void, no params = correct test method [OK]
Hint: Test methods: public + void + no parameters [OK]
Common Mistakes:
  • Making test methods private
  • Adding parameters to test methods
  • Using non-void return types
3. Given the code below, what will be the test result when running with JUnit?
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

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

Solution

  1. Step 1: Analyze the test method logic

    The method calculates 2 + 3 and stores in result, then asserts result equals 5.
  2. Step 2: Check assertion correctness

    Since 2 + 3 is 5, the assertion assertEquals(5, result) will pass.
  3. Final Answer:

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

    2 + 3 = 5, assertion true [OK]
Hint: If assertion matches calculation, test passes [OK]
Common Mistakes:
  • Assuming test fails without checking assertion
  • Confusing compilation errors with test failures
  • Ignoring that test methods return void
4. Identify the error in the following JUnit test method:
import org.junit.jupiter.api.Test;

public class ErrorTest {
    @Test
    private void testHidden() {
        // test code
    }
}
medium
A. Test method must be public, not private
B. Missing return statement in test method
C. Test method should have parameters
D. Missing @BeforeEach annotation

Solution

  1. Step 1: Check method visibility

    JUnit requires test methods to be public so it can access and run them.
  2. Step 2: Validate other method properties

    Test methods must be void and parameterless; private visibility is the error here.
  3. Final Answer:

    Test method must be public, not private -> Option A
  4. Quick Check:

    Test methods must be public [OK]
Hint: Test methods must be public, not private [OK]
Common Mistakes:
  • Making test methods private
  • Adding parameters to test methods
  • Confusing lifecycle annotations with test method requirements
5. You want to write a JUnit test method that checks if a method throws an exception. Which is the correct way to use @Test for this purpose in JUnit 5?
hard
A. Use @Test(expected = Exception.class) annotation parameter
B. Add throws Exception to the test method signature
C. Write a try-catch block without @Test
D. Use @Test with assertThrows() inside the method

Solution

  1. Step 1: Recall JUnit 5 exception testing

    JUnit 5 uses assertThrows() inside @Test methods to check exceptions.
  2. Step 2: Eliminate outdated or incorrect options

    Use @Test(expected = Exception.class) annotation parameter is from JUnit 4 and invalid in JUnit 5. Add throws Exception to the test method signature only declares exception, not tests it. Write a try-catch block without @Test misses @Test.
  3. Final Answer:

    Use @Test with assertThrows() inside the method -> Option D
  4. Quick Check:

    JUnit 5 uses assertThrows() inside @Test [OK]
Hint: JUnit 5 tests exceptions with assertThrows() inside @Test [OK]
Common Mistakes:
  • Using JUnit 4 style @Test(expected=) in JUnit 5
  • Only declaring throws without assertion
  • Not using @Test annotation
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