0
0
JUnittesting~20 mins

assertEquals in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.