目次 ⇒ Java単体テストライブラリ-Artery-サンプル
package jp.avaj.lib.test;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.util.List;
import org.junit.Test;
import jp.avaj.lib.algo.ArList;
/**
Java単体テストライブラリ-Artery/JUnit4-配列の等値判定
Arteryではint[] vs Integer[]やint[] vs long[]などの異なるタイプでも判定することができる
JUnitでは等値判定はassertArrayEqualsを使用する(assertEqualsではない)
JUnitではint[] vs Integer[]やint[] vs long[]など異なるタイプでは判定できない
*/
public class Q02_01 {
//////// 以下はArteryのサンプル
public static void main(String[] args) {
// テストケースの開始を宣言する ⇒ 集計が不要な場合は不要
ArTest.startTestCase("Q02_01");
// int[] vs int[]
{
int[] value0 = new int[]{0,1,2};
int[] value1 = new int[]{0,1,2};
ArTest.equals("int[] vs int[]","value0",value0,"value1",value1);
value1 = new int[]{10,11,12};
ArTest.equals("int[] vs int[] (NG)","value0",value0,"value1",value1);
}
// int[] vs Integer[]
{
int[] value0 = new int[]{0,1,2};
Integer[] value1 = new Integer[]{0,1,2};
ArTest.equals("int[] vs Integer[]","value0",value0,"value1",value1);
value1 = new Integer[]{10,11,12};
ArTest.equals("int[] vs Integer[] (NG)","value0",value0,"value1",value1);
}
// int[] vs Long[]
{
int[] value0 = new int[]{0,1,2};
Long[] value1 = new Long[]{0L,1L,2L};
ArTest.equals("int[] vs Long[]","value0",value0,"value1",value1);
value1 = new Long[]{10L,11L,12L};
ArTest.equals("int[] vs Long[] (NG)","value0",value0,"value1",value1);
}
// int[] vs Double[]
{
int[] value0 = new int[]{0,1,2};
Double[] value1 = new Double[]{0D,1D,2D};
ArTest.equals("int[] vs Double[]","value0",value0,"value1",value1);
value1 = new Double[]{10D,11D,12D};
ArTest.equals("int[] vs Double[] (NG)","value0",value0,"value1",value1);
}
// int[] vs BigDecimal[]
{
int[] value0 = new int[]{0,1,2};
BigDecimal[] value1 = new BigDecimal[]{new BigDecimal(0),new BigDecimal(1),new BigDecimal(2)};
ArTest.equals("int[] vs BigDecimal[]","value0",value0,"value1",value1);
value1 = new BigDecimal[]{new BigDecimal(10),new BigDecimal(11),new BigDecimal(12)};
ArTest.equals("int[] vs BigDecimal[] (NG)","value0",value0,"value1",value1);
}
// int[] vs List<Integer>
{
int[] value0 = new int[]{0,1,2};
List<Integer> value1 = ArList.construct(new Integer[]{0,1,2});
ArTest.equals("int[] vs List<Integer>","value0",value0,"value1",value1);
value1 = ArList.construct(new Integer[]{10,11,12});
ArTest.equals("int[] vs List<Integer> (NG)","value0",value0,"value1",value1);
}
// int[] vs String[] ⇒ 判定できるけど、使わない方がよいかも?
{
int[] value0 = new int[]{0,1,2};
String[] value1 = new String[]{"0","1","2"};
ArTest.equals("int[] vs String[]","value0",value0,"value1",value1);
value1 = new String[]{"10","11","12"};
ArTest.equals("int[] vs String[] (NG)","value0",value0,"value1",value1);
}
// String[] vs String[]
{
String[] value0 = new String[]{"0","1","2"};
String[] value1 = new String[]{"0","1","2"};
ArTest.equals("String[] vs String[]","value0",value0,"value1",value1);
value1 = new String[]{"10","11","12"};
ArTest.equals("String[] vs String[] (NG)","value0",value0,"value1",value1);
}
// String[] vs List<String>[]
{
String[] value0 = new String[]{"0","1","2"};
List<String> value1 = ArList.construct(new String[]{"0","1","2"});
ArTest.equals("String[] vs List<String>[]","value0",value0,"value1",value1);
value1 = ArList.construct(new String[]{"10","11","12"});
ArTest.equals("String[] vs List<String>[] (NG)","value0",value0,"value1",value1);
}
// テストケースを終了する ⇒ 集計が不要な場合は不要
ArTest.endTestCase();
}
//////// 以下はJUnit4のサンプル
// int[] vs int[] - 等しい場合 ⇒ しかしNGになる
// 「assertArrayEqualsを使う」ということを知らない場合は間違うかも..
@Test
public void test00() {
assertEquals(new int[]{0,1,2},new int[]{0,1,2});
}
// int[] vs int[] - 等しい場合 ⇒ これでOK
@Test
public void test01() {
assertArrayEquals(new int[]{0,1,2},new int[]{0,1,2});
}
// int[] vs Integer[] - 等しい場合 ⇒ これはコンパイルエラー
// このくらいはやってほしいけど...
@Test
public void test02() {
// assertArrayEquals(new int[]{0,1,2},new Integer[]{0,1,2});
}
// int[] vs long[] - 等しい場合 ⇒ これもコンパイルエラー
// このくらいはやってほしいけど...
@Test
public void test03() {
// assertArrayEquals(new int[]{0,1,2},new long[]{0,1,2});
}
// double[] vs double[],判定誤差を指定する必要がある ⇒ 等しい場合
@Test
public void test04() {
assertArrayEquals(new double[]{0D,1D,2D},new double[]{0D,1D,2D},0.5D);
}
// double[] vs double[],判定誤差を指定する必要がある ⇒ 等しくない場合
@Test
public void test05() {
assertArrayEquals(new double[]{0D,1D,2D},new double[]{10D,11D,12D},0.5D);
}
// String[] va String[] - 等しい場合
@Test
public void test06() {
assertArrayEquals(new String[]{"a","b","c"},new String[]{"a","b","c"});
}
// String[] va String[] - 等しくない場合
@Test
public void test07() {
assertArrayEquals(new String[]{"a","b","c"},new String[]{"x","y","z"});
}
}
result.txt
**** Q02_01 start ****
OK int[] vs int[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs int[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:32)
OK int[] vs Integer[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs Integer[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:40)
OK int[] vs Long[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs Long[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:48)
OK int[] vs Double[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs Double[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:56)
OK int[] vs BigDecimal[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs BigDecimal[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:64)
OK int[] vs List<Integer>:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs List<Integer> (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:72)
OK int[] vs String[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs String[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:80)
OK String[] vs String[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG String[] vs String[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:88)
OK String[] vs List<String>[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG String[] vs List<String>[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:96)
**** Q02_01 summary ****
test count = 18
success = 9