概要
JUnit4のDataPointsによるテストと特定ケースの実行
内容
JUnit4のDataPointsによって、テストをパラメータ化することができます。
パラメータ化されたケースはFixtureの配列として定義されます。
詳しくは下記参照。
JUnit4のDataPointsによるテストとパラメータ化の基準
JUnit4のDataPointsによるテストとFixtureと流れるようなインターフェース
Fixtureの配列を用いたテスト時に、特定のケースのみ実行したい場合があります。
このような際のサンプルコードを作成します。
サンプルコード
テスト対象のコード(文字列ユーティリティ。ゼロパディングの処理)
StringUtil.java
package gr.java_conf.tb.tbpg_util.lang; import java.math.BigDecimal; import java.text.DecimalFormat; /** * 文字列ユーティリティ。 * */ public class StringUtil { private static final String POINT_STRING = "."; private static final String ZERO_STRING = "0"; private static final String ERR_NOT_NUMBER = "数値を指定してください"; /** * 文字列を指定桁数,小数点桁数でゼロパディングする。 * * @param value 文字列 * @param digit 桁数 * @param underDigit 小数点以下桁数 * @return パディングされた文字列 */ public static String getZeroPadding(String value, int digit, int underDigit) { String sValue = ""; if (StringUtil.isEmpty(value)) { sValue = ZERO_STRING; } else { if (!StringUtil.isNumber(value)) { throw new IllegalArgumentException(ERR_NOT_NUMBER); } sValue = value; } BigDecimal decimalValue = new BigDecimal(sValue); // 小数点以下のフォーマット作成 StringBuffer sUnderDigit = new StringBuffer(); for (int i = 0; i < underDigit; i++) { sUnderDigit.append(ZERO_STRING); } // 小数点の文字数(小数点対応時のみ長さ1) int pointLength = underDigit == 0 ? 0 : 1; // 整数部のフォーマット作成 StringBuffer sOverDigit = new StringBuffer(); for (int i = 0; i < digit - underDigit - pointLength; i++) { sOverDigit.append(ZERO_STRING); } String sFormat; if (underDigit == 0) { sFormat = sOverDigit.toString(); } else { sFormat = sOverDigit.toString() + POINT_STRING + sUnderDigit.toString(); } DecimalFormat decimalFormat = new DecimalFormat(sFormat); return decimalFormat.format(decimalValue); } /** * 文字列がnullもしくは空文字か判定する。 * * @param value * @return 判定結果 */ public static boolean isEmpty(String value) { if (value == null) { return true; } if (value.equals("")) { return true; } return false; } /** * 文字列の数値判定。 * * @param value 文字列 * @return 判定結果 */ public static boolean isNumber(String value) { if (value == null) { return false; } try { new BigDecimal(value); return true; } catch (NumberFormatException e) { return false; } } }
StringUtilTest.java
package gr.java_conf.tb.tbpg_util.lang; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import gr.java_conf.tb.tbpg_util.common.CommonUtil; import java.util.Arrays; import java.util.List; import org.junit.experimental.runners.Enclosed; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; @RunWith(Enclosed.class) public class StringUtilTest { @RunWith(Theories.class) public static class GetZeroPaddingPoint { static class Fixture { int caseNo; String target; int digit; int underDigit; String expected; boolean hasException; String errorMessage; Fixture() { } public Fixture caseNo(int caseNo) { this.caseNo = caseNo; return this; } public Fixture digit(int digit) { this.digit = digit; return this; } public Fixture underDigit(int underDigit) { this.underDigit = underDigit; return this; } public Fixture errorMessage(String errorMessage) { this.errorMessage = errorMessage; return this; } public Fixture expected(String expected) { this.expected = expected; return this; } public Fixture hasException(boolean hasException) { this.hasException = hasException; return this; } public Fixture target(String target) { this.target = target; return this; } public String toString() { return CommonUtil.getAllFiledInfo(Fixture.class, this); } }; // private List<Integer> validTestCases = Arrays.asList(1, 2, 3, 4, 5); private List<Integer> validTestCases = Arrays.asList(5); @DataPoints public static Fixture[] getFixture() { Fixture[] fixture = { new Fixture().caseNo(1).target("").digit(5).underDigit(2).expected("00.00").hasException(false), new Fixture().caseNo(2).target(null).digit(5).underDigit(2).expected("00.00").hasException(false), new Fixture().caseNo(3).target("1").digit(5).underDigit(2).expected("01.00").hasException(false), new Fixture().caseNo(4).target("a").digit(5).underDigit(2).hasException(true).errorMessage("数値を指定してください"), new Fixture().caseNo(5).target("1").digit(10).underDigit(2).expected("0000001.00").hasException(false), new Fixture().caseNo(6).target("1.23").digit(10).underDigit(2).expected("0000001.23").hasException(false), new Fixture().caseNo(7).target("1.234").digit(10).underDigit(2).expected("0000001.23").hasException(false), }; return fixture; } @Theory public void testGetZeroPaddingPoint(Fixture fixture) { if (!validTestCases.contains(fixture.caseNo)) { return; } /* 初期化 */ try { /* 実行 */ String actual = StringUtil.getZeroPadding(fixture.target, fixture.digit, fixture.underDigit); if (!fixture.hasException) { /* 検証 */ assertThat(fixture.toString(), actual, is(fixture.expected)); } else { assertThat(fixture.toString(), "", is("例外系にも関わらず正常終了")); } } catch (IllegalArgumentException e) { if (fixture.hasException) { /* 検証 */ assertThat(fixture.toString(), e.getMessage(), is(fixture.errorMessage)); } else { e.printStackTrace(); assertThat(fixture.toString(), "", is("正常系にも関わらず例外終了")); } } catch (Exception e) { e.printStackTrace(); assertThat(fixture.toString(), "", is("意図せぬ例外")); } } } }