Cyfry z MethodSource
Jak wiele starych i popularnych języków programowania, Java ma rozbudowany ekosystem – i niemal każde zadanie można rozwiązać na kilka sposobów.
Wróć do ćwiczenia LastDigit z poprzedniej lekcji. Przećwicz poznaną składnię, przepisując je z użyciem @MethodSource.
To ćwiczenie jest częścią kursu
Wprowadzenie do testowania w Javie
Instrukcje do ćwiczenia
- Użyj odpowiedniej adnotacji dla testu parametryzowanego, który pobiera argumenty z metody.
- Wskaż adnotacji metodę dostarczającą argumenty.
- Użyj tej samej składni trzy razy, aby utworzyć pary argumentów.
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
public class LastDigitWithTests {
public static void main(String[] args) {
launchTestsAndPrint(LastDigitTest.class);
}
}
class LastDigitTest {
@ParameterizedTest
// Add the annotation to use a method to provide arguments and point it to the arguments method
@____("____")
void testLastDigit(int number, int expected) {
int actual = LastDigit.lastDigit(number);
assertEquals(expected, actual);
}
private static List getArgs() {
return List.of(
// Create three argument objects for the test using the same syntax
____.of(2025, 5),
____.of(-2025, 5),
____.of(2020, 0)
);
}
}