【发布时间】:2016-08-20 09:47:55
【问题描述】:
我正在尝试使用 groovy 的 metaClass 约定来模拟静态方法 readAttributes 之一,但真正的方法会被调用。
这就是我模拟静态函数的方式:
def "test"() {
File file = fold.newFile('file.txt')
Files.metaClass.static.readAttributes = { path, cls ->
null
}
when:
fileUtil.fileCreationTime(file)
then:
1 * fileUtil.LOG.debug('null attribute')
}
我在这里做错了吗?
我的java方法
public Object fileCreationTime(File file) {
try {
BasicFileAttributes attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
if(attributes == null) {
LOG.debug("null attribute");
}
//doSomething
} catch (IOException exception) {
//doSomething
}
return new Object();
}
【问题讨论】:
标签: java unit-testing groovy spock