Friday, 29 June 2018

Junit: Asserting equality, not equality checks

JUnit provides below methods to check the equality/inequality of objects.



JUnit provides below methods to check the equality of objects.

static public void assertEquals(String message, Object expected, Object actual)
Asserts that two objects are equal. If expected and actual are null, they are considered equal. If the objects are not equal, then junit fails the test case with given message.

static public void assertEquals(Object expected, Object actual)
Asserts that two objects are equal. If expected and actual are null, they are considered equal. If the objects are not equal, then junit fails the test case without message.

static public void assertNotEquals(String message, Object expected, Object actual)
Asserts that two objects are not equal. If expected and actual are null, they are considered equal. If the objects are equal, then junit fails the test case with given message.

static public void assertNotEquals(Object unexpected, Object actual)
Asserts that two objects are not equal. If expected and actual are null, they are considered equal. If the objects are equal, then junit fails the test case without message.

static public void assertNotEquals(String message, long expected, long actual)
Assert that two longs are not equal. If the expected and actual are equal then junit fails the test case with given message.

static public void assertNotEquals(long unexpected, long actual)
Assert that two longs are not equal. If the expected and actual are equal then junit fails the test case without message.

static public void assertEquals(long expected, long actual)
Assert that two longs are equal. If the expected and actual are not equal then junit fails the test case without message.

static public void assertEquals(String message, long expected, long actual)
Assert that two longs are equal. If the expected and actual are not equal then junit fails the test case with given message.

static public void assertNotEquals(String message, double expected, double actual, double delta)
Asserts that two doubles are not equal to within a positive delta. If the expected and actual are equal within a positive delta, then junit fails the test case with given message.

static public void assertNotEquals(double unexpected, double actual, double delta)
Asserts that two doubles are not equal to within a positive delta. If the expected and actual are equal within a positive delta, then junit fails the test case without message.

static public void assertEquals(double expected, double actual, double delta)
Asserts that two doubles are equal within a positive delta. If the expected and actual are not equal within a positive delta, then junit fails the test case without message.


static public void assertEquals(String message, double expected, double actual, double delta)
Asserts that two doubles are equal within a positive delta. If the expected and actual are not equal within a positive delta, then junit fails the test case without message.

static public void assertNotEquals(float unexpected, float actual, float delta)
Asserts that two floats are not equal to within a positive delta. If the expected and actual are equal within a positive delta, then junit fails the test case without message.

static public void assertEquals(float expected, float actual, float delta)
Asserts that two floats are equal within a positive delta. If the expected and actual are not equal within a positive delta, then junit fails the test case without message.

static public void assertNotEquals(String message, float unexpected, float actual, float delta)
Asserts that two floats are not equal to within a positive delta. If the expected and actual are not equal within a positive delta, then junit fails the test case with given message.

static public void assertEquals(String message, float expected, float actual, float delta)
Asserts that two floats are equal within a positive delta. If the expected and actual are equal within a positive delta, then junit fails the test case without message.

public static void assertArrayEquals(String message, Object[] expecteds, Object[] actuals)
Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null, they are considered equal.

public static void assertArrayEquals(Object[] expecteds, Object[] actuals)
Asserts that two object arrays are equal. If they are not, an AssertionError is thrown without the message. If expecteds and actuals are null, they are considered equal.

public static void assertArrayEquals(String message, boolean[] expecteds, boolean[] actuals)
Asserts that two boolean arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(boolean[] expecteds, boolean[] actuals)
Asserts that two boolean arrays are equal. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, byte[] expecteds, byte[] actuals)
Asserts that two byte arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(byte[] expecteds, byte[] actuals)
Asserts that two byte arrays are equal. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, char[] expecteds, char[] actuals)
Asserts that two char arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(char[] expecteds, char[] actuals)
Asserts that two char arrays are equal. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, short[] expecteds, short[] actuals)
Asserts that two short arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(short[] expecteds, short[] actuals)
Asserts that two short arrays are equal. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, int[] expecteds, int[] actuals)
Asserts that two int arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(int[] expecteds, int[] actuals)
Asserts that two int arrays are equal. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, long[] expecteds, long[] actuals)
Asserts that two long arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(long[] expecteds, long[] actuals)
Asserts that two long arrays are equal. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, double[] expecteds, double[] actuals, double delta)
Asserts that two double arrays are equal within given delta. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(double[] expecteds, double[] actuals, double delta)
Asserts that two double arrays are equal within given delta. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(String message, float[] expecteds, float[] actuals, float delta)
Asserts that two float arrays are equal within given delta. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null they are considered equal.

public static void assertArrayEquals(float[] expecteds, float[] actuals, float delta)
Asserts that two float arrays are equal within given delta. If they are not, an AssertionError is thrown without message. If expecteds and actuals are null they are considered equal.

Find the below working application.

DemoTestApp.java
package com.sample.arithmetic;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import org.junit.Test;

public class DemoTestApp {

 private static class Employee {
  private int id;
  private String firstName;
  private String lastName;

  public Employee(int id, String firstName, String lastName) {
   this.id = id;
   this.firstName = firstName;
   this.lastName = lastName;
  }

  @Override
  public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
   result = prime * result + id;
   result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
   return result;
  }

  @Override
  public boolean equals(Object obj) {
   if (this == obj)
    return true;
   if (obj == null)
    return false;
   if (getClass() != obj.getClass())
    return false;
   Employee other = (Employee) obj;
   if (firstName == null) {
    if (other.firstName != null)
     return false;
   } else if (!firstName.equals(other.firstName))
    return false;
   if (id != other.id)
    return false;
   if (lastName == null) {
    if (other.lastName != null)
     return false;
   } else if (!lastName.equals(other.lastName))
    return false;
   return true;
  }

 }

 @Test
 public void testSssertEqualsWithMessage() {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(1, "Krishna", "Gurram");

  assertEquals("emp1 and emp2 are not equal", emp1, emp2);
 }

 @Test
 public void testAssertEqualsWithOutMessage() {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(1, "Krishna", "Gurram");

  assertEquals(emp1, emp2);
 }

 @Test
 public void testAssertNotEqualsWithMessage() {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Krishna", "Gurram");

  assertNotEquals("emp1 and emp2 are not equal", emp1, emp2);
 }

 @Test
 public void testAssertNotEqualsWithOutMessage() {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Krishna", "Gurram");

  assertNotEquals(emp1, emp2);
 }

 @Test
 public void testAssertNotEqualsLongWithMessage() {
  long var1 = 10;
  long var2 = 11;

  assertNotEquals("var1 and var2 are equal", var1, var2);
 }

 @Test
 public void testAssertNotEqualsLongWithOutMessage() {
  long var1 = 10;
  long var2 = 11;

  assertNotEquals(var1, var2);
 }

 @Test
 public void testAssertEqualsLongWithMessage() {
  long var1 = 10;
  long var2 = 10;

  assertEquals("var1 and var2 are not equal", var1, var2);
 }

 @Test
 public void testAssertEqualsLongWithOutMessage() {
  long var1 = 10;
  long var2 = 10;

  assertEquals(var1, var2);
 }

 @Test
 public void testAssertNotEqualsDoubleWithMessage() {
  double var1 = 10;
  double var2 = 11;

  assertNotEquals("var1 and var2 are equal", var1, var2);
 }

 @Test
 public void testAssertNotEqualsDoubleWithOutMessage() {
  double var1 = 10;
  double var2 = 11;

  assertNotEquals(var1, var2);
 }

 @Test
 public void testAssertEqualsDoubleWithMessage() {
  double var1 = 10;
  double var2 = 10;
  double delta = 0.01;

  assertEquals("var1 and var2 are not equal", var1, var2, delta);
 }

 @Test
 public void testAssertEqualsDoubleWithOutMessage() {
  double var1 = 10;
  double var2 = 10;
  double delta = 0.01;

  assertEquals(var1, var2, delta);
 }

 @Test
 public void testAssertNotEqualsFloatWithMessage() {
  float var1 = 10;
  float var2 = 11;

  assertNotEquals("var1 and var2 are equal", var1, var2);
 }

 @Test
 public void testAssertNotEqualsFloatWithOutMessage() {
  float var1 = 10;
  float var2 = 11;

  assertNotEquals(var1, var2);
 }

 @Test
 public void testAssertEqualsFloatWithMessage() {
  float var1 = 10;
  float var2 = 10;
  float delta = 0.01f;

  assertEquals("var1 and var2 are not equal", var1, var2, delta);
 }

 @Test
 public void testAssertEqualsFloatWithOutMessage() {
  float var1 = 10;
  float var2 = 10;
  float delta = 0.01f;

  assertEquals(var1, var2, delta);
 }

 @Test
 public void testObjectArrayEqualsWithMessage() {
  Employee[] emps1 = new Employee[2];
  Employee[] emps2 = new Employee[2];

  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(1, "Krishna", "Gurram");

  emps1[0] = emp1;
  emps1[1] = emp2;

  emps2[0] = emp1;
  emps2[1] = emp2;

  assertArrayEquals("emps1 and emps2 are not equal", emps1, emps2);

 }

 @Test
 public void testObjectArrayEqualsWithOutMessage() {
  Employee[] emps1 = new Employee[2];
  Employee[] emps2 = new Employee[2];

  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(1, "Krishna", "Gurram");

  emps1[0] = emp1;
  emps1[1] = emp2;

  emps2[0] = emp1;
  emps2[1] = emp2;

  assertArrayEquals(emps1, emps2);

 }

 @Test
 public void testBooleanArrayEqualsWithMessage() {
  boolean arr1[] = { true, false, true };
  boolean arr2[] = { true, false, true };

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2);
 }

 @Test
 public void testBooleanArrayEqualsWithOutMessage() {
  boolean arr1[] = { true, false, true };
  boolean arr2[] = { true, false, true };

  assertArrayEquals(arr1, arr2);
 }

 @Test
 public void testByteArrayEqualsWithMessage() {
  byte arr1[] = { 2, 5, 7 };
  byte arr2[] = { 2, 5, 7 };

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2);
 }

 @Test
 public void testByteArrayEqualsWithOutMessage() {
  byte arr1[] = { 2, 5, 7 };
  byte arr2[] = { 2, 5, 7 };

  assertArrayEquals(arr1, arr2);
 }

 @Test
 public void testCharArrayEqualsWithMessage() {
  char arr1[] = { 'a', 'e', 'i' };
  char arr2[] = { 'a', 'e', 'i' };

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2);
 }

 @Test
 public void testCharArrayEqualsWithOutMessage() {
  char arr1[] = { 'a', 'e', 'i' };
  char arr2[] = { 'a', 'e', 'i' };

  assertArrayEquals(arr1, arr2);
 }

 @Test
 public void testShortArrayEqualsWithMessage() {
  short arr1[] = { 2, 5, 7 };
  short arr2[] = { 2, 5, 7 };

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2);
 }

 @Test
 public void testShortArrayEqualsWithOutMessage() {
  short arr1[] = { 2, 5, 7 };
  short arr2[] = { 2, 5, 7 };

  assertArrayEquals(arr1, arr2);
 }

 @Test
 public void testIntArrayEqualsWithMessage() {
  int arr1[] = { 2, 5, 7 };
  int arr2[] = { 2, 5, 7 };

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2);
 }

 @Test
 public void testIntArrayEqualsWithOutMessage() {
  int arr1[] = { 2, 5, 7 };
  int arr2[] = { 2, 5, 7 };

  assertArrayEquals(arr1, arr2);
 }

 @Test
 public void testLongArrayEqualsWithMessage() {
  long arr1[] = { 2, 5, 7 };
  long arr2[] = { 2, 5, 7 };

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2);
 }

 @Test
 public void testLongArrayEqualsWithOutMessage() {
  long arr1[] = { 2, 5, 7 };
  long arr2[] = { 2, 5, 7 };

  assertArrayEquals(arr1, arr2);
 }

 @Test
 public void testDoubleArrayEqualsWithMessage() {
  double arr1[] = { 2, 5, 7 };
  double arr2[] = { 2, 5, 7 };
  double delta = 0.01;

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2, delta);
 }

 @Test
 public void testDoubleArrayEqualsWithOutMessage() {
  double arr1[] = { 2, 5, 7 };
  double arr2[] = { 2, 5, 7 };
  double delta = 0.01;

  assertArrayEquals(arr1, arr2, delta);
 }

 @Test
 public void testFloatArrayEqualsWithMessage() {
  float arr1[] = { 2, 5, 7 };
  float arr2[] = { 2, 5, 7 };
  float delta = 0.01f;

  assertArrayEquals("arr1 and arr2 are not equal", arr1, arr2, delta);
 }

 @Test
 public void testFloatArrayEqualsWithOutMessage() {
  float arr1[] = { 2, 5, 7 };
  float arr2[] = { 2, 5, 7 };
  float delta = 0.01f;

  assertArrayEquals(arr1, arr2, delta);
 }

}




Previous                                                 Next                                                 Home

No comments:

Post a Comment