Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hamcrest matcher to compare two arrays

I have a method that do sort an array, and I want to test it, I want to compare its result with the expected one, of course that I can do it using a for loop, but i'm asking if there is a Hamcrest matcher to do the comparison

I have a class

class Person{
 String name;
 int age;
 double budget;
 Person(String name,int age,double budget){
  this.name = name;
  this.age = age;
  this.budget = budget;
 }
 @Override
 public boolean equals(Object obj) {
   if (obj == null || !(obj instanceof Person)) return false;
   Person p = (Person) obj;
   if (((Person) obj).budget == budget && ((Person) obj).age == age && ((Person) obj).name.equals(name)) {
    return true;
   }
   return false;
 }
}

and my test method is like this

@Test
public void InsertionSortOfObjectUsingComparator() {
    Person p1 = new Person("A", 18, 800);
    Person p2 = new Person("K", 15, 1800);
    Person p3 = new Person("L", 18, 600);
    Person[] persons = {p1,p2,p3};
    Person[] expected = {p3, p1, p2};
    Person[] result = (new Sort()).sort(persons, Comparator.<Person>comparingDouble(o-> o.budget);
    //want to compare the content of the two arrays result and expected; using assertThat
}

Is it possible doing it using Hamcrest ? if yes, how ?

UPDATE

yes it is possible, using IsArrayContainingInOrder.arrayContaining

    ....
    assertThat(expected, IsArrayContainingInOrder.arrayContaining((new InsertionSort().sort(persons, Comparator.comparingDouble(o -> o.budget)))));
    assertThat(3,is((new InsertionSort().sort(persons, Comparator.comparingDouble(o -> o.budget))).length));
}
like image 683
octopus Avatar asked Jan 30 '19 11:01

octopus


People also ask

Is Hamcrest a matcher?

Hamcrest is typically viewed as a third generation matcher framework. The first generation used assert(logical statement) but such tests were not easily readable. The second generation introduced special methods for assertions, e.g., assertEquals() .

How do you use Hamcrest matchers?

Hamcrest can also be used with mock objects frameworks by using adaptors to bridge from the mock objects framework's concept of a matcher to a Hamcrest matcher. For example, JMock 1's constraints are Hamcrest's matchers. Hamcrest provides a JMock 1 adaptor to allow you to use Hamcrest matchers in your JMock 1 tests.

What is Hamcrest in testing?

Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.


1 Answers

The arrays can be matched with the simplest is matcher, e.g.:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

// ...

assertThat(result, is(new byte[]{1, 2, 3}));

Under the hood it will figure out that the input is an array. It will use the appropriate matcher for arrays (i.e. not just a.equal(b)).

like image 83
Pavel Avatar answered Sep 27 '22 17:09

Pavel