JUnitによるテスト

詳しいことはIBMdeveloperWorksに書いてありますが(http://www-6.ibm.com/jp/developerworks/java/050114/j_j-pg11094.html),ちょこっとやってみたので紹介.こんな感じでテストを作ると,

import junit.textui.TestRunner
class TestTest extends GroovyTestCase {
  void testA() {
    Thread.sleep(100)
  }
  void testB() {
    Thread.sleep(100)
    fail()
  }
  void testC() {
    Thread.sleep(100)
    assertEquals("hoge", "fuga")
  }
  void testD() {
    Thread.sleep(100)
  }
}

TestRunner.run(TestTest.class);

こんな感じで普通にテストできる.GroovyTestCaseにはassert系のメソッドがいくつか追加されている模様.

E:\workspace\GroovyLab>groovy Sandbox.groovy
..F.F.
Time: 0.41
There were 2 failures:
1) testB(TestTest)junit.framework.AssertionFailedError
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 (非常に長いので、数十行くらいのスタックトレース割愛)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
2) testC(TestTest)junit.framework.ComparisonFailure: expected: but was:
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 (非常に長いので、数十行くらいのスタックトレース割愛)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

FAILURES!!!
Tests run: 4,  Failures: 2,  Errors: 0

E:\workspace\GroovyLab>

なんかほんとJavaですね〜〜.