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?

More than 3 years have passed since last update.

javaのmockitoを使用してリスト型データを返すには

Posted at

Junitで単体テストをする。(ライブラリ下記を使用)
・Eclipse4.8
・Java 8
・mockit-1.10.19
・junit-4.12


package G_T.OfficeSystem.controller;
import java.util.List;

public class Test1  {

	//DBが無い時にリストデータを返すメソッド
	public  List<String> runSample(String ID,String PASS) {
		// 未実装
		return null;
	}

	//ただ、何か文字を返すメソッド
	public String runSample(String ID) {
		// 未実装
		return null;
	}
}
//DBが無い時にリストデータを返すメソッド
		Test1 hoge = mock(Test1.class);
		List<String> list = Arrays.asList(new String[] { "a","b","c" });

		when(hoge.runSample("one","1")).thenReturn(list);

		assertEquals( "a", list.get(0));

		List<String> list_結果= hoge.runSample("one","1");

		for (int i=0; i<list_結果.size(); ++i)
		{
		    System.out.println(list_結果.get(i));
		}

(uniqにブロックを与える書き方は http://ruby-doc.org/core-2.2.0/Array.html を参照)

参考資料
junit基礎 https://qiita.com/ryuutamaehara/items/c8efb304b73cc0542e6f

テストでよく使う検証メソッド一覧 https://qiita.com/opengl-8080/items/e57dab6e1fa5940850a3 を参照

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?