0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

リファクタリングしたJavaソースの単体テストを実装してみた

Posted at

はじめに

下記記事で過去に自分で書いたJavaソースをリファクタリングしました。
今回はとても簡易的ではありますが、Mockitoを使用した単体テストを実装してみました。
実務でjunitを使用したことはありますが、Mockitoを使用した経験はないので初挑戦です。

単体テストの実装

過去記事の「SectionService.java」をテストする場合を考え下記「SectionServiceTest.java」を実装しました。

InfoDaoクラスがDBと密結合しているため、実際のDBがなくても動くようにInfoDaoクラスをモック化しています。

SectionServiceTest.java
package service;

import static org.junit.Assert.*;

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

import java.util.ArrayList;
import model.SecBean;
import model.InfoDao;

public class SectionServiceTest {

	@InjectMocks
	private SectionService sectionService;

	@Mock
	private InfoDao infoDao;

	@Before
	public void initMocks() {
		MockitoAnnotations.initMocks(this);
		System.out.println("initMocksが呼ばれました。");
	}

	@Test
	public void testGetAllSectionList01(){
		// infoDaoのallSectionSearchDaoメソッドが呼ばれた際、空のリストを返すように設定(スタブ化)する
		Mockito.doReturn(new ArrayList<SecBean>()).when(infoDao).allSectionSearchDao();

		// 実行して結果を受け取る
		ArrayList<SecBean> actual = sectionService.getAllSectionList();

		// 期待値の設定
		ArrayList<SecBean> expected = new ArrayList<SecBean>();

		// 検証
		assertEquals(expected,actual);
	}

	@Test
    // データを1件追加して検証
	public void testGetAllSectionList02(){
		// infoDaoのallSectionSearchDaoメソッドが呼ばれた際、1件のデータを持つリストを返すように設定(スタブ化)する
        ArrayList<SecBean> expected = new ArrayList<SecBean>();
        // ここでデータを1件追加
        expected.add(new SecBean());
		Mockito.doReturn(expected).when(infoDao).allSectionSearchDao();

		// 実行して結果を受け取る
		ArrayList<SecBean> actual = sectionService.getAllSectionList();

		// 検証
		assertEquals(expected,actual);
        assertEquals(expected.size(),actual.size());
	}

	@Test
    // SecBeanの中身まで検証
	public void testGetAllSectionList03(){
		// 期待値の準備
        ArrayList<SecBean> expected = new ArrayList<SecBean>();
        SecBean bean = new SecBean();
        bean.setSecName("HR");
        expected.add(bean);
        
        // モックの設定
		Mockito.doReturn(expected).when(infoDao).allSectionSearchDao();

		// 実行して結果を受け取る
		ArrayList<SecBean> actual = sectionService.getAllSectionList();

		// 検証
		assertEquals(expected,actual);
        assertEquals(expected.size(),actual.size());
        assertEquals(expected.get(0).getSecName(),actual.get(0).getSecName());
	}
}

1. testGetAllSectionList01()
  →単純にArrayList型のリストが返るかの検証

2. testGetAllSectionList02()
  →ArrayList型のリストにデータが1件入っているかの検証

3. testGetAllSectionList03()
  →ArrayList型のリストにデータが1件入っていて、かつリストの先頭にあるSecBeanのName値が「HR」であるかの検証

参考にした記事

Mockito(モキート)とは

Mockito使い方メモ

おわりに

未経験の時にはControllerにほとんどの処理を集約していました。
それをリファクタリングし、サービス層にわける事により単体テストが行える設計へ修正。
今回はそのサービス層の単体テストを実装!

実務ではJUnitを使用していますが、しっかり保守・運用まで管理できるように設計されているんだと実感しました。
Excelから値を読み取りDTOへセット、テスト対象のクラスへはDTOを介して全てデータが渡るようになっている為このような単体テストが可能でした。

ブラックボックス化されていて意識しないと見えない部分が見えてきたので楽しくなりました。
もっと深堀りしていきたいと思います!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?