Bird
Raised Fist0
JUnittesting~10 mins

Test result publishing in JUnit - Interactive Code Practice

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

Complete the code to publish the test result using JUnit's assertion.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {
    @Test
    public void testAddition() {
        int result = 2 + 3;
        assertEquals([1], result);
    }
}
Drag options to blanks, or click blank then click option'
A7
B4
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual result as the expected value.
Mixing up the order of expected and actual in assertEquals.
2fill in blank
medium

Complete the code to publish a test failure when the condition is false.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class NumberTest {
    @Test
    public void testIsPositive() {
        int number = -1;
        assertTrue([1]);
    }
}
Drag options to blanks, or click blank then click option'
Anumber < 0
Bnumber > 0
Cnumber == 0
Dnumber >= 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that is false for the given number.
Confusing greater than and less than operators.
3fill in blank
hard

Fix the error in the test result publishing code by completing the assertion.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class StringTest {
    @Test
    public void testStringLength() {
        String text = "hello";
        assertEquals([1], text.length());
    }
}
Drag options to blanks, or click blank then click option'
A5
B6
C4
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual length as the expected value.
Off-by-one errors in counting string length.
4fill in blank
hard

Fill both blanks to publish test results for checking if a list contains a specific element.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;

public class ListTest {
    @Test
    public void testContainsElement() {
        List<String> fruits = List.of("apple", "banana", "cherry");
        assertTrue(fruits.[1]([2]));
    }
}
Drag options to blanks, or click blank then click option'
Acontains
B"banana"
C"orange"
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that modifies the list instead of checking.
Checking for an element not in the list.
5fill in blank
hard

Fill all three blanks to publish a test result that verifies a map contains a key with a specific value.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Map;

public class MapTest {
    @Test
    public void testMapEntry() {
        Map<String, Integer> scores = Map.of("Alice", 90, "Bob", 85);
        assertEquals([1], scores.get([2]));
        assertTrue(scores.[3]([2]));
    }
}
Drag options to blanks, or click blank then click option'
A90
B"Alice"
CcontainsKey
D85
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values in the map.
Using a method that checks values instead of keys.

Practice

(1/5)
1. What does test result publishing in JUnit primarily show?
easy
A. Whether tests pass or fail
B. The source code of the tests
C. The time taken to write tests
D. The number of test files in the project

Solution

  1. Step 1: Understand the purpose of test result publishing

    Test result publishing shows the outcome of running tests, mainly if they pass or fail.
  2. Step 2: Identify what is displayed after tests run

    JUnit displays test results to inform developers about test success or failure.
  3. Final Answer:

    Whether tests pass or fail -> Option A
  4. Quick Check:

    Test result publishing = pass/fail status [OK]
Hint: Test results show pass or fail status [OK]
Common Mistakes:
  • Confusing test results with source code
  • Thinking test results show test writing time
  • Assuming test results count files
2. Which JUnit annotation is used to mark a method as a test so its result can be published?
easy
A. @Test
B. @AfterEach
C. @BeforeAll
D. @Ignore

Solution

  1. Step 1: Identify the annotation that marks test methods

    The @Test annotation tells JUnit which methods to run as tests and publish results for.
  2. Step 2: Differentiate from other lifecycle annotations

    @BeforeAll and @AfterEach are for setup/cleanup, @Ignore skips tests, so they don't mark tests themselves.
  3. Final Answer:

    @Test -> Option A
  4. Quick Check:

    Test method annotation = @Test [OK]
Hint: Use @Test to mark test methods [OK]
Common Mistakes:
  • Using @BeforeAll or @AfterEach as test markers
  • Confusing @Ignore with test execution
  • Missing @Test annotation on test methods
3. Given this JUnit test method, what will the test result publishing show?
  @Test
  void testSum() {
    assertEquals(5, 2 + 3);
  }
medium
A. Test fails because 2 + 3 is not 5
B. Test throws an error due to syntax
C. Test passes because 2 + 3 equals 5
D. Test is ignored due to missing annotation

Solution

  1. Step 1: Evaluate the assertion in the test method

    The assertion checks if 2 + 3 equals 5, which is true.
  2. Step 2: Determine the test outcome based on assertion

    Since the assertion is true, the test passes and JUnit publishes a pass result.
  3. Final Answer:

    Test passes because 2 + 3 equals 5 -> Option C
  4. Quick Check:

    2 + 3 = 5 means test passes [OK]
Hint: Check if assertion condition is true for pass [OK]
Common Mistakes:
  • Assuming 2 + 3 is not 5
  • Thinking missing annotation causes ignore here
  • Confusing syntax errors with assertion failures
4. This JUnit test method is supposed to check if a list is empty, but the test result publishing shows failure. What is the error?
  @Test
  void testEmptyList() {
    List<String> list = new ArrayList<>();
    assertTrue(list.isEmpty());
  }
medium
A. assertTrue is used incorrectly; should be assertFalse
B. No error; test should pass and publish success
C. The list is not initialized properly
D. Missing import for List causes failure

Solution

  1. Step 1: Analyze the list initialization

    The list is created as a new empty ArrayList, so list.isEmpty() returns true.
  2. Step 2: Check the assertion logic

    assertTrue(list.isEmpty()) expects true, which matches the empty list state, so the test passes.
  3. Final Answer:

    No error; test should pass and publish success -> Option B
  4. Quick Check:

    Empty list means assertTrue(list.isEmpty()) passes [OK]
Hint: Empty list means isEmpty() returns true [OK]
Common Mistakes:
  • Thinking list is not empty after initialization
  • Confusing assertTrue with assertFalse
  • Assuming missing imports cause test failure without error
5. You want to publish test results in a JUnit project and generate a report file automatically after tests run. Which approach is best?
hard
A. Add @Report annotation to test classes to auto-generate reports
B. Manually write test results to a file inside each test method
C. Print test results to console and copy them manually to a file
D. Use a build tool like Maven or Gradle with JUnit plugins to generate reports

Solution

  1. Step 1: Understand automated test result publishing

    Build tools like Maven or Gradle integrate with JUnit to run tests and generate reports automatically.
  2. Step 2: Evaluate other options for report generation

    Manually writing or copying results is error-prone and inefficient; @Report annotation does not exist in JUnit.
  3. Final Answer:

    Use a build tool like Maven or Gradle with JUnit plugins to generate reports -> Option D
  4. Quick Check:

    Automate reports with build tools [OK]
Hint: Use Maven/Gradle plugins for automatic reports [OK]
Common Mistakes:
  • Trying to write results manually inside tests
  • Copying console output instead of automating
  • Assuming non-existent annotations generate reports
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