assertThat()方法和assertEquals()方法都是JUnit框架中用于斷言測試結果的方法,但它們之間有一些區別。
使用方式:
assertThat(actual, Matchers.is(expected));
assertThat(actual, Matchers.allOf(Matchers.greaterThan(0), Matchers.lessThan(100)));
assertEquals(expected, actual);
對比方式:
assertThat(actual, Matchers.is(expected));
assertThat(actual, Matchers.containsString("Hello"));
錯誤信息:
Expected: is "Hello"
but: was "World"
expected:<Hello> but was:<World>
總的來說,assertThat()方法更加靈活,可以進行更復雜的斷言,而assertEquals()方法更加簡單直觀,適合進行簡單的相等比較。