已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。
关闭24分钟前
Improve this question
我正在寻找一个解决方案来测试这些方法,特别是日期实用程序类。我添加了我的两个方法,我想写一个测试。我如何在最后调用方法,以及如何doReturn XMLGregorianCalendar时,它有嵌套返回内部像我添加的方法?
public static XMLGregorianCalendar getCalendar() throws DatatypeConfigurationException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
XMLGregorianCalendar gdateFormatted =
DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(DateUtils.currentDate()));
return gdateFormatted;
}
public static String getDateNowYyMm() throws DatatypeConfigurationException {
DateFormat format = new SimpleDateFormat("yyMM");
XMLGregorianCalendar gdateFormatted =
DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(DateUtils.currentDate()));
return gdateFormatted.toString();
}
我写的是:
public class DateUtils {
@Test
public void privateConstructorTest() throws Exception {
Constructor<DateUtils> constructor = DateUtils.class.getDeclaredConstructor();
Assert.assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
constructor.newInstance((Object[]) null);
}
@Test
public void getCalendarTest() throws DatatypeConfigurationException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
XMLGregorianCalendar gdateFormatted = new XMLGregorianCalendar();
}
}
我不能成功的XMLGregorianCalendar的一部分。
1条答案
按热度按时间8yoxcaq71#
完全看不出你的代码是如何与void方法相关的,但是如果你的函数返回当前日期为
XMLGregorianCalendar
或String
,并且你正在询问如何在当前日期总是变化的情况下对它们进行单元测试,那么我会通过构造函数传递DateUtils
并模拟currentDate()
函数以返回一些可以轻松进行单元测试的特定日期。编辑:
我刚刚意识到你的方法是静态的。考虑让它们成为非静态的,并将它们 Package 到一个可以轻松测试的类中。如果你想要一个全局静态助手的好处,你可以在非测试代码中创建这个类的静态单例。