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.

Spockでの条件付き判定

Posted at

Spockでテストコードを書くとき、1つのメソッドに対してテストケースが複数ある場合、whereブロックを使ってまとめて書けるとスッキリして見やすくなる。

before
def "StringUtils#split()"() {
	when:
	def result1 = StringUtils.split("a")

	then:
	result1.size() == 1

	when:
	def result2 = StringUtils.split("a b c")

	then:
	result2.size() == 3
}
after
def "StringUtils#split()"() {
	when:
	def result = StringUtils.split(str)

	then:
	result.size() == size

	where:
	str     || size
	"a"     ||    1
	"a b c" ||    3
}

が、場合によってはwhereブロックでは表現できないケースもある。例えばメソッドの戻り値がnullの場合とそうでない場合とで判定を分けたい、とか。

if文とかで分岐させりゃいいのか?、と思ってやってみる。

def "StringUtils#split()"() {
	when:
	def result = StringUtils.split(str)

	then:
	if (result != null) {
		result.size() == size
	}

	where:
	str     || size
	"a"     ||    1
	"a b c" ||    3
	null    || null
}

テストは成功するのでめでたしめでたし、と思いきや、whereブロックのsizeの値が正しくなくてもテストは成功してしまう。if文じゃなくてswitch文とかでも同じ。

	where:
	str     || size
	"a"     ||   11
	"a b c" ||   33
	null    || null

危険!
このような場合は明示的にassertを使用することで正しく動作する。

	then:
	if (result != null) {
		assert result.size() == size
	}

あるいは、三項演算子を使うという手も、なくはない。

	then:
	result != null ? result.size() == size : true

これでも一応ちゃんと判定はしてくれる。
見やすいか見にくいかは、あなた次第。

なお、使用したSpockのバージョンは1.3。

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?