如何用JAVA编写10位电话号码的Junit测试

问题描述 投票:0回答:1

我正在尝试为电话号码编写一个测试用例,其中只允许通过数字。如何使用对象类型在java中编写它。

我尝试过对字符进行硬编码。但我想要一个比这更好的解决方案

电话号码分机号 = new 电话号码(); ExtensionNUM.setExtensionNum("!@#$%^&*()_-+=}]{[|;:'<,>./?qwertyuioplkjhgfdsazxcvbnmàrèhìp\òtù"); contactPreference.setTelephoneNumber(ExtensionNUM);

java unit-testing junit testcase
1个回答
0
投票

由于您要在对象中捕获此内容,因此您可以简单地在对象的构造函数中断言此事实:


public USTelephoneNumber implement TelephoneNumber {
   public USTelephoneNumber(String number) {
    // here you can handle +1 formats
    // But of course Canada also uses +1, so +1 is just a sanity check
    // but you can also apply much more sophisticated logic (like length, or using external validation.
     if (!number.startsWith("+1")){
        throw new IllegalArgumentException(String.format("%s is not a US number", number));
     }
     
     if (!number.replaceAll("[ ]","").replaceAll("+1","").matches("[0-9]+")) {
        throw new IllegalArgumentException(String.format("%s is not all numbers",number));

   }
}
@Throws(IllegalArgumentException.class)
public testIsNotUsPhoneNumber() throws IllegalArgumentException {
    new USPhoneNumber("+48 158 610 715");
}


public testIsUsPhoneNumber() {
    new USPhoneNumber("+1 425 388 2588");
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.