英文:
The utilities I wrote don't work correctly + test cases
问题
以下是翻译好的内容:
我编写的工具无法正常工作。因此,我有两个执行不同任务的工具:
-
ExtractPhoneNumber
- 如果电话号码为null,则返回null;如果不为null,则该工具必须从号码中除数字以外的所有字符(例如“+”或“-”或“()”)删除。如果号码以0开头,则我的工具会在变量phone中写入去除0的号码(012345 -> 12345);如果电话号码加上国家区号的字符数小于或大于配置中允许的号码长度,则返回null;如果号码不包含国家区号,则将国家区号添加到电话号码中。 -
ValidatePhoneNumber
- 如果我的字符串与正则表达式匹配,则返回true;如果不匹配,则返回false(如果电话号码包含除“()”或“+”或“-”之外的其他内容,则工具应返回false)。
我试图解释这些工具应该如何工作,此外,在编写了这些工具之后,我编写了一些测试,但不幸的是这些测试未能通过,因此我无法理解我的错误在哪里。我在下面提供了这两个工具及其测试的代码:
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;
}
}
}
测试:
英文:
The utilities I wrote don't work correctly . So, I have 2 utilities that do different things :
-
ExtractPhoneNumber - if the phone number is null then it returns null , if not null, the utility must remove from the number all characters except numbers( e.g. "+" or "-" or "()")
If number starts with 0 then my utility write in variable phone , without 0 (012345 -> 12345)
if length of phone number + country code number (of characters) < or > than the number allowed in the configuration it will return null
if the number does not contain a country code then the country code added to phone number -
ValidatePhoneNumber - if my string matches a regular expression, then I return true, if not, then false (if the phone number contains something other than "()" or "+" or "-" then the utility should return false)
I tried to explain how the utilities should work, also after I wrote them, I wrote tests that unfortunately do not pass, so I can not understand what my mistake is , I put 2 utilities and tests on them.
below are the utilities and tests for them :
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;
}
}
}
Tests :
答案1
得分: 0
你的测试中使用了@Mock
和@InjectMocks
注解在未在测试中使用的实例变量上。
UtilService
上的@InjectMocks
本身不是一个模拟对象,因此不应该称为utilsServiceMock
,而应该简单地称为utilsService
。然后,您需要模拟configService
(它是一个模拟对象)的方法返回值,以返回您需要测试utilsService
的数据。
您的UtilsService
应该接受configService
作为构造函数参数。你有一个不带参数的utilsService
构造函数,这很奇怪,它不应该存在。
最后,你需要在测试中使用实例变量utilsService
,而不是每次都重新创建UtilsService
对象。至于你的核心逻辑,你应该先修复测试,然后使用它们来找出代码中是否有任何错误(如果有的话)。
英文:
Your tests are using @Mock
and @InjectMocks
on instance variables that are not used in your tests.
The @InjectMocks
on the UtilService
is itself not a mock btw, and thus shouldn't be called utilsServiceMock
, but simply utilsService
. You will need to then mock the method return values of your configService
(which is a mock) to return the data that you need to test the utilsService
.
Your UtilsService
should accept the configService
as a constructor parameter. It's strange that you have a utilsService
constructor that takes no parameters; it should not be the case.
Finally you need to use the instance variable utilsService
in your tests, and not recreate the UtilsService
object each time. As for your core logic, you should first fix the tests and then use them to find any bugs in your code (if there are any).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论