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
	public void transactBooleanTest()
		Account account = new Account();
		assertTrue("Test Failed Message", account.balanceAmount == 5);
	public void transactObjectTest()
		Account accountOne = new Account();
		Account accountTwo = new Account();
		assertEquals("Test Failed Message", accountOne, accountTwo);