Bird
Raised Fist0
JUnittesting~20 mins

Test classes and naming in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test Class Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Test Class Naming Conventions

Why is it important to follow naming conventions for test classes in JUnit?

ATo ensure test classes are easily identifiable and grouped logically in reports and IDEs
BTo make test classes run faster during execution
CTo reduce the size of the compiled test code
DTo prevent test classes from being executed accidentally
Attempts:
2 left
💡 Hint

Think about how naming helps humans and tools recognize test code.

❓ Predict Output
intermediate
2:00remaining
JUnit Test Class Naming and Execution

Given the following JUnit test classes, which one will be automatically detected and run by default JUnit 5 test runners?

JUnit
public class CalculatorTest {
    @org.junit.jupiter.api.Test
    void testAdd() {}
}

public class CalculatorTests {
    @org.junit.jupiter.api.Test
    void testSubtract() {}
}

public class TestCalculator {
    @org.junit.jupiter.api.Test
    void testMultiply() {}
}

public class CalcTest {
    @org.junit.jupiter.api.Test
    void testDivide() {}
}
AAll four classes are detected
BOnly CalculatorTest and CalculatorTests are detected
COnly TestCalculator is detected
DOnly CalculatorTest is detected
Attempts:
2 left
💡 Hint

JUnit 5 detects any class with @Test annotations regardless of name by default.

❓ assertion
advanced
2:00remaining
Correct Assertion for Test Class Naming Check

Which assertion correctly verifies that a test class name ends with 'Test' in a JUnit test?

JUnit
String className = "CalculatorTest";
AassertFalse(className.contains("Test"));
BassertEquals(className.startsWith("Test"), true);
CassertEquals(className.length(), 4);
DassertTrue(className.endsWith("Test"));
Attempts:
2 left
💡 Hint

Check the method that tests if a string ends with a specific suffix.

🔧 Debug
advanced
2:00remaining
Debugging a Test Class Naming Issue

Consider this JUnit 5 test class:

public class calculatorTest {
    @org.junit.jupiter.api.Test
    void testAdd() {}
}

Why might this class cause confusion or issues in a team project?

AMissing @Test annotation on the method
BClass name does not follow Java naming conventions (should start with uppercase letter)
CTest method name does not start with 'test'
DClass is missing a package declaration
Attempts:
2 left
💡 Hint

Java class names should follow standard capitalization rules.

❓ framework
expert
2:00remaining
JUnit 5 Test Class Discovery Rules

Which statement correctly describes how JUnit 5 discovers test classes by default?

AJUnit 5 only runs classes whose names end with 'Test' or 'Tests'
BJUnit 5 requires test classes to extend a specific base test class to be discovered
CJUnit 5 runs any class containing at least one method annotated with @Test, regardless of class name
DJUnit 5 only runs classes located in a 'test' package
Attempts:
2 left
💡 Hint

Think about how JUnit 5 improved test discovery compared to older versions.

Practice

(1/5)
1. Which of the following is the best practice for naming a JUnit test class?
easy
A. Use all uppercase letters for the class name
B. Start the class name with Test and end with Class
C. Use the suffix Test at the end of the class name
D. Name the class with random characters to avoid conflicts

Solution

  1. Step 1: Understand naming conventions for test classes

    JUnit test classes should be named clearly to indicate they contain tests. The common convention is to add the suffix Test to the class name being tested.
  2. Step 2: Evaluate the options

    Use the suffix Test at the end of the class name follows the standard convention by using the suffix Test. Options B, C, and D do not follow common or recommended practices.
  3. Final Answer:

    Use the suffix Test at the end of the class name -> Option C
  4. Quick Check:

    Test class suffix = Test [OK]
Hint: Test classes end with 'Test' suffix for clarity [OK]
Common Mistakes:
  • Starting class name with 'Test' instead of suffix
  • Using uppercase only names
  • Using unclear or random names
2. Which of the following is a correct JUnit test class declaration in Java?
easy
A. public class calculator_test { }
B. class TestCalculator { }
C. public test class Calculator { }
D. public class CalculatorTest { }

Solution

  1. Step 1: Check Java class declaration syntax

    Java classes must be declared with the keyword class and the class name should follow Java naming conventions (PascalCase). The class should be public if accessed externally.
  2. Step 2: Validate each option

    public class CalculatorTest { } is syntactically correct and follows naming conventions. class TestCalculator { } misses the public modifier but is valid Java; however, the name does not end with Test. public test class Calculator { } uses an invalid keyword test. public class calculator_test { } uses underscores and lowercase which is against Java class naming conventions.
  3. Final Answer:

    public class CalculatorTest { } -> Option D
  4. Quick Check:

    Correct Java class syntax = public class CalculatorTest { } [OK]
Hint: Use 'public class ClassNameTest' for test classes [OK]
Common Mistakes:
  • Using invalid keywords like 'test'
  • Using underscores in class names
  • Not using PascalCase for class names
3. Given the following test class, what will be the output when running the tests?
public class MathUtilsTest {
    @org.junit.jupiter.api.Test
    void testAdd() {
        int result = 2 + 3;
        org.junit.jupiter.api.Assertions.assertEquals(5, result);
    }

    @org.junit.jupiter.api.Test
    void testSubtract() {
        int result = 5 - 3;
        org.junit.jupiter.api.Assertions.assertEquals(1, result);
    }
}
medium
A. Both tests pass
B. One test passes, one test fails
C. Both tests fail
D. Compilation error due to missing imports

Solution

  1. Step 1: Analyze the testAdd method

    The testAdd method calculates 2 + 3 = 5 and asserts it equals 5. This assertion passes.
  2. Step 2: Analyze the testSubtract method

    The testSubtract method calculates 5 - 3 = 2 but asserts it equals 1. This assertion fails.
  3. Final Answer:

    One test passes, one test fails -> Option B
  4. Quick Check:

    Assertions: testAdd=pass, testSubtract=fail [OK]
Hint: Check assertion values carefully to spot failures [OK]
Common Mistakes:
  • Assuming both tests pass without checking values
  • Ignoring assertion expected vs actual values
  • Confusing compilation errors with assertion failures
4. Identify the error in the following JUnit test class:
public class StringUtilsTest {
    @Test
    public void testIsEmpty() {
        boolean result = StringUtils.isEmpty("");
        assertTrue(result);
    }
}
medium
A. Missing import for @Test annotation
B. Method name should start with 'should'
C. assertTrue is not a valid assertion method
D. Class name should not end with 'Test'

Solution

  1. Step 1: Check for missing imports

    The code uses @Test annotation and assertTrue method but does not show imports. Without importing org.junit.jupiter.api.Test and org.junit.jupiter.api.Assertions.assertTrue, this code will not compile.
  2. Step 2: Validate other options

    Method naming conventions are flexible; starting with 'should' is not mandatory. assertTrue is a valid assertion method. Class name ending with 'Test' is correct.
  3. Final Answer:

    Missing import for @Test annotation -> Option A
  4. Quick Check:

    Missing imports cause compilation errors [OK]
Hint: Always import @Test and assertion methods [OK]
Common Mistakes:
  • Assuming method names must start with 'should'
  • Confusing assertion method names
  • Ignoring missing imports causing errors
5. You have a class named OrderProcessor. Which of the following is the best name for its JUnit test class and a test method that checks if an order is processed successfully?
hard
A. Class: OrderProcessorTest, Method: testProcessOrderSuccess
B. Class: TestOrderProcessor, Method: processOrder
C. Class: OrderProcessorTests, Method: checkOrder
D. Class: OrderProcessor_Test, Method: orderSuccessTest

Solution

  1. Step 1: Choose the correct test class naming

    The best practice is to name the test class by adding Test suffix to the class under test. Class: OrderProcessorTest, Method: testProcessOrderSuccess uses OrderProcessorTest, which is correct. Class: TestOrderProcessor, Method: processOrder prefixes with 'Test', which is less common. Class: OrderProcessorTests, Method: checkOrder uses plural 'Tests', which is acceptable but less standard. Class: OrderProcessor_Test, Method: orderSuccessTest uses underscore, which is not recommended.
  2. Step 2: Choose the best test method name

    Test method names should describe the behavior tested clearly. Class: OrderProcessorTest, Method: testProcessOrderSuccess's testProcessOrderSuccess clearly states the test purpose. Other options are less descriptive or ambiguous.
  3. Final Answer:

    Class: OrderProcessorTest, Method: testProcessOrderSuccess -> Option A
  4. Quick Check:

    Test class suffix + descriptive method name = Class: OrderProcessorTest, Method: testProcessOrderSuccess [OK]
Hint: Use ClassNameTest and descriptive test method names [OK]
Common Mistakes:
  • Using underscores in class names
  • Using vague test method names
  • Prefixing class names with 'Test' instead of suffix
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