JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: Assertion method Assert.assertArrayEquals() example.


Java Class: org.junit.Assert

Assert class provides a set of assertion methods useful for writing tests.

assertArrayEquals() method checks that two object arrays are equal or not. If they are not, it throws an AssertionError with the given message. Incase if expected input and actual inputs are null, then they are considered to be equal. It checks whether both arrays are having same number of elements or not, and all elements should be same. It compares based on the order. If mismatch in order results in failure.


package com.java2novice.junit.tests;

import org.junit.Test;
import static org.junit.Assert.*;

public class MyAssertArrayEqualsTest {

	@Test
	public void myTestMethod(){
		/**
		 * we are demonstrating the usage of assertArrayEquals()
		 * method here, so we are preparing input data here itself.
		 * In real scenario, we will consider the methods returned 
		 * value which suppose to be test, as a input. 
		 */
		//assume that the below array represents expected result
		String[] expectedOutput = {"apple", "mango", "grape"};
		//assuem that the below array is returned from the method 
		//to be tested.
		String[] methodOutput = {"apple", "mango", "grape"};
		assertArrayEquals(expectedOutput, methodOutput);
	}
}

<< Previous Program | Next Program >>

Java JUnit Examples

  1. Simple JUnit test using @Test annotation.
  2. List of JUnit annotations.
  3. Assertion method Assert.assertArrayEquals() example.
  4. How to do JUnit test for comapring two list of user defined objects?
  5. Assertion method Assert.assertEquals() example.
  6. Assertion method Assert.assertFalse() example.
  7. Assertion method Assert.assertTrue() example.
  8. Assertion method Assert.assertNotNull() example.
  9. Assertion method Assert.assertNull() example.
  10. Assertion method Assert.assertNotSame() example.
  11. Assertion method Assert.assertSame() example.
Knowledge Centre
Difference between Enumeration and Iterator
The functionality of Enumeration and the Iterator are same. You can get remove() from Iterator to remove an element, while while Enumeration does not have remove() method. Using Enumeration you can only traverse and fetch the objects, where as using Iterator we can also add and remove the objects. So Iterator can be useful if you want to manipulate the list and Enumeration is for read-only access.
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected] or you can comment on the page.

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.