Chryssa Aliferi

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

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.

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.

Account.java

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 {

	@Test
	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
		assertNull(account_four);

		// assertNotNull
		// checking if an object is not null
		assertNotNull(account_three);

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

Download
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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close