数字の桁で失敗させる
成功するテストを書けたので、次は境界ケースで想定外の動作に遭遇したときに失敗するテストを書きましょう。
ソフトウェアエンジニアは、バグの存在を示すために、意図的に失敗するテストを書くことがあります。
必要な JUnit パッケージはすでにインポートされています。
この演習はコースの一部です
Javaによるテスト入門
演習の手順
- このテストを JUnit が実行できるようにする正しいアノテーションを入力します。
- テスト対象のメソッドを呼び出します。
- 期待する動作をアサートするために適切な JUnit のアサーションを使います(このテストは失敗するはずです)。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
public class LastDigitWithTests {
public static void main(String[] args) {
launchTestsAndPrint(LastDigitTest.class);
}
}
class LastDigit {
public static int lastDigit(int a) {
return a % 10;
}
}
class LastDigitTest {
// Use the correct annotation to mark this as a JUnit test
@____
void testLastDigit() {
int number = -2025;
int expected = 5;
// Call the method under test
int actual = LastDigit.____(____);
// Use the correct JUnit assertion for the scenario
____(expected, actual);
}
}