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 5 years have passed since last update.

Java単体テストライブラリ-Artery/JUnit4-数値の等値判定

Posted at

目次 ⇒ Java単体テストライブラリ-Artery-サンプル

package jp.avaj.lib.test;

import static org.junit.Assert.*;

import java.math.BigDecimal;

import org.junit.Test;

/**
  Java単体テストライブラリ-Artery/JUnit4-数値の等値判定

  Arteryでは異なるタイプの数値でも等値判定をすることができる
    doubleでは(現在のところ)判定誤差は指定できない.
  JUnit4ではInteger vs Longなどの比較では等しい値でもNGになるので注意する.
 */
public class Q02_00 {
  //////// 以下はArteryのサンプル
  public static void main(String[] args) {
    ArTest.startTestCase("Q02_00");

    // int vs int
    {
      int value0 = 5;
      int value1 = 5;
      ArTest.equals("int vs int","value0",value0,"value1",value1);
      value1 = 10;
      ArTest.equals("int vs int (NG)","value0",value0,"value1",value1);
    }
    // int vs long
    {
      int value0 = 5;
      long value1 = 5L;
      ArTest.equals("int vs long","value0",value0,"value1",value1);
      value1 = 10L;
      ArTest.equals("int vs long (NG)","value0",value0,"value1",value1);
    }
    // int vs double
    {
      int value0 = 5;
      double value1 = 5D;
      ArTest.equals("int vs double","value0",value0,"value1",value1);
      value1 = 10D;
      ArTest.equals("int vs double (NG)","value0",value0,"value1",value1);
    }
    // int vs BigDecimal
    {
      int value0 = 5;
      BigDecimal value1 = new BigDecimal(5);
      ArTest.equals("int vs BigDecimal","value0",value0,"value1",value1);
      value1 = new BigDecimal(10);
      ArTest.equals("int vs BigDecimal (NG)","value0",value0,"value1",value1);
    }
    // int vs String ⇒ 数値でなくてもできる..⇒ 使わない方が良いかも..
    {
      int value0 = 5;
      String value1 = "5";
      ArTest.equals("int vs String","value0",value0,"value1",value1);
      value1 = "10";
      ArTest.equals("int vs String (NG)","value0",value0,"value1",value1);
    }
    // Integer vs Long
    {
      Integer value0 = 5;
      Long value1 = 5L;
      ArTest.equals("Integer vs Long","value0",value0,"value1",value1);
      value1 = 10L;
      ArTest.equals("Integer vs Long (NG)","value0",value0,"value1",value1);
    }
    // Integer vs Double
    {
      Integer value0 = 5;
      Double value1 = 5D;
      ArTest.equals("Integer vs Double","value0",value0,"value1",value1);
      value1 = 10D;
      ArTest.equals("Integer vs Double (NG)","value0",value0,"value1",value1);
    }
    // Integer vs BigDecimal
    {
      Integer value0 = 5;
      BigDecimal value1 = new BigDecimal(5);
      ArTest.equals("Integer vs BigDecimal","value0",value0,"value1",value1);
      value1 = new BigDecimal(10);
      ArTest.equals("Integer vs BigDecimal (NG)","value0",value0,"value1",value1);
    }
    // Integer vs String ⇒ 数値でなくてもできる..⇒ 使わない方が良いかも..
    {
      Integer value0 = 5;
      String value1 = "5";
      ArTest.equals("Integer vs String","value0",value0,"value1",value1);
      value1 = "10";
      ArTest.equals("Integer vs String (NG)","value0",value0,"value1",value1);
    }
    //
    ArTest.endTestCase();
  }

  //////// 以下はJUnit4のサンプル

  // int同士の等値判定-等しい場合
  @Test
  public void test00() {
    assertEquals(5,5);
  }

  // int同士の等値判定-等しくない場合
  @Test
  public void test01() {
    assertEquals(5,10);
  }

  // int vs longの等値判定-等しい場合
  @Test
  public void test02() {
    assertEquals(5,5L);
  }

  // int vs longの等値判定-等しくない場合
  @Test
  public void test03() {
    assertEquals(5,10L);
  }

  // int vs doubleの等値判定
  @Test
  public void test04() {
    // doubleの場合は判定誤差を指定する
    assertEquals(5,5D,0.01D);
  }

  // int vs BigDecimalの等値判定 ⇒ 同じ値でもNGとなる
  // これはOKにしてほしい... ⇒ でなければコンパイルエラーにしてほしい
  @Test
  public void test05() {
    assertEquals(5,new BigDecimal(5));
  }

  // Integer vs Longの等値判定 ⇒ 同じ値でもNGとなる
  // これはOKにしてほしい... ⇒ でなければコンパイルエラーにしてほしい
  @Test
  public void test06() {
    assertEquals(new Integer(5),new Long(5L));
  }

  // 蛇足 int vs String ⇒ (当然ながら)NGになる
  @Test
  public void test07() {
    assertEquals(5,"5");
  }
}

結果は次の通り

無題.png

result.txt
**** Q02_00 start ****
OK int vs int:value0=5:value1=5
NG int vs int (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:27)
OK int vs long:value0=5:value1=5
NG int vs long (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:35)
OK int vs double:value0=5:value1=5
NG int vs double (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:43)
OK int vs BigDecimal:value0=5:value1=5
NG int vs BigDecimal (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:51)
OK int vs String:value0=5:value1=5
NG int vs String (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:59)
OK Integer vs Long:value0=5:value1=5
NG Integer vs Long (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:67)
OK Integer vs Double:value0=5:value1=5
NG Integer vs Double (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:75)
OK Integer vs BigDecimal:value0=5:value1=5
NG Integer vs BigDecimal (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:83)
OK Integer vs String:value0=5:value1=5
NG Integer vs String (NG):value0=5:value1=10
jp.avaj.lib.test.Q02_00.main(Q02_00.java:91)
**** Q02_00 summary ****
test count = 18
success    = 9

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?