0
0
JUnittesting~20 mins

Test class organization in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Test Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of JUnit test class with multiple test methods
Consider the following JUnit 5 test class. What will be the output when running this test class?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {

    @Test
    void testAdd() {
        assertEquals(5, 2 + 3);
    }

    @Test
    void testSubtract() {
        assertEquals(1, 3 - 2);
    }

    @Test
    void testMultiply() {
        assertEquals(6, 2 * 3);
    }
}
AAll three tests pass successfully.
BThe testSubtract method fails due to wrong assertion.
CThe testMultiply method throws an exception and fails.
DThe testAdd method is skipped and not executed.
Attempts:
2 left
💡 Hint
Check each assertion carefully to see if the expected and actual values match.
assertion
intermediate
2:00remaining
Correct assertion to verify exception thrown in JUnit test
You want to test that a method throws an IllegalArgumentException when given a negative number. Which assertion correctly tests this in JUnit 5?
AassertTrue(method(-1) instanceof IllegalArgumentException);
BassertEquals(IllegalArgumentException.class, method(-1));
CassertThrows(IllegalArgumentException.class, () -> method(-1));
DassertDoesNotThrow(() -> method(-1));
Attempts:
2 left
💡 Hint
Look for the JUnit 5 method designed to check exceptions.
🔧 Debug
advanced
2:00remaining
Identify the problem in this JUnit test class setup
Examine the following JUnit 5 test class. What is the main issue that will cause tests not to run as expected?
JUnit
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ServiceTest {

    private static Service service;

    @BeforeAll
    static void setup() {
        service = new Service();
    }

    @Test
    void testServiceMethod() {
        assertTrue(service.isReady());
    }
}
AThe test class is missing a constructor, so tests won't run.
BThe test method 'testServiceMethod' is missing the @BeforeEach annotation.
CThe 'service' variable should not be static for @BeforeAll to work.
DThe @BeforeAll method must be static, but 'setup' is not static.
Attempts:
2 left
💡 Hint
Check the rules for @BeforeAll methods in JUnit 5.
framework
advanced
2:00remaining
Best practice for organizing test classes in a large Java project
In a large Java project using JUnit, what is the best practice for organizing test classes to keep tests maintainable and clear?
APut all test classes in a single package regardless of the classes they test.
BPlace test classes in the same package as the classes they test, but under a separate 'test' source folder.
CMix test classes with production classes in the same source folder and package.
DPlace test classes in a completely different project unrelated to the main codebase.
Attempts:
2 left
💡 Hint
Think about how to easily find tests related to each class.
🧠 Conceptual
expert
2:00remaining
Understanding test class lifecycle annotations in JUnit 5
Which statement correctly describes the difference between @BeforeEach and @BeforeAll annotations in JUnit 5 test classes?
A@BeforeEach runs before each test method; @BeforeAll runs once before all tests.
B@BeforeEach runs once before all tests; @BeforeAll runs before each test method.
C@BeforeEach and @BeforeAll both run once before all tests but differ in visibility.
D@BeforeEach runs after each test; @BeforeAll runs after all tests.
Attempts:
2 left
💡 Hint
Think about how often each setup method runs during the test class execution.