Bird
Raised Fist0
JUnittesting~20 mins

Test class organization in JUnit - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of organizing test methods inside a test class in JUnit?
easy
A. To group related tests for better readability and maintenance
B. To make tests run faster by parallel execution
C. To reduce the number of test cases needed
D. To avoid writing assertions in test methods

Solution

  1. Step 1: Understand test class role

    Test classes group related test methods logically to keep tests organized and easy to find.
  2. Step 2: Identify benefits of grouping

    Grouping improves readability and helps maintain tests as the project grows.
  3. Final Answer:

    To group related tests for better readability and maintenance -> Option A
  4. Quick Check:

    Test class organization = Group related tests [OK]
Hint: Test classes group related tests logically [OK]
Common Mistakes:
  • Thinking test classes speed up test execution
  • Believing test classes reduce test count
  • Confusing test class purpose with assertion writing
2. Which of the following is the correct way to name a JUnit test class for testing a Calculator class?
easy
A. CalcTestClass
B. TestCalculatorClass
C. CalculatorTest
D. CalculatorTesting

Solution

  1. Step 1: Review common naming conventions

    JUnit test classes usually use the pattern ClassNameTest for clarity and consistency.
  2. Step 2: Match options to convention

    "CalculatorTest" follows this convention exactly, making it clear and standard.
  3. Final Answer:

    CalculatorTest -> Option C
  4. Quick Check:

    Test class naming = ClassNameTest [OK]
Hint: Use ClassNameTest format for test classes [OK]
Common Mistakes:
  • Using vague or inconsistent test class names
  • Adding unnecessary words like 'Class' or 'Testing'
  • Not matching test class name to the class under test
3. Given the following JUnit test class snippet, what will be the output when running the tests?
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

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

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

    @Test
    void testMultiply() {
        assertEquals(6, 2 * 3);
    }
}
medium
A. testAdd fails, others pass
B. All tests pass
C. testSubtract fails, others pass
D. testMultiply fails, others pass

Solution

  1. Step 1: Check each assertion correctness

    testAdd: 2 + 3 = 5 matches expected 5; testSubtract: 3 - 2 = 1 matches expected 1; testMultiply: 2 * 3 = 6 matches expected 6.
  2. Step 2: Confirm all tests pass

    Since all assertions match expected values, all tests will pass successfully.
  3. Final Answer:

    All tests pass -> Option B
  4. Quick Check:

    Assertions correct = All pass [OK]
Hint: Verify each assertion matches expected result [OK]
Common Mistakes:
  • Assuming tests fail without checking assertion values
  • Confusing test method names with test results
  • Ignoring that all assertions are correct
4. Identify the problem in the following JUnit test class organization:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class StringUtils {
    static boolean isEmpty(String str) {
        return str.length() == 0;
    }
}

public class StringUtilsTest {
    @Test
    void testIsEmpty() {
        assertTrue(StringUtils.isEmpty(""));
    }

    void testIsNotEmpty() {
        assertFalse(StringUtils.isEmpty("abc"));
    }
}
medium
A. Test methods should be static
B. Incorrect assertion method used in testIsEmpty
C. Test class name does not match tested class
D. Missing @Test annotation on testIsNotEmpty method

Solution

  1. Step 1: Check annotations on test methods

    testIsEmpty has @Test annotation, but testIsNotEmpty does not, so it won't run as a test.
  2. Step 2: Understand impact of missing annotation

    Without @Test, testIsNotEmpty is ignored during test execution, missing coverage.
  3. Final Answer:

    Missing @Test annotation on testIsNotEmpty method -> Option D
  4. Quick Check:

    All test methods need @Test [OK]
Hint: Every test method must have @Test annotation [OK]
Common Mistakes:
  • Forgetting @Test annotation on test methods
  • Thinking test methods must be static
  • Confusing assertion methods with annotations
5. You have a test class with multiple test methods for a UserService. You want to organize tests by feature (e.g., login, registration) within the same class. Which is the best practice to keep the test class organized and maintainable?
hard
A. Group test methods by feature using nested classes or clear method name prefixes
B. Put all tests in one method to reduce class size
C. Create separate test classes for each feature without grouping
D. Use random order of test methods to avoid bias

Solution

  1. Step 1: Consider organization within a test class

    Grouping related tests by feature improves readability and helps maintain tests logically.
  2. Step 2: Use nested classes or naming conventions

    Nested classes or method name prefixes (e.g., loginTest_*, registrationTest_*) clearly separate features inside one test class.
  3. Final Answer:

    Group test methods by feature using nested classes or clear method name prefixes -> Option A
  4. Quick Check:

    Organize tests by feature inside class [OK]
Hint: Use nested classes or prefixes to group feature tests [OK]
Common Mistakes:
  • Putting all tests in one method reduces clarity
  • Creating too many small classes without grouping
  • Ignoring logical grouping harms maintainability
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