BaşlayınÜcretsiz Başlayın

CsvSource: Rakamlar

Bölüm 1'den lastDigit() yöntemine geri döndün ve bu kez Math.abs() kullanarak negatif değerlere karşı hazırlık yaptın. Artık testleri @ParameterizedTest ile kısaltabileceğini bildiğine göre, lastDigit() yönteminin tüm sonuçlarını tek seferde test etmeyi denedin.

Yöntemin pozitif, negatif ve sıfırla biten tamsayılar için doğru rakamı döndürdüğünü doğrula.

Bu egzersiz

Java'da Teste Giriş

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Bir test yöntemine birden fazla değer girmek için kullanılan ek açıklamayı kullan.
  • Test yöntemi için yöntem argümanlarını ekle.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

public class LastDigitWithTests {
  public static void main(String[] args) {
    launchTestsAndPrint(LastDigitTest.class);
  }
}

class LastDigit {
    public static int lastDigit(int a) {
        return Math.abs(a) % 10;
    }
}

class LastDigitTest {

    @ParameterizedTest
    // Use the correct annotation for a pair of integer inputs
    @____(value = {"2025, 5", "-2025, 5", "2020, 0"}) 
    // Write the corresponding argument types for the test method
    void testLastDigit(____ number, ____ expected) {
        int actual = LastDigit.lastDigit(number);

        assertEquals(expected, actual);
    }
}
Kodu Düzenle ve Çalıştır