我编写的实用程序无法正常工作 + 测试用例
The utilities I wrote don't work correctly + test cases
我编写的实用程序不能正常工作。所以,我有 2 个实用程序可以做不同的事情:
ExtractPhoneNumber - 如果 phone 号码为空,则它 return 为空,如果不为空,实用程序必须从号码中删除除数字以外的所有字符(例如“ +”或“-”或“()”)
如果数字以 0 开头,那么我的实用程序写入变量 phone ,不带 0 (012345 -> 12345)
如果 phone 号码的长度 + 国家代码号码(字符)< 或 > 超过配置中允许的号码,它将 return null
如果该号码不包含国家代码,则将国家代码添加到 phone 号码
ValidatePhoneNumber - 如果我的字符串匹配正则表达式,那么我 return 为真,如果不匹配,则为假(如果 phone 数字包含除“() " 或 "+" 或 "-" 那么实用程序应该 return false)
我试图解释这些实用程序应该如何工作,而且在我编写它们之后,我编写的测试很遗憾没有通过,所以我不明白我的错误是什么,我在它们上面放了 2 个实用程序和测试。
以下是它们的实用程序和测试:
public String extractPhoneNumber(String value) {
String phone = StringUtils.trimToNull(value);
if (phone == null)
return null;
phone = phone.replaceAll("[^0-9]", "");
if (phone.startsWith("0")) {
phone = phone.substring(1);
}
if (phone.length() + configService.getPhoneCountryCode().length() < configService.getPhoneNumberLength()) {
return null;
} else {
if (!phone.startsWith(configService.getPhoneCountryCode())) {
if (phone.length() + configService.getPhoneCountryCode().length() > configService.getPhoneNumberLength()) {
return null;
}
phone = configService.getPhoneCountryCode() + phone;
}
}
return phone;
}
public final static Pattern VALID_PHONE_NUMBER_PATTERN =
Pattern.compile("[^0-9()\-+]");
public boolean validatePhoneNumber(String phoneNumber) {
if (phoneNumber == null) {
return false;
} else {
Matcher matcher = VALID_PHONE_NUMBER_PATTERN.matcher(phoneNumber);
if (matcher.matches()) {
return true;
} else {
return false;
}
}
}
测试:
您的测试在您的测试中未使用的实例变量上使用 @Mock
和 @InjectMocks
。
UtilService
上的 @InjectMocks
本身不是模拟 btw,因此不应称为 utilsServiceMock
,而应简称为 utilsService
。然后,您需要将 configService
的方法 return 值(这是一个模拟)模拟为 return 您需要测试 utilsService
.[=23 的数据=]
您的 UtilsService
应该接受 configService
作为构造函数参数。奇怪的是,您有一个不带参数的 utilsService
构造函数;不应该是这样的。
最后,您需要在测试中使用实例变量 utilsService
,而不是每次都重新创建 UtilsService
对象。至于您的核心逻辑,您应该首先修复测试,然后使用它们来查找代码中的任何错误(如果有的话)。
我编写的实用程序不能正常工作。所以,我有 2 个实用程序可以做不同的事情:
ExtractPhoneNumber - 如果 phone 号码为空,则它 return 为空,如果不为空,实用程序必须从号码中删除除数字以外的所有字符(例如“ +”或“-”或“()”) 如果数字以 0 开头,那么我的实用程序写入变量 phone ,不带 0 (012345 -> 12345) 如果 phone 号码的长度 + 国家代码号码(字符)< 或 > 超过配置中允许的号码,它将 return null 如果该号码不包含国家代码,则将国家代码添加到 phone 号码
ValidatePhoneNumber - 如果我的字符串匹配正则表达式,那么我 return 为真,如果不匹配,则为假(如果 phone 数字包含除“() " 或 "+" 或 "-" 那么实用程序应该 return false)
我试图解释这些实用程序应该如何工作,而且在我编写它们之后,我编写的测试很遗憾没有通过,所以我不明白我的错误是什么,我在它们上面放了 2 个实用程序和测试。
以下是它们的实用程序和测试:
public String extractPhoneNumber(String value) {
String phone = StringUtils.trimToNull(value);
if (phone == null)
return null;
phone = phone.replaceAll("[^0-9]", "");
if (phone.startsWith("0")) {
phone = phone.substring(1);
}
if (phone.length() + configService.getPhoneCountryCode().length() < configService.getPhoneNumberLength()) {
return null;
} else {
if (!phone.startsWith(configService.getPhoneCountryCode())) {
if (phone.length() + configService.getPhoneCountryCode().length() > configService.getPhoneNumberLength()) {
return null;
}
phone = configService.getPhoneCountryCode() + phone;
}
}
return phone;
}
public final static Pattern VALID_PHONE_NUMBER_PATTERN =
Pattern.compile("[^0-9()\-+]");
public boolean validatePhoneNumber(String phoneNumber) {
if (phoneNumber == null) {
return false;
} else {
Matcher matcher = VALID_PHONE_NUMBER_PATTERN.matcher(phoneNumber);
if (matcher.matches()) {
return true;
} else {
return false;
}
}
}
测试:
您的测试在您的测试中未使用的实例变量上使用 @Mock
和 @InjectMocks
。
UtilService
上的 @InjectMocks
本身不是模拟 btw,因此不应称为 utilsServiceMock
,而应简称为 utilsService
。然后,您需要将 configService
的方法 return 值(这是一个模拟)模拟为 return 您需要测试 utilsService
.[=23 的数据=]
您的 UtilsService
应该接受 configService
作为构造函数参数。奇怪的是,您有一个不带参数的 utilsService
构造函数;不应该是这样的。
最后,您需要在测试中使用实例变量 utilsService
,而不是每次都重新创建 UtilsService
对象。至于您的核心逻辑,您应该首先修复测试,然后使用它们来查找代码中的任何错误(如果有的话)。