JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to do JUnit test for comapring two list of user defined objects?


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. Incase of user defined objects, the objects should override equals() and hashCode() methods. Then assertArrayEquals() method called equals and hashCode methods to check the equality on each object.


package com.java2novice.junit.tests;

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

import com.java2novice.junit.samples.Employee;

public class MyArrayObjectEqualsTest {
	
	Object[] expectedEmps = new Object[3];
	
	@Before
	public void initInputs(){
		expectedEmps[0] = new Employee(1, "Nats", 15000);
		expectedEmps[1] = new Employee(2, "Kalid", 25000);
		expectedEmps[2] = new Employee(3, "Krish", 5000);
	}
	
	@Test
	public void compareEmployees(){
		/**
		 * convert List of objects to array of objects
		 */
		Object[] testOutput = Employee.getEmpList().toArray();
		assertArrayEquals(expectedEmps, testOutput);
	}
}

package com.java2novice.junit.samples;

import java.util.ArrayList;
import java.util.List;

public class Employee {

	private String name;
	private int empId;
	private int salary;
	
	public Employee(int id, String name, int sal){
		this.empId = id;
		this.name = name;
		this.salary = sal;
	}
	
	public boolean equals(Object obj){
		Employee emp = (Employee) obj;
		boolean status = false;
		if(this.name.equalsIgnoreCase(emp.name)
				&& this.empId == emp.empId 
				&& this.salary == emp.salary){
			status = true;
		}
		return status;
	}
	
	public static List<Employee> getEmpList(){
		List<Employee> emps = new ArrayList<Employee>();
		emps.add(new Employee(1, "Nats", 15000));
		emps.add(new Employee(2, "Kalid", 25000));
		emps.add(new Employee(3, "Krish", 5000));
		return emps;
	}
	
	public int hashCode(){
		return this.empId;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
}

<< 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
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

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].

Most Visited Pages

Other Interesting Sites

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