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 @AfterClass
methods 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 | The Test annotation indicates that the public void method to which it is attached can be run as a test case. |
@Before | 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 | 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 | 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 | 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 | 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.
You can download the full source code of this example here: JUnitAnnotationsExample.zip