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

  • Home
  • SEARCH
  • 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 6925603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:48:39+00:00 2026-05-27T10:48:39+00:00

Creating tests for an assignment, I’m getting an strange AssertionError exception. I have changed

  • 0

Creating tests for an assignment, I’m getting an strange AssertionError exception.

I have changed it until i got to a simple case:

List<Integer> elements= new ArrayList<Integer>();
elements.add(1);
elements.add(2);
elements.add(3);

Permutation p2 = new Permutation(elements);
Permutation p1 = new Permutation(elements);

assertThat(p2, equalTo(p1));

Permutation.java:

public class Permutation {

  private List<Integer> elements;

  public Permutation(List<Integer> elements) {
    this.elements = elements;
  }

public boolean equals(Permutacion permutation){
  if ( this.elements.size() != permutation.elements.size()){
    return false;
  }

  Iterator<Integer> iterThis = this.elements.iterator();
  Iterator<Integer> iterOther = permutation.elements.iterator();
  while ( iterThis.hasNext() && iterOther.hasNext()){
    if ( iterThis.next() != iterOther.next()){
      return false;
    }
  }

  return true;

}

Digging in both junit and hamcrest source code I have found that junit assertThat only calls matches on the matcher.

The matches method in this case is:

public boolean matches(Object arg) {
    return areEqual(arg, object);
}

private static boolean areEqual(Object o1, Object o2) {
    if (o1 == null) {
        return o2 == null;
    } else if (o2 != null && isArray(o1)) {
        return isArray(o2) && areArraysEqual(o1, o2);
    } else {
        return o1.equals(o2);
    }
}

Where arg should be “p2” and object should be “p1”.

(It can be browsed at Hamcrest repository)

The results of the comparisons in areEqual method, using debugger inspection, are:

"p2 == null"                    false   
"p1 != null"                    true    
"p2.getClass().isArray()"       false   
"p2.equals(p1)"                 true    
"equalTo(p1).matches(p2)"       false   

So as you can see, the code should reach the last else condition and return true ( p2.equals(p1)), but equalTo(p1).matches(p2) returns false

Thanks for your help

  • 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-05-27T10:48:39+00:00Added an answer on May 27, 2026 at 10:48 am

    Why would you expect p2.equals(p1) to return true? You haven’t overridden equals in the Permutation class, so it will use reference identity by default. You need to override equals (and hashCode, in general, to match equals) to indicate what you mean by equality when it comes to permutations.

    EDIT: Now you’ve posted more code, it’s clearer what’s going on. Your equals method has this signature:

    public boolean equals(Permutacion permutation){
    

    That doesn’t override Object.equals, which is what the matcher will be using. Instead, it overloads it – introducing a new method, which is what is being called in the debugger inspection. If you write:

    Object o1 = p1;
    

    then p2.equals(o1) will show false in the debugger too – and that’s effectively what the matcher is doing. Your equals method should look something like:

    @Override
    public boolean equals(Object other)
    {
        if (other == null || other.getClass() != this.getClass())
        {
            return false;
        }
        Permutation otherPermutation = (Permutation) other;
    
        // List.equals should do the right thing here
        return elements.equals(otherPermutation.elements);
    }
    

    (You should also override hashCode in a way which corresponds to this.)

    Additionally:

    • Either consider the case where elements is null, or validate it in the constructor
    • Equality is simplest to define in final classes
    • As you’re not making a defensive copy of the list, it’s possible for it to be mutated by the caller after construction; that could lead to problems if you ever use the permutation as a key in a map, for example.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been creating Unit tests like crazy and find that I'm often having
We have some unit tests which work with performance counters (specifically creating new categories)
Does anyone have any frameworks/apps/methodologies for creating Unit tests with Oracle?. I'm using Oracle
Is there a simple way of creating stubs for Junit4 tests in Eclipse (Galileo)?
I've just started creating Unit Tests for my code again. I have had PHPUnit
I am creating some tests that have a variety of inputs. I am testing
I'm working on creating a tests, and I can't figure out why the creation
I was curious if anyone had any problems creating unit tests around using the
I am creating a mock database for import export tests (of the algorithm reading
I am creating an automated testing framework in Perl for regression tests. I would

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.