mockito(2) 呼び出し回数チェック+例外処理チェック

mockito(1) スタブ的に使う - 基礎からのjavaの続き。
verifyメソッドで呼び出し回数のチェック。
例外を発生させ、キャッチした例外が想定通りかチェック。
(@Test(expected=XXX.class)では型のチェックしかできないのでキャッチしています)

あわせてテストケースに漏れがないか、カバレッジも確認したい。

UserLogicTest.java

package jp.ne.hatena.matasaburou.logic;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import jp.ne.hatena.matasaburou.dao.UserDao;
import jp.ne.hatena.matasaburou.entity.UserEntity;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class UserLogicTest{

	@Mock
	private UserDao dao;

	@InjectMocks
	private UserLogic target;

	@Before
	public void setup(){
		MockitoAnnotations.initMocks(this);
	}

	/**
	 * daoのfindAllメソッドが呼び出されていることを確認する。
	 * @throws Exception
	 */
	@Test
	public void test_findAll_ok() throws Exception{
		List<UserEntity> list = new ArrayList<>();
		list.add(createEntity("takeda",0));
		list.add(createEntity("sanada", 1));
		when(dao.findAll()).thenReturn(list);

		List<UserEntity> result = target.findAll();
		assertThat(result.size(), is(2));

		// dao呼び出し回数チェック
		verify(dao, times(1)).findAll();
	}

	/**
	 * レコードの登録でエラーが発生しないことを確認する。
	 * @throws Exception
	 */
	@Test
	public void test_insertData_ok() throws Exception{
		UserEntity entity = createEntity("suzuki", 2);
		when(dao.insert(entity)).thenReturn(1);
		target.insertData(entity);

		// dao呼び出し回数チェック
		verify(dao, times(1)).insert(entity);
	}

	/**
	 * nullが渡されて例外がスローされることを確認する。
	 */
	@Test
	public void test_insertData_null() throws Exception{
		try{
			// メソッド呼び出し
			target.insertData(null);
		}catch(Exception e){
			// 期待する例外がスローされているかチェック
			assertThat(e, is(instanceOf(InvalidParameterException.class)));
			assertThat(e.getMessage().startsWith("entity is null."), is(true));
		}

		// dao呼び出し回数チェック
		verify(dao, never()).insert(null);
	}

	/**
	 * フィールドがnullのエンティティを渡されて例外がスローされることを確認する。
	 */
	@Test(expected=Exception.class)
	public void test_insertData_nullParam() throws Exception{
		UserEntity entity = createEntity(null, null);
		target.insertData(entity);

		// dao呼び出し回数チェック
		verify(dao, times(1)).insert(entity);
	}

	/**
	 * 更新件数が1件以外のとき、例外がスローされることを確認する。
	 */
	@Test
	public void test_insertData_exception() throws Exception{
		// メソッドの挙動を定義
		when(dao.insert((UserEntity)anyObject())).thenReturn(10);

		try{
			// メソッド呼び出し
			target.insertData(createEntity("test", 0));
		}catch(Exception e){
			// 期待する例外がスローされているかチェック
			assertThat(e, is(instanceOf(Exception.class)));
			assertThat(e.getMessage().startsWith("insert userData failed."), is(true));
		}

		// dao呼び出し回数チェック
		verify(dao, times(1)).insert((UserEntity)anyObject());
	}

	/**
	 * テスト用オブジェクト作成
	 * @param name 名前
	 * @param sex 性別
	 * @return UserEntity
	 */
	public UserEntity createEntity(String name, Integer sex){
		UserEntity entity = new UserEntity();
		entity.setName(name);
		entity.setSex(sex);
		return entity;
	}

}