Bird
Raised Fist0
JUnittesting~10 mins

First JUnit test - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the JUnit test annotation.

JUnit
import org.junit.jupiter.api.[1];
Drag options to blanks, or click blank then click option'
AAfterAll
BBeforeEach
CDisabled
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing other annotations like BeforeEach instead of Test.
Forgetting to import any annotation.
2fill in blank
medium

Complete the code to write a simple test method named testAddition.

JUnit
@Test
public void [1]() {
    int sum = 2 + 3;
    org.junit.jupiter.api.Assertions.assertEquals(5, sum);
}
Drag options to blanks, or click blank then click option'
AtestAddition
BcheckSum
CadditionTest
DsumTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than requested.
Forgetting the @Test annotation.
3fill in blank
hard

Fix the error in the assertion to correctly check that result equals 10.

JUnit
int result = 5 + 5;
org.junit.jupiter.api.Assertions.assertEquals([1], result);
Drag options to blanks, or click blank then click option'
A10
B5 + 5
C"10"
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable result as expected value.
Using a string "10" instead of integer 10.
4fill in blank
hard

Fill both blanks to create a test method that checks if multiply(2, 3) returns 6.

JUnit
@Test
public void [1]() {
    int output = multiply(2, 3);
    org.junit.jupiter.api.Assertions.[2](6, output);
}
Drag options to blanks, or click blank then click option'
AtestMultiply
BassertEquals
CassertTrue
DmultiplyTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue instead of assertEquals.
Using a wrong method name.
5fill in blank
hard

Fill all three blanks to write a test method that verifies isEven(4) returns true.

JUnit
@Test
public void [1]() {
    boolean result = isEven([2]);
    org.junit.jupiter.api.Assertions.[3](result);
}
Drag options to blanks, or click blank then click option'
AtestIsEven
B4
CassertEquals
DassertTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertEquals instead of assertTrue for boolean checks.
Passing wrong input value.

Practice

(1/5)
1. What is the main purpose of a JUnit test method marked with @Test?
easy
A. To check if a piece of code works as expected
B. To create a new Java class
C. To print output to the console
D. To declare a variable

Solution

  1. Step 1: Understand the role of @Test annotation

    The @Test annotation marks a method as a test method in JUnit, meaning it will be run to check code behavior.
  2. Step 2: Identify the purpose of test methods

    Test methods verify if the code produces the expected results, helping find bugs or confirm correctness.
  3. Final Answer:

    To check if a piece of code works as expected -> Option A
  4. Quick Check:

    JUnit @Test marks test methods [OK]
Hint: JUnit tests verify code correctness using @Test [OK]
Common Mistakes:
  • Thinking @Test creates classes
  • Confusing test methods with print statements
  • Using @Test to declare variables
2. Which of the following is the correct way to write a simple JUnit 5 test method?
easy
A. public void testMethod() { assertEquals(5, 2 + 3); }
B. void testMethod() { assertEquals(5, 2 + 3); }
C. @Test public void testMethod() { assertEquals(5, 2 + 3); }
D. @Test void testMethod() { System.out.println(5); }

Solution

  1. Step 1: Check for @Test annotation

    JUnit 5 requires the @Test annotation to mark a method as a test.
  2. Step 2: Verify method signature and assertion

    The method should be public void with an assertion like assertEquals(expected, actual). @Test public void testMethod() { assertEquals(5, 2 + 3); } matches this.
  3. Final Answer:

    @Test public void testMethod() { assertEquals(5, 2 + 3); } -> Option C
  4. Quick Check:

    JUnit test method needs @Test and assertion [OK]
Hint: Always use @Test before test methods [OK]
Common Mistakes:
  • Missing @Test annotation
  • Wrong method visibility or return type
  • Using print statements instead of assertions
3. What will be the result of running this JUnit test?
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class SampleTest {
  @Test
  void additionTest() {
    assertEquals(10, 5 + 5);
  }
}
medium
A. Runtime exception occurs
B. Test passes successfully
C. Compilation error due to missing import
D. Test fails with assertion error

Solution

  1. Step 1: Analyze the assertion

    The test asserts that 5 + 5 equals 10, which is true.
  2. Step 2: Check for syntax and imports

    All necessary imports and syntax are correct, so the test will run without errors.
  3. Final Answer:

    Test passes successfully -> Option B
  4. Quick Check:

    Correct assertion means test passes [OK]
Hint: True assertions make tests pass [OK]
Common Mistakes:
  • Assuming test fails when assertion is true
  • Missing static import for assertions
  • Confusing compilation errors with runtime errors
4. Identify the error in this JUnit test code:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalcTest {
  @Test
  public void testSum() {
    int result = 2 + 2;
    assertEquals(5, result);
  }
}
medium
A. The assertion expects wrong value causing test failure
B. Missing @Test annotation
C. Method should return int, not void
D. No import for assertEquals

Solution

  1. Step 1: Check the assertion values

    The test expects 5 but the actual result is 4 (2 + 2), so the assertion will fail.
  2. Step 2: Verify annotations and imports

    The @Test annotation and imports are correct, so no syntax errors exist.
  3. Final Answer:

    The assertion expects wrong value causing test failure -> Option A
  4. Quick Check:

    Assertion mismatch causes test failure [OK]
Hint: Check expected vs actual values in assertions [OK]
Common Mistakes:
  • Ignoring assertion expected value
  • Thinking missing annotation causes failure here
  • Confusing method return types in tests
5. You want to write a JUnit test that checks if a method multiply(int a, int b) returns 0 when one input is 0. Which test method is correct?
hard
A. @Test public void testMultiplyZero() { assertEquals(5, multiply(0, 5)); }
B. public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); }
C. @Test public int testMultiplyZero() { return multiply(0, 5); }
D. @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); }

Solution

  1. Step 1: Confirm @Test annotation and method signature

    The test method must be annotated with @Test and return void. @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } meets this.
  2. Step 2: Check assertion correctness

    The method should assert that multiplying by zero returns zero. @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } asserts assertEquals(0, multiply(0, 5)), which is correct.
  3. Final Answer:

    @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } -> Option D
  4. Quick Check:

    Use @Test, void method, correct assertion [OK]
Hint: Test methods need @Test, void, and correct assertions [OK]
Common Mistakes:
  • Omitting @Test annotation
  • Using wrong return type in test method
  • Asserting wrong expected value
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