使用參數化的使用者名稱驗證
隨著你對 Java 的理解加深,你決定重溫第 1 章的使用者名稱驗證器。因為它只依賴單一輸出,所以可以用參數化來縮短測試。
請思考 isValidUsername() 方法所有會失敗的情況。把它們濃縮到單一的 @ParameterizedTest 中。
所需的 JUnit 套件已為你匯入。
本練習屬於課程
Java 測試入門
練習說明
- 使用正確的註解,將測試標記為參數化測試。
- 在測試中加入一個 null 輸入。
- 對測試中的輸入值加入相應的註解。
- 標示輸入值的型別。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
public class Main {
public static void main(String[] args) {
launchTestsAndPrint(UsernameValidatorTest.class);
}
}
class UsernameValidatorTest {
// Annotate with the correct annotation for parameterized test
@____
// Add the annotation for inputting a null input
@____
// Add the annotation for inputting values and the type of the inputs
@____(____ = {"john doe", "jd"})
void isValidUsername_returnsFalse_whenSpaces(String username) {
boolean actual = UsernameValidator.isValidUsername(username);
assertFalse(actual);
}
}