3
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 1 year has passed since last update.

assertThatとは

Posted at

assertThat

JUnitやHamcrestを使用して、テストコード内で条件を検証するためのメソッド

テスト対象の実行結果やオブジェクトの状態を、期待する値や条件と比較する
比較結果が一致しない場合は、テストがFailする

assertThatの例

このメソッドをテストする

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

assertThatを使ってテストする

import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class CalculatorTest {

    @Test
    public void testAdd() {
        Calculator calculator = new Calculator();

        // メソッドの実行結果をテスト
        int result = calculator.add(3, 5);

        // assertThatとHamcrestのマッチャーを使って結果をテスト
        assertThat(result, is(equalTo(8))); // 3 + 5 = 8

        // 他のマッチャーを使ったテストの例
        assertThat(result, is(not(equalTo(10)))); // 結果は10と等しくない
        assertThat(result, is(greaterThan(5))); // 結果は5より大きい
    }
}
3
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
3
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?