Bird
Raised Fist0
JUnittesting~20 mins

assertEquals in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
AssertEquals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Understanding assertEquals with primitive types
What will be the result of this JUnit assertion?

assertEquals(5, 2 + 3);
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestClass {
    @org.junit.jupiter.api.Test
    void testSum() {
        assertEquals(5, 2 + 3);
    }
}
ATest fails because assertEquals expects strings
BTest passes because 2 + 3 equals 5
CTest fails due to a compilation error
DTest passes but with a warning about types
Attempts:
2 left
💡 Hint
assertEquals compares expected and actual values for equality.
❓ assertion
intermediate
2:00remaining
assertEquals with String objects
What will happen when this assertion runs?

assertEquals("hello", "he" + "llo");
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestClass {
    @org.junit.jupiter.api.Test
    void testStringConcat() {
        assertEquals("hello", "he" + "llo");
    }
}
ATest passes because the strings are equal in content
BTest fails because the strings are different objects
CTest fails due to a NullPointerException
DTest passes but only if using assertSame instead
Attempts:
2 left
💡 Hint
assertEquals compares string content, not object references.
❓ assertion
advanced
2:00remaining
assertEquals with floating point numbers and delta
What is the result of this assertion?

assertEquals(0.3, 0.1 + 0.2, 0.0001);
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestClass {
    @org.junit.jupiter.api.Test
    void testFloatingPoint() {
        assertEquals(0.3, 0.1 + 0.2, 0.0001);
    }
}
ATest passes because the difference is within the delta
BTest fails because floating point addition is exact
CTest fails due to a compilation error missing delta
DTest passes but delta should be zero for exact match
Attempts:
2 left
💡 Hint
Floating point arithmetic can have small precision errors; delta allows tolerance.
❓ assertion
advanced
2:00remaining
assertEquals with arrays
What will happen when this assertion runs?

assertEquals(new int[]{1, 2}, new int[]{1, 2});
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestClass {
    @org.junit.jupiter.api.Test
    void testArrayEquals() {
        assertEquals(new int[]{1, 2}, new int[]{1, 2});
    }
}
ATest passes only if using assertArrayEquals instead
BTest passes because arrays have the same contents
CTest fails due to NullPointerException
DTest fails because assertEquals compares array references, not contents
Attempts:
2 left
💡 Hint
assertEquals on arrays compares references, not contents.
❓ assertion
expert
3:00remaining
assertEquals with custom objects and equals method
Given this class:

public class Person {
    String name;
    public Person(String name) { this.name = name; }
}

What will happen when this assertion runs?

assertEquals(new Person("Alice"), new Person("Alice"));
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestClass {
    public static class Person {
        String name;
        public Person(String name) { this.name = name; }
    }

    @org.junit.jupiter.api.Test
    void testPersonEquals() {
        assertEquals(new Person("Alice"), new Person("Alice"));
    }
}
ATest passes because names are equal
BTest fails due to NullPointerException
CTest fails because Person does not override equals method
DTest passes only if Person overrides hashCode
Attempts:
2 left
💡 Hint
Default equals compares object references unless overridden.

Practice

(1/5)
1. What is the main purpose of assertEquals in JUnit testing?
easy
A. To check if two values are equal and fail the test if not
B. To print values during test execution
C. To skip a test case
D. To create a new test method

Solution

  1. Step 1: Understand the role of assertEquals

    assertEquals compares two values: expected and actual.
  2. Step 2: Identify the test behavior

    If the values differ, the test fails, helping find bugs.
  3. Final Answer:

    To check if two values are equal and fail the test if not -> Option A
  4. Quick Check:

    assertEquals checks equality [OK]
Hint: assertEquals fails test if values differ [OK]
Common Mistakes:
  • Thinking assertEquals prints output
  • Confusing assertEquals with skipping tests
  • Using assertEquals to create tests
2. Which of the following is the correct syntax for assertEquals with a message in JUnit?
easy
A. assertEquals("Error message", expected, actual);
B. assertEquals(actual, expected, "Error message");
C. assertEquals(expected, actual, "Error message");
D. assertEquals("Error message", actual, expected);

Solution

  1. Step 1: Recall assertEquals method signature with message

    The correct order is: message, expected, actual.
  2. Step 2: Match options with correct order

    assertEquals("Error message", expected, actual); matches this order exactly.
  3. Final Answer:

    assertEquals("Error message", expected, actual); -> Option A
  4. Quick Check:

    Message first, then expected, actual [OK]
Hint: Message comes first in assertEquals with message [OK]
Common Mistakes:
  • Swapping expected and actual parameters
  • Placing message last instead of first
  • Using actual before expected
3. What will happen when this JUnit test runs?
int expected = 5;
int actual = 3 + 2;
assertEquals(expected, actual);
medium
A. Compilation error due to wrong assertEquals usage
B. Test fails because expected does not equal actual
C. Test passes because expected equals actual
D. Test is skipped automatically

Solution

  1. Step 1: Calculate actual value

    3 + 2 equals 5, so actual is 5.
  2. Step 2: Compare expected and actual

    Both expected and actual are 5, so assertEquals passes.
  3. Final Answer:

    Test passes because expected equals actual -> Option C
  4. Quick Check:

    5 equals 5 means pass [OK]
Hint: Calculate actual before comparing [OK]
Common Mistakes:
  • Assuming 3 + 2 is not 5
  • Confusing expected and actual values
  • Thinking assertEquals causes compilation error here
4. Identify the error in this JUnit assertion:
assertEquals(5, "5");
medium
A. Runtime exception thrown
B. No error, test passes
C. Compilation error due to wrong parameter order
D. Test fails because int and String types differ

Solution

  1. Step 1: Check parameter types

    First is int 5, second is String "5".
  2. Step 2: Understand assertEquals behavior with different types

    assertEquals compares values and types; int and String differ, so test fails.
  3. Final Answer:

    Test fails because int and String types differ -> Option D
  4. Quick Check:

    Different types cause failure [OK]
Hint: Types must match for assertEquals to pass [OK]
Common Mistakes:
  • Assuming int 5 equals String "5"
  • Thinking parameter order is wrong here
  • Expecting compilation or runtime error
5. You want to test a method that returns a list of strings. Which assertEquals usage **including a custom error message** correctly checks if the returned list matches List.of("a", "b")?
List<String> expected = List.of("a", "b");
List<String> actual = myMethod();
hard
A. assertEquals(expected, actual);
B. assertEquals("Lists differ", expected, actual);
C. assertEquals(expected.size(), actual.size());
D. assertEquals(expected.toString(), actual.toString());

Solution

  1. Step 1: Understand assertEquals for lists

    assertEquals compares lists by content if types match.
  2. Step 2: Check options for best practice

    assertEquals("Lists differ", expected, actual); adds a helpful message and uses correct parameter order.
  3. Final Answer:

    assertEquals("Lists differ", expected, actual); -> Option B
  4. Quick Check:

    Message first, then expected and actual [OK]
Hint: Use message first with expected, actual for clarity [OK]
Common Mistakes:
  • Comparing string representations instead of lists
  • Only comparing list sizes
  • Omitting message for clarity
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