Thursday, 12 March 2020

TestNG: assertEquals: Assert equality

'assertEquals' method is used to check whether two elements are equal or not.

'assertEquals' method is available in below forms.
 public static void assertEquals(byte[] actual, byte[] expected)
 public static void assertEquals(byte[] actual, byte[] expected, String message)
 public static void assertEquals(short[] actual, short[] expected)
 public static void assertEquals(short[] actual, short[] expected, String message)
 public static void assertEquals(int[] actual, int[] expected)
 public static void assertEquals(int[] actual, int[] expected, String message)
 public static void assertEquals(boolean[] actual, boolean[] expected)
 public static void assertEquals(boolean[] actual, boolean[] expected, String message)
 public static void assertEquals(char[] actual, char[] expected)
 public static void assertEquals(char[] actual, char[] expected, String message)
 public static void assertEquals(float[] actual, float[] expected)
 public static void assertEquals(float[] actual, float[] expected, String message)
 public static void assertEquals(float[] actual, float[] expected, float delta)
 public static void assertEquals(float[] actual, float[] expected, float delta, String message)
 public static void assertEquals(double[] actual, double[] expected)
 public static void assertEquals(double[] actual, double[] expected, String message)
 public static void assertEquals(double[] actual, double[] expected, double delta)
 public static void assertEquals(double[] actual, double[] expected, double delta, String message)
 public static void assertEquals(long[] actual, long[] expected)
 public static void assertEquals(long[] actual, long[] expected, String message)
 public static void assertEquals(Object actual, Object expected)
 public static void assertEquals(String actual, String expected, String message)
 public static void assertEquals(String actual, String expected)
 public static void assertEquals(double actual, double expected, double delta, String message)
 public static void assertEquals(double actual, double expected, double delta)
 public static void assertEquals(double actual, double expected, String message)
 public static void assertEquals(double actual, double expected) 
 public static void assertEquals(float actual, float expected, float delta, String message)
 public static void assertEquals(float actual, float expected, float delta)
 public static void assertEquals(float actual, float expected, String message)
 public static void assertEquals(float actual, float expected)
 public static void assertEquals(long actual, long expected, String message)
 public static void assertEquals(long actual, long expected)
 public static void assertEquals(boolean actual, boolean expected, String message)
 public static void assertEquals(boolean actual, boolean expected)
 public static void assertEquals(byte actual, byte expected, String message)
 public static void assertEquals(byte actual, byte expected)
 public static void assertEquals(char actual, char expected, String message)
 public static void assertEquals(char actual, char expected)
 public static void assertEquals(short actual, short expected, String message)
 public static void assertEquals(short actual, short expected)
 public static void assertEquals(int actual, int expected, String message)
 public static void assertEquals(int actual, int expected)
 public static void assertEquals(Collection<?> actual, Collection<?> expected)
 public static void assertEquals(Collection<?> actual, Collection<?> expected, String message)
 public static void assertEquals(Iterator<?> actual, Iterator<?> expected)
 public static void assertEquals(Iterator<?> actual, Iterator<?> expected, String message)
 public static void assertEquals(Iterable<?> actual, Iterable<?> expected)
 public static void assertEquals(Iterable<?> actual, Iterable<?> expected, String message)
 public static void assertEquals(Object[] actual, Object[] expected, String message)
 public static void assertEqualsNoOrder(Object[] actual, Object[] expected, String message)
 public static void assertEquals(Object[] actual, Object[] expected)
 public static void assertEquals(Set<?> actual, Set<?> expected)
 public static void assertEquals(Set<?> actual, Set<?> expected, String message)
 public static void assertEquals(Map<?, ?> actual, Map<?, ?> expected)
 public static void assertEquals(Map<?, ?> actual, Map<?, ?> expected, String message)

Calculator.java
package com.sample.app.arithmetic;

public class Calculator {
 public int add(int a, int b) {
  return a + b;
 }

 public int subtract(int a, int b) {
  return a - b;
 }

 public int mul(int a, int b) {
  return a * b;
 }

 public int div(int a, int b) {
  return a / b;
 }

 public int remainder(int a, int b) {
  return a % b;
 }

 public boolean isEven(int number) {
  return number % 2 == 0;
 }

 public boolean isOdd(int number) {
  return number % 2 != 0;
 }
}

AssertEqualsTest.java
package com.sample.app.assertions;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import com.sample.app.arithmetic.Calculator;

public class AssertEqualsTest {

 private Calculator calc = new Calculator();

 @Test
 public void add_1And2_3() {
  assertEquals(calc.add(1, 2), 3);
 }

 @Test
 public void failMe() {
  assertEquals(true, false, "Failing the test case");
 }
}

Run AssertEqualsTest.java, you will see below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
PASSED: add_1And2_3
FAILED: failMe
java.lang.AssertionError: Failing the test case expected [false] but found [true]
 at org.testng.Assert.fail(Assert.java:97)
 at org.testng.Assert.assertEqualsImpl(Assert.java:136)
 at org.testng.Assert.assertEquals(Assert.java:118)
 at org.testng.Assert.assertEquals(Assert.java:751)
 at com.sample.app.assertions.AssertEqualsTest.failMe(AssertEqualsTest.java:20)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
 at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
 at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
 at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
 at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================



Previous                                                    Next                                                    Home

No comments:

Post a Comment