かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

JUnit Factory!

Agitar社のAgitarOneのJUnitの自動生成機能がタダで使えるっぽい。
サイトは↓。
junitfactory.com


ユーザ登録すれば、Eclipseのプラグインがダウンロードできる。
ダウンロードしなくてもお試しは可能!!
Web DemoでブラウザにJavaのコードをぱちぱちと打ち込んでボタン押せばOK。


ちなみに、このJUnit Factoryは、現状でグリーンになるコードを吐いてくれる。
たとえコードにバグがあろうとも!!
まぁ、明らかにこのロジック通らないよ!ってのはすぐ見つかるけど。


早速コードを食べさせてみよう。
有りがちな値検証コード。

public class Validator {
  public boolean validateId(String id) {
    if (id == null || "".equals(id)) {
      return false;
    }
    return id.matches("\\d{3}-\\d{4}");
  }
}

これを食べさせると、こんなコードを吐く。

/**
 * Generated by Agitar build: Agitator Version 1.0.4.000199 (Build date: Feb 16, 2007) [1.0.4.000199]
 * JDK Version: 1.5.0_09
 *
 * Generated on Feb 26, 2007 4:38:55 AM
 * Time to generate: 00:04.173 seconds
 *
 */

import com.agitar.lib.junit.AgitarTestCase;

public class ValidatorAgitarTest extends AgitarTestCase {
    static Class TARGET_CLASS = Validator.class;
    
    public void testConstructor() throws Throwable {
        new Validator();
        assertTrue("Test completed without Exception", true);
    }
    
    public void testValidateId() throws Throwable {
        boolean result = new Validator().validateId("575-8687");
        assertTrue("result", result);
    }
    
    public void testValidateId1() throws Throwable {
        boolean result = new Validator().validateId("testValidatorId");
        assertFalse("result", result);
    }
    
    public void testValidateId2() throws Throwable {
        boolean result = new Validator().validateId(null);
        assertFalse("result", result);
    }
    
    public void testValidateId3() throws Throwable {
        boolean result = new Validator().validateId("");
        assertFalse("result", result);
    }
}
  1. コンストラクタでちゃんとインスタンスが生成できるか
  2. 575-8687を与えるとtrueが返る
  3. testValidatorIdを与えるとfalseが返る
  4. nullを渡すとfalseが返る
  5. ""を渡すとfalseが返る


ばっちり!
境界値とかを見て判断してるので、結構いいテストケースを吐き出す。
これを、ダウンロードしたEclipseプラグインからは、ボタン1つで作れる!!
専用のランナーで走らせるとテストが実行された部分は緑色に、実行されなかったところが赤になるので何処が未テストが一目瞭然。


ソースがむこうのサーバに送られるから仕事じゃ使えないだろうけど趣味では無問題!