Publish Your Article Here

Program: Assertion method Assert.assertEquals() example.

Java Class: org.junit.Assert

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

Assert.assertEquals() methods checks that the two objects are equals or not. If they are not, an AssertionError without a message is thrown. Incase if both expected and actual values are null, then this method returns equal. In the below example, the first Test (mySimpleEqualsTest()) compares two strings. The second test (myObjectEqualsTest()) we are comparing two different user defined objects. The assertEquals() method calls equals method on each object to check equality.

package com.java2novice.junit.tests;

import org.junit.Test;

import com.java2novice.junit.samples.Employee;

import static org.junit.Assert.*;

public class MyAssertEqualsTest {

	public void mySimpleEqualsTest(){
		String expectedName = "Nattu";
		assertEquals(expectedName, Employee.getEmpNameWithHighestSalary());
	public void myObjectEqualsTest(){
		Employee expectedEmpObj = new Employee(1, "Nattu", 15000);
		assertEquals(expectedEmpObj, Employee.getHighestPaidEmployee());

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; = name;
		this.salary = sal;
	public boolean equals(Object obj){
		Employee emp = (Employee) obj;
		boolean status = false;
				&& this.empId == emp.empId 
				&& this.salary == emp.salary){
			status = true;
		return status;
	public static String getEmpNameWithHighestSalary(){
		 * logic to get Highest paid employee
		return "Nattu";
	public static Employee getHighestPaidEmployee(){
		 * hiding logic to get highest paid employee
		return new Employee(1, "Nattu", 15000);
	public int hashCode(){
		return this.empId;
	public String getName() {
		return name;
	public void setName(String 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 >>
blog comments powered by Disqus

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
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
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 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.