Bird
Raised Fist0
JUnittesting~20 mins

Test method naming conventions in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Identify the correct JUnit test method naming convention
Which of the following method names follows the recommended JUnit 5 test method naming conventions for clarity and readability?
ATestCalculateSum
BcalculateSumTest
CtestCalculateSum_whenInputIsValid_shouldReturnCorrectSum
Dcalculate_sum_test
Attempts:
2 left
💡 Hint
JUnit 5 encourages descriptive method names that explain the test scenario and expected behavior.
❓ assertion
intermediate
2:00remaining
Determine the test method name from assertion purpose
You have a test that verifies the login method throws an exception when credentials are invalid. Which method name best reflects this test purpose?
AtestLogin
BloginShouldThrowExceptionForInvalidCredentials
CinvalidCredentialsTest
DcheckLoginException
Attempts:
2 left
💡 Hint
Good test method names describe the expected behavior and condition clearly.
❓ Predict Output
advanced
2:00remaining
What is the output of the test report for this method name?
Given the following JUnit 5 test method, what will be the displayed test name in the test report by default?
JUnit
import org.junit.jupiter.api.Test;

public class CalculatorTest {
    @Test
    void addTwoNumbers_shouldReturnSum() {
        // test code
    }
}
AaddTwoNumbersShouldReturnSum()
BaddTwoNumbers should return sum
CaddTwoNumbers_shouldReturnSum()
DaddTwoNumbers-shouldReturnSum
Attempts:
2 left
💡 Hint
JUnit 5 by default shows the method name as is unless a display name is specified.
🔧 Debug
advanced
2:00remaining
Find the naming issue causing test discovery failure
A JUnit 5 test method is not being executed by the test runner. Which method name is causing this issue?
JUnit
import org.junit.jupiter.api.Test;

public class SampleTest {
    @Test
    public void TestAddition() {
        // test code
    }
}
ATestAddition
BtestAddition
CadditionTest
Dtest_addition
Attempts:
2 left
💡 Hint
JUnit 5 requires test methods to be non-static and annotated with @Test; method name casing does not prevent execution.
❓ framework
expert
3:00remaining
Which naming style best supports behavior-driven development (BDD) in JUnit tests?
In BDD style testing with JUnit, which test method naming style is most appropriate to clearly express behavior and expected outcomes?
AvalidInputTest
BtestValidInputReturnsTrue
CcheckInput
DshouldReturnTrueWhenInputIsValid
Attempts:
2 left
💡 Hint
BDD style emphasizes expressing behavior in method names starting with 'should'.

Practice

(1/5)
1. Which of the following is the best practice for naming a JUnit test method?
easy
A. test1
B. check
C. shouldReturnTrueWhenInputIsValid
D. runTest

Solution

  1. Step 1: Understand naming clarity

    Good test method names clearly describe what is tested and the expected behavior.
  2. Step 2: Evaluate options

    shouldReturnTrueWhenInputIsValid uses camelCase and starts with 'should', describing expected behavior. Others are vague or generic.
  3. Final Answer:

    shouldReturnTrueWhenInputIsValid -> Option C
  4. Quick Check:

    Clear descriptive names = shouldReturnTrueWhenInputIsValid [OK]
Hint: Start test names with 'should' and describe behavior [OK]
Common Mistakes:
  • Using vague names like 'test1' or 'check'
  • Starting names with 'run' without context
  • Not using camelCase for readability
2. Which of the following is a syntactically correct JUnit 5 test method name?
easy
A. 2ShouldReturnFalseWhenInputIsNull
B. shouldReturnFalseWhenInputIsNull
C. should return false when input is null
D. should-return_false_when_input_is_null

Solution

  1. Step 1: Recall Java identifier rules for method names

    Java method names must be valid identifiers: start with letter/$_ , no spaces, hyphens, or starting digits; conventionally camelCase.
  2. Step 2: Check each option

    shouldReturnFalseWhenInputIsNull uses valid camelCase. The option starting with '2' can't start with digit. The option with hyphen '-' is invalid. The option with spaces is invalid.
  3. Final Answer:

    shouldReturnFalseWhenInputIsNull -> Option B
  4. Quick Check:

    CamelCase method names = shouldReturnFalseWhenInputIsNull [OK]
Hint: Use camelCase valid Java identifiers, no spaces/hyphens/digit starts [OK]
Common Mistakes:
  • Starting method names with digits
  • Using hyphens or spaces in method names
  • Ignoring Java identifier syntax rules
3. Given the following test method name, what behavior is it most likely testing?
shouldThrowExceptionWhenInputIsNegative
medium
A. The method tests that negative input is accepted without error
B. The method tests that negative input returns false
C. The method tests that input is positive
D. The method tests that an exception is thrown for negative input

Solution

  1. Step 1: Analyze the method name parts

    The name starts with 'shouldThrowException' indicating expected exception behavior.
  2. Step 2: Understand the condition

    'WhenInputIsNegative' means the test triggers when input is negative.
  3. Final Answer:

    The method tests that an exception is thrown for negative input -> Option D
  4. Quick Check:

    Method name meaning = exception thrown on negative input [OK]
Hint: Look for 'should' + action + condition in test names [OK]
Common Mistakes:
  • Ignoring 'ThrowException' meaning
  • Confusing exception with return values
  • Misreading the condition part of the name
4. Identify the problem in this JUnit test method name:
testshouldReturnTrueWhenValid
medium
A. It mixes 'test' prefix with 'should' which is redundant
B. It uses invalid characters
C. It is too short
D. It does not start with a verb

Solution

  1. Step 1: Understand naming redundancy

    Using both 'test' prefix and 'should' is redundant and not recommended.
  2. Step 2: Check other options

    No invalid characters are present, length is sufficient, and 'should' implies verb usage.
  3. Final Answer:

    It mixes 'test' prefix with 'should' which is redundant -> Option A
  4. Quick Check:

    Redundant prefixes = test + should [OK]
Hint: Avoid combining 'test' prefix with 'should' in names [OK]
Common Mistakes:
  • Using both 'test' and 'should' together
  • Assuming length affects correctness
  • Ignoring naming conventions for verbs
5. You want to write a JUnit test method name that clearly shows it tests if a user login fails when the password is empty. Which is the best name?
hard
A. shouldFailLoginWhenPasswordIsEmpty
B. testLoginFailEmptyPassword
C. loginFailsEmptyPassword
D. failLoginEmptyPasswordTest

Solution

  1. Step 1: Check clarity and style

    shouldFailLoginWhenPasswordIsEmpty uses 'should' + verb + condition, making it clear and readable.
  2. Step 2: Evaluate other options

    Options B, C, and D are less clear or mix styles, making them harder to understand quickly.
  3. Final Answer:

    shouldFailLoginWhenPasswordIsEmpty -> Option A
  4. Quick Check:

    Clear, descriptive, camelCase with 'should' = shouldFailLoginWhenPasswordIsEmpty [OK]
Hint: Use 'should' + action + condition for clear test names [OK]
Common Mistakes:
  • Using unclear or mixed naming styles
  • Omitting 'should' for behavior clarity
  • Using underscores or inconsistent casing
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