junit

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

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button