CLOVER🍀

That was when it all began.

Fest Assertionsを使って、テストコードを書く

最近、こちらの本を読んでいまして。

まだ読み始めたばかりで、かつ全然Play Framework2と関係なくてなんなのですが、ここの第1章に出てきたこんなコードが気になりまして。

    assertThat(contentType(html)).isEqualTo("text/html");

JUnit+Hamcrestかなぁ〜と思って流そうとしたら、なんか雰囲気が違う…。どうも、Fest Assertionsというテストライブラリで、アサーションを流れるようなインターフェースで書けるものみたいです。JUnitTestNGと一緒に使えるみたいですよ。

FEST Fluent Assertions 2.x
https://github.com/alexruiz/fest-assert-2.x

Fest Assertions 2.0 documentation
https://github.com/alexruiz/fest-assert-2.x/wiki

Javadocはどこだろう…。

とりあえず、チュートリアル的なものを見ながら進めてみることにします。

One minute starting guide
https://github.com/alexruiz/fest-assert-2.x/wiki/One-minute-starting-guide

今回はJUnitと一緒に使うことにして、最初に軽くJUnitと比べてみます。

こちら参考にして。

Converting JUnit assertions to Fest Assertions
https://github.com/alexruiz/fest-assert-2.x/wiki/Converting-JUnit-assertions-to-Fest-Assertions

Using fest assertions
https://github.com/alexruiz/fest-assert-2.x/wiki/Using-fest-assertions

まずは、依存関係の定義。

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.easytesting</groupId>
      <artifactId>fest-assert-core</artifactId>
      <version>2.0M10</version>
      <scope>test</scope>
    </dependency>

JUnitと合わせて使います。

また、スケープゴート的に、Commons Lang3にも入ってもらいましょう。

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.1</version>
      <scope>compile</scope>
    </dependency>

では、最初にJUnit(+Hamcrest)で書いたテストコード。
src/test/java/fest/assertions/example/CommonsLangJUnitTest.java

package fest.assertions.example;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.apache.commons.lang3.StringUtils;

import org.junit.Test;

public class CommonsLangJUnitTest {
    @Test
    public void simpleTest() {
        assertThat(StringUtils.join("Hello", " ", "World"),
                   is("Hello World"));

        assertThat("JUnit TestCase.", startsWith("JUnit"));
    }
}

自分は、このスタイルをいつも使っています。

続いて、Fest Assertionsを使ったテストコード。
src/test/java/fest/assertions/example/CommonsLangFestAssertionsTest.java

package fest.assertions.example;

import static org.fest.assertions.api.Assertions.*;

import org.apache.commons.lang3.StringUtils;

import org.junit.Test;

public class CommonsLangFestAssertionsTest {
    @Test
    public void simpleTest() {
        assertThat(StringUtils.join("Hello", " ", "World"))
            .isEqualTo("Hello World");

        assertThat("Fest Assertions TestCase.")
            .startsWith("Fest Assertions");
    }
}

JUnit+Hamcrestと違って、確かに流れるようなインターフェースになりますね。

static importは、以下の1文です。

import static org.fest.assertions.api.Assertions.*;

あとは、今回はJUnitと合わせているので@Testアノテーションを使っている感じですね。

他にも、いくつか使ってみましょう。
src/test/java/fest/assertions/example/FestAssertionsTest.java

package fest.assertions.example;

import static org.fest.assertions.api.Assertions.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class FestAssertionsTest {
    @Test
    public void stringTest() {
        assertThat("Hello" + ", " + "World")
            .isEqualTo("Hello, World");

        assertThat("").isNullOrEmpty();
        assertThat((String) null).isNullOrEmpty();

        assertThat("Java").hasSize(4);

        assertThat("The quick brown fox jumps over the lazy dog")
            .contains("fox");
    }

    @Test
    public void intTest() {
        assertThat(1 + 3).isEqualTo(4);
        assertThat(5 * 5).isLessThan(30);
        assertThat(5 * 5).isGreaterThan(20);
    }

    @Test
    public void listTest() {
        assertThat(new ArrayList<Object>()).isNullOrEmpty();
        assertThat(new ArrayList<Object>()).isEmpty();

        assertThat(Arrays.asList("Hello", ", ", "World"))
            .hasSize(3);
        assertThat(Arrays.asList("Hello", ", ", "World"))
            .contains("World");
        assertThat(Arrays.asList("Hello", ", ", "World"))
            .containsOnly(", ", "Hello", "World");
        assertThat(Arrays.asList("Hello", ", ", "World"))
            .containsSequence("Hello", ", ", "World");
    }
}

isEqualToとかisEmpty、hasSize、containsなど、個人的にはこっちのスタイルの方が好きですかね〜。

でも、あんまり情報が見つからない気もしますけど。世の中的にはどうなんでしょう?

そして、最近の開発状況は…??