junit

JUnit Annotations 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.

JUnit 4 has been updated, and on the latest version supports annotations for the testing methods. Annotations are a big change from JUnit 3 to JUnit 4, and compared to the previous version, JUnit 4 has also introduced @BeforeClass and @AfterClassmethods which have to be declared as static methods.

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

1. Junit Annotations

Before we code our example, let’s take a look at the following table. This table describes the JUnit Annotations and gives an overview of the most important available annotations, showing their meaning and use.

@Test
public void method()
The Test annotation indicates that the public void method to which it is attached can be run as a test case.
@Before
public void method()
The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.
@BeforeClass
public static void method()
The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class. That happens when the test methods share computationally expensive setup (e.g. connect to database).
@After
public void method()
The After annotation indicates that this method gets executed after execution of each test (e.g. reset some variables after execution of every test, delete temporary variables etc)
@AfterClass
public static void method()
The AfterClass annotation can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the expensive set-up (e.g disconnect from a database). Attention: The method attached with this annotation (similar to BeforeClass) must be defined as static.
@Ignores
public static void method()
The Ignore annotation can be used when you want temporarily disable the execution of a specific test. Every method that is annotated with @Ignore won’t be executed.

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;

	public Account(double balance) {
		this.balance = balance;
	}

	public double getBalance() {
		return balance;
	}

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

	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 annotations mentioned above.

AccountTest.java

package com.javacodegeeks.core.junit;
import org.junit.*;

public class AccountTest extends Assert {
	private Account account;
	private static double balance;

	@BeforeClass
	public static void BeforeClass() {
		balance = 100;
		System.out.println("BeforeClass");
	}

	@Before
	public void setUp() throws Exception {
		account = new Account(balance);
	}

	@Test
	public void balanceForAccount() {
		Assert.assertEquals("Test balance", account.getBalance(), balance, 0);
		System.out.println("Test balance. Balance: " + account.getBalance());
	}

	@Test
	public void testOneDeposit() {
		account.deposit(20);
		Assert.assertEquals("Test deposit", account.getBalance(), balance, 20);
		System.out.println("Test deposit. Balance: " + account.getBalance());
	}

	@Test
	public void testManyDeposits() {
		account.deposit(20);
		account.deposit(10);
		Assert.assertEquals("Test many deposits", account.getBalance(), balance, 30);
		System.out.println("Test many deposits. Balance: " + account.getBalance());
	}

	@Test
	public void testOneWithdraw() {
		account.withdraw(20);
		Assert.assertEquals("Test withdraw", account.getBalance(), balance, 20);
		System.out.println("Test withdraw. Balance: " + account.getBalance());
	}

	@Test
	public void testManyWithdraws() {
		account.withdraw(20);
		account.withdraw(10);
		Assert.assertEquals("Test many withdraws", account.getBalance(), balance, 30);
		System.out.println("Test many withdraws. Balance: " + account.getBalance());
	}

	@After
	public void tearDown() throws Exception {
		account = null;
		System.out.println("tearDown");
	}

	@AfterClass
	public static void AfterClass() {
		balance = 0;
		System.out.println("AfterClass");
	}

	@Ignore
	public void executionIgnored() {
		System.out.println("@Ignore: This execution is ignored");
	}
}

4. Run the Test Case

Here is the output of our test case. As we can see, the sequence of the executed test methods, complies with what we descibed in the annotations table.This JUnit test is fully passed.

BeforeClass
Test balance. Balance: 100.0
tearDown
Test deposit. Balance: 120.0
tearDown
Test many deposits. Balance: 130.0
tearDown
Test withdraw. Balance: 80.0
tearDown
Test many withdraws. Balance: 70.0
tearDown
AfterClass

Download the example

This was an example of JUnit Annotations.

Download
You can download the full source code of this example here: JUnitAnnotationsExample.zip

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
Inline Feedbacks
View all comments
Back to top button