2 Minutes To Code (β Version)

JUnit 4.10 - 2 minute tutorial

How to test whether expected Exception is thrown

If your application code is designed to throw an exception when certain conditions are met, you can test for this by passing the name of the exception in the parameter "expected" to the Test annotation.

If the exception is not thrown, then junit reports that the test failed.

The trivial example below throws a NullPointerException.
package com.techfundaes.junit4Bag;

import org.junit.Test;

public class TestException 
{
	@Test( expected = NullPointerException.class)
	public void transactNullTest()
	{
		Account accountOne = new Account();
		accountOne.transact(100);
		
		Account accountTwo = null;
		accountOne.merge(accountTwo);
	}
}