Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:21:54+00:00 2026-06-17T16:21:54+00:00

I am having a problem where some class X extends extends java.util.AbstractMap, and also

  • 0

I am having a problem where some class X extends extends java.util.AbstractMap, and also overloads equals(Object). Calling org.testng.Assert.assertEquals(X a, X b) resolves to assertEquals(Map<?,?>, Map<?,?>). Instead of calling the ‘equals’ method, the map entries are compared. This results in assertEquals(a, b) passing even though a.equals(b) is false.

This code demonstrates the problem:

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.Set;

import org.testng.annotations.Test;

public class AssertTest {
    static class X extends AbstractMap<String, Object> {
        private int i;

        public X(int i) {
        this.i = i;
        }

        @Override
        public Set<Entry<String, Object>> entrySet() {
        return Collections.EMPTY_SET;
        }

        @Override
        public boolean equals(Object o) {
        return o instanceof X && i == ((X)o).i;
        }

        @Override
        public int hashCode() {
        return i;
        }
    }

    @Test
    public void test() {
        X one = new X(1);
        X two = new X(2);
        assertEquals(one, two); // passes, should fail IMO
        assertTrue(one.equals(two)); // correctly fails
    }
}

What is the best way to work around this problem? One possibility is to remember to not use assertEquals to verify that instances of X match, but that is extremely error-prone.

Another possibility is to simply make a local copy of testng and rename the overloads. That creates an ongoing maintenance problem.

The only other idea I have is to create a project-specific Assert class that delegates to testng.Assert but renames the problematic overloads to ‘assertCollectionEquals’, ‘assertMapEquals’, etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:21:56+00:00Added an answer on June 17, 2026 at 4:21 pm

    org.testng.Assert.assertEquals(X a, X b) calls the assertEquals(Map, Map) method which iterates over the entry set and checks that all entries are equals, bypassing the Map#equals method, as you have noticed.

    A simple cast would avoid calling that method and would use the X#equals method instead:

    assertEquals((Object) one, (Object) two);
    

    You could also declare your variables as objects to achieve the same result:

    Object one = new X(1);
    Object two = new X(2);
    assertEquals(one, two);
    

    That does not realy solve your issue in the sense that it still is error prone.

    To avoid the occasional error, there are a few workarounds I can think of (getting tired here so some might make little sense), using the fact that assertEquals without cast calls x.entrySet():

    • write some code that parses all your test files for X class and make sure that there is a cast in place (not straightforward) or that no X is declared on the left of an =
    • use a mocking framework and mock X in a @BeforeGroups method (if you use groups) and put each test that relies on that idiom in the same group, so that they will use a mocked X where entrySet fails your tests (and the other methods work as expected), or at least logs/prints some warning. If you also need to use entrySet in the same method that won’t work
    • use a mocking framework to mock TestNG#assertEquals(Map, Map) to get the desired behaviour
    • in all the test classes that need that idiom, create an ad hoc assertEquals(Map, Map) method. If you have a static import of all assertEquals method you will have to reimplement the other signatures too 🙁
    • have all those test classes inherit a BaseXTestClass where you implement an assertEquals(Map, Map) method – same caveat
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm modeling a chess game on Java, and I'm having some problem. Here's what
I´m having some problems when i try to deserializing a Java class. My classes
I am having some problems generating a list for a class in Python. I
I'm having some problems when defining inner classes in a Test class inherited from
I'm having some problems with this piece of code. I've included a class declaration
I'm having some problem with validations for multiple fields, specifically with case-sensitive unique validations
i am having some problem with position: absolute div. IE7 displays it next to
I'm having some problem with this statement declare @result int select @result = (select
I am having some problem with jsonp and jquery. This is my code -
I'm having some problem consuming REST Service and want to figure out what I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.