0
0
JUnittesting~20 mins

@Test annotation in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.