Java 正则表达式匹配 9 月 15 日星期三(XXX,XXX ##)的单元测试未通过
Unit test for Java regex to match Wed, Sep 15 (XXX, XXX ##) not passing
我正在尝试匹配与 "XXX, XXX ##"
等内容相匹配的正则表达式
所以我尝试了这个:"\w{3},\s\w{3}\s\d{2}"
但它不起作用
包括完整代码,这是我要测试的方法:
public String formatDate(String instruction) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat();
if (instruction.equalsIgnoreCase("short")) {
format = new SimpleDateFormat("EEE, MMM yy");
}
else if (instruction.equalsIgnoreCase("full")) {
format = new SimpleDateFormat("EEEE, MMMM yyyy");
}
String dateToString = format.format(date);
//Log.e("exe", dateToString);
return dateToString;
}
这是单元测试:
public class SchedulerDateTest {
SchedulerDate schedulerDate;
@Before
public void setUp() throws Exception {
schedulerDate = new SchedulerDate();
}
//....
@Test
public void testFormatDate() throws Exception {
assertTrue("if parameter is short I want to receive the date in a short format for example: Wed, Sep 15",
schedulerDate.formatDate("short")
.matches("\w{3},\s\w{3}\s\d{2}");
}
}
写问题时,请使其最小化、完整且可验证。参见 https://whosebug.com/help/mcve
这是以上所有内容:
Date date = new Date();
System.out.println("date = " + date);
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM yy");
String dateToString = format.format(date);
System.out.println("dateToString = '" + dateToString + "'");
System.out.println("matches = " + dateToString.matches("\w{3},\s\w{3}\s\d{2}"));
输出
date = Wed Sep 09 22:16:21 EDT 2015
dateToString = 'Wed, Sep 15'
matches = true
通过打印中间值,您将能够看到哪里可能出了问题。
这可能是您这边的语言问题,例如如果我切换 Locale
:
Locale.setDefault(Locale.CANADA_FRENCH);
输出
date = Wed Sep 09 22:19:17 EDT 2015
dateToString = 'mer., sept. 15'
matches = false
我正在尝试匹配与 "XXX, XXX ##"
等内容相匹配的正则表达式所以我尝试了这个:"\w{3},\s\w{3}\s\d{2}"
但它不起作用
包括完整代码,这是我要测试的方法:
public String formatDate(String instruction) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat();
if (instruction.equalsIgnoreCase("short")) {
format = new SimpleDateFormat("EEE, MMM yy");
}
else if (instruction.equalsIgnoreCase("full")) {
format = new SimpleDateFormat("EEEE, MMMM yyyy");
}
String dateToString = format.format(date);
//Log.e("exe", dateToString);
return dateToString;
}
这是单元测试:
public class SchedulerDateTest {
SchedulerDate schedulerDate;
@Before
public void setUp() throws Exception {
schedulerDate = new SchedulerDate();
}
//....
@Test
public void testFormatDate() throws Exception {
assertTrue("if parameter is short I want to receive the date in a short format for example: Wed, Sep 15",
schedulerDate.formatDate("short")
.matches("\w{3},\s\w{3}\s\d{2}");
}
}
写问题时,请使其最小化、完整且可验证。参见 https://whosebug.com/help/mcve
这是以上所有内容:
Date date = new Date();
System.out.println("date = " + date);
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM yy");
String dateToString = format.format(date);
System.out.println("dateToString = '" + dateToString + "'");
System.out.println("matches = " + dateToString.matches("\w{3},\s\w{3}\s\d{2}"));
输出
date = Wed Sep 09 22:16:21 EDT 2015
dateToString = 'Wed, Sep 15'
matches = true
通过打印中间值,您将能够看到哪里可能出了问题。
这可能是您这边的语言问题,例如如果我切换 Locale
:
Locale.setDefault(Locale.CANADA_FRENCH);
输出
date = Wed Sep 09 22:19:17 EDT 2015
dateToString = 'mer., sept. 15'
matches = false