Comparing Arrays in Junit Assertions, Concise Built-In Way

Comparing arrays in JUnit assertions, concise built-in way?

Use org.junit.Assert's method assertArrayEquals:

import org.junit.Assert;
...

Assert.assertArrayEquals( expectedResult, result );

If this method is not available, you may have accidentally imported the Assert class from junit.framework.

JUnit - Comparing Arrays of objects

You might want to use assertj for that. Especially its usingFieldByFieldElementComparator

import java.util.Arrays;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

class MyTests {
static class Rule {
final String id;
final String name;
final String description;

Rule(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}

// no equals, no hashCode
}

@Test
void rulesShouldEqual_whenTheirPropertiesEqual() {
Rule actualRule1 = new Rule("id1", "name1", "description1");
Rule actualRule2 = new Rule("id2", "name2", "description2");
List<Rule> actual = Arrays.asList(actualRule1, actualRule2);

Assertions.assertThat(actual).usingFieldByFieldElementComparator().containsExactly(
new Rule("id1", "name1", "description1"),
new Rule("id2", "name2", "description2"));
}
}

How can I check two Object-Arrays for Equality in JUnit?

assertThat(ob1.getProperties(), 
IsArrayContainingInOrder.contains(obj2.getProperties));

This is using a Hamcrest Matcher which I believe to be a preferable method to doing asserts since the output on failure is much more descriptive.

There is also an IsArrayContainingInAnyOrder if order does not matter.

IsArrayContainingInAnyOrder

JUnit - Test array results

You can use AssertJ and use either containsExactly() or containsExactlyInAnyOrder(). For example:

String[] expected = { "ABC", "123" };
String[] actual = { "ABC", "234" };
assertThat(actual).containsExactly(expected);

will produce an error:

java.lang.AssertionError: 
Expecting:
<["ABC", "234"]>
to contain exactly (and in same order):
<["ABC", "123"]>
but some elements were not found:
<["123"]>
and others were not expected:
<["234"]>

JUnit assertArrayEquals

If you are using JUnit4, you could use the org.junit.Assert class:

Assert.assertArrayEquals(new int[]{0,1,3,5,7}, someMethod(10));

is this a good way to call Junit assertions in a common method?

This question doesn't have an exact answer (IMO), so it will probably be deleted soon.

I wouldn't do it for simple cases like your example but if it makes your life easier, I don't see anything wrong with it. You could rename the method as assertMissingNumber and it wouldn't be that much different than usual (although the method you really want to test is in the assertion call).

I think your specific example is perfect for parameterized tests, though.

This is what it would look like with Junit 4:

@RunWith( Parameterized.class )
public class MissingNumberInArrayTest {

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList( new Object[][] {
{ 5, new int[]{1, 2, 3, 4}, 5},
{ 5, new int[]{2, 3, 4, 5}, 1}
});
}

@Parameterized.Parameter(0)
public int arrElements;

@Parameterized.Parameter(1)
public int[] arr;

@Parameterized.Parameter(2)
public int expected;

@Test
public void mustReturnMissingNumberCase() {
// When
int missingNumber = MissingNumberInArray.findMissingNumber(arr, arrElements);

// Then
assertEquals( expected, missingNumber);
}
}

You can check on the JUnit documentation for examples with JUnit 5

Comparing two objects with JUnit shows strange behaviour

assertEquals(Object, Object) method uses equals() of given objects to check equality. Since you have not overridden equals() in AbsorptionScheme class, equals() implementation of java.lang.Object is used by assertEquals().

Object.equals() returns true only when the two objects are the same (same object reference). See below text from Javadoc of Object.equals().

The equals method for class Object implements the most discriminating
possible equivalence relation on objects; that is, for any non-null
reference values x and y, this method returns true if and only if x
and y refer to the same object (x == y has the value true).

In your testcase, absorptionSchemeActual and absorptionSchemeExpected are two difference objects. Hence assertEquals() fails.



Related Topics



Leave a reply



Submit