我想要一种优雅的方法将java Date向上或向下舍入到最近的分钟(或秒,小时,天).
例如,"Wed Wed 25 10:36:34 GMT 2012"的日期四舍五入到最接近的分钟将是"Wed Jan 25 10:37:00 GMT 2012"
我有一个通过JNA进行本机Windows API调用的类.如何编写将在Windows开发机器上执行但在Unix构建服务器上将被忽略的JUnit测试?
我可以轻松地使用主机操作系统 System.getProperty("os.name")
我可以在测试中编写保护块:
@Test public void testSomeWindowsAPICall() throws Exception {
if (isWindows()) {
// do tests...
}
}
Run Code Online (Sandbox Code Playgroud)
这个额外的锅炉板代码并不理想.
或者,我创建了一个只在Windows上运行测试方法的JUnit规则:
public class WindowsOnlyRule implements TestRule {
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (isWindows()) {
base.evaluate();
}
}
};
}
private boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}
}
Run Code Online (Sandbox Code Playgroud)
这可以通过将这个带注释的字段添加到我的测试类来强制执行:
@Rule public WindowsOnlyRule runTestOnlyOnWindows = new WindowsOnlyRule();
Run Code Online (Sandbox Code Playgroud)
这两种机制在我看来都是不足的,因为在Unix机器上它们会默默地传递.如果它们可以在执行时以某种方式用类似的东西标记它会更好@Ignore
有人有其他建议吗?
我真正想要做的是创建一个不同时运行的Quartz作业,但也可以访问它JobExecutionContext以获得previousFireTime.这是我的目标:
// imports...
public class UtilityObject {
private SomeService someService;
@Autowired
public UtilityObject(SomeService someService) {
this.someService = someService;
}
public void execute(JobExecutionContext ctx) throws JobExecutionException {
Date prevDate = ctx.getPreviousFireTime();
// rest of the code...
}
}
Run Code Online (Sandbox Code Playgroud)
这是我如何配置我的bean:
<bean name="utilityBean" class="UtilityObject" />
<bean id="utilityJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetOjbect" ref="utilityBean" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>
<bean name="utilityTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="utilityJob" />
<property name="startDelay" value="5000" />
<property name="repeatInterval" value="20000" />
</bean>
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,它在创建bean时失败了
NoSuchMethodException:UtilityJob.execute() …