2 Minutes To Code (β Version)

JUnit 4.10 - 2 minute tutorial

How to test a class

The example below shows how test cases are written. In order to mark a methods a test method, just annotate it with @Test which is an annotation in the org.junit package.

The next page shows how to run these test cases.

The most important part in the test methods below are the methods calls assertTrue and assertEquals. These are methods in the class org.junit.Assert and are usually statically imported as in the import statement below.

The methods provided by Assert class are assertArrayEquals, assertEquals, assertTrue, assertFalse, assertNull, assertNotNull, assertSame, assertNotSame, assertThat and fail.
package com.techfundaes.junit4Bag;

import org.junit.Test;
import static org.junit.Assert.*;

public class TestBasic
{
	@Test
	public void transactBooleanTest()
	{
		Account account = new Account();
		account.transact(10).transact(-5);
		
		assertTrue("Test Failed Message", account.balanceAmount == 5);
	}
	
	@Test
	public void transactObjectTest()
	{
		Account accountOne = new Account();
		accountOne.transact(1000).transact(-300);
		Account accountTwo = new Account();
		accountTwo.transact(500);
		
		assertEquals("Test Failed Message", accountOne, accountTwo);
	}
}