Digits with MethodSource
Like most old and popular programming languages, Java has a vast ecosystem, and there are many ways to do one thing.
Consider the LastDigit exercise from the previous lesson. Practice the lesson's syntax by rewriting it using @MethodSource.
Este exercício faz parte do curso
Introduction to Testing in Java
Instruções do exercício
- Use the correct annotation for a parameterized test that takes its arguments from a method.
- Point the annotation to the arguments provider method.
- Use the same syntax three times to create argument pairs.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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)
);
}
}