Home » Core Java » junit » JUnit Assertions Example

About Chryssa Aliferi

Chryssa Aliferi
Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.

JUnit Assertions Example

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.

The JUnit testing is done through assertions. The tests include assertions which are conditions that should be always correct. If an assertion fails, then it is assumed that fails the execution of the whole test. The assertions give us the possibility of full automation of the implementation of controls.These different arguments are static methods of the class Assert.

This example will help us understand the JUnit Assertion and will explain their basic use and basic meaning.

Want to be a JUnit Master ?

Subscribe to our newsletter and download the JUnit Programming Cookbook right now!

In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!


1. Junit Assertions

Before we code our example, let’s take a look at the following table. This table describes the JUnit Assertions and gives an overview of the most basic and important methods of the Assertclass:

assertTrue(boolean contition)
Asserts that a condition is true.
assertFalse(boolean contition)
Asserts that a condition is false.
assertNull(Object object)
Asserts that an object is null.
assertNotNull(Object object)
Asserts that an object isn’t null.
assertEquals(Object object1, Object object2)
Asserts that two objects are equal.
assertSame(Object object1, Object object2)
Asserts that two objects refer to the same object.
assertNotSame(Object object1, Object object2)
Asserts that two objects dont refer to the same object.
assertArrayEquals(Array array1, Array array2)
Asserts that two arrays are equal.

2. Basic Java Class to be tested

Here we have our basic Java Class that we need to test. We are going to use a simple example regarding Account balances.


package com.javacodegeeks.core.junit;

public class Account {
	private double balance;
	private String name;

	public Account(double balance, String name) {
		this.balance = balance;
		this.name = name;

	public double getBalance() {
		return balance;

	public void setBalance(double balance) {
		this.balance = balance;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public void withdraw(double withdrawAmount) {
		this.balance = balance - withdrawAmount;

	public void deposit(double depositAmount) {
		this.balance = balance + depositAmount;

3. Test Case

Here we have our test case for the Account.class Java Class.This test class includes all the basic assertions mentioned above.

AccountAssertionsTest .java

package com.javacodegeeks.core.junit;

import static org.junit.Assert.*;

import org.junit.Test;

public class AccountAssertionsTest {

	public void AccountAssertionsTest() {

		Account account_one = new Account(200, "John Doe");
		Account account_two = new Account(200, "Tom Smith");
		Account account_three = new Account(100, "John Doe");
		Account account_four = null;
		Account account_five = account_one;
		Account[] allAccounts_one = { account_one, account_two, account_three };
		Account[] allAccounts_two = { account_one, account_two, account_three };

		// assertTrue
		// checking if a condition is true
		assertTrue(account_one.getBalance() == account_two.getBalance());

		// assertFalse
		// checking if a condition is true
		assertFalse(account_one.getBalance() == account_three.getBalance());

		// assertFalse
		// checking if a condition is false
		assertFalse(account_one.getBalance() == account_three.getBalance());

		// assertNull
		// checking if an object is null

		// assertNotNull
		// checking if an object is not null

		// assertEquals
		// checking if two objects are equal
		assertEquals(account_one.getName(), account_three.getName());

		// assertSame
		// checking if two objects references point the same object
		assertSame(account_one, account_five);

		// assertNotSame
		// checking if two objects references don't point the same object
		assertNotSame(account_one, account_four);

		// assertArrayEquals
		// checking if two arrays are the equal
		assertArrayEquals(allAccounts_one, allAccounts_two);

4. Run the Test Case

Now, we only have to run in Junit the AccountAssertionsTest.class. This JUnit should be fully passed, because all the assertations are correct. The output shows that all the assertions in the AccountAssertionsTest.java where true.

All tests finished successfully... 

Download the example

This was an example of JUnit Assertions.

You can download the full source code of this example here: JUnitAssertionsExample

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....



Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns