くりにっき

フルスタックキュアエンジニアです

irofさんをJUnitで走らせてみた #irof_history

はじめに

これは いろふ Advent Calendar 2013 : ATND の7日目です

6日目

@ さんの "性善説"いろふさん登場! #irof_history でした

7日目:irofさんをJUnitで動かしてみた

id:irof さんは走るとすぐに疲れます

そしてirofさんといえばJUnitです。

そこでirofさんをJUnitにしてみました

Irof Runner

irofさんと一緒に走りましょう!

@RunWith(Irof)
class SampleIrofTest {
    @Test
    void "test 01"(){
    }

    @Test
    void "test 02"(){
    }

    // ....

    @Test
    void "test 162"(){
    }

    @Test
    void "test 163"(){
        // test failed!
    }

しかしirofさんは走ると疲れてしまいます

162*1テストケースを走ると疲れてしまうので、163個目で疲れてエラーになってしまいます

ソースコード

https://github.com/irof/irof_history/tree/master/irof-runner

maven central repositoryに公開する気はないので、使いたい場合には下記をコピペしてくればいいと思います( ´∀`)

Irof.groovy

package irof.runner

import org.junit.runner.notification.RunNotifier
import org.junit.runners.BlockJUnit4ClassRunner
import org.junit.runners.model.FrameworkMethod
import org.junit.runners.model.InitializationError

class Irof extends BlockJUnit4ClassRunner {
    static final int MAX_IROF_COUNT = 162
    static int testCount = 0

    Irof(Class<?> klass) throws InitializationError {
        super(klass)
    }

    @Override
    protected void runChild(FrameworkMethod method, RunNotifier notifier) {
        testCount++
        if(testCount > MAX_IROF_COUNT){
            throw new IrofTiredException()
        }
        originalRunChild(method, notifier)
    }

    protected void originalRunChild(FrameworkMethod method, RunNotifier notifier){
        super.runChild(method, notifier)
    }
}

IrofTiredException.groovy

package irof.runner

class IrofTiredException extends RuntimeException {
    IrofTiredException(){
        super("つかれた")
    }
}

分からなければJUnit実践入門を読めばいいと思います(ステマ)

JUnit実践入門 ~体系的に学ぶユニットテストの技法 (WEB+DB PRESS plus)

JUnit実践入門 ~体系的に学ぶユニットテストの技法 (WEB+DB PRESS plus)

まとめ

さぁみんなも自分のテストコードにirofさんをインストールしましょう!

8日目

@ さん

いろふAdvent Calendar 2013 : 人生、気合いと具合 - blog

*1:いろふ