测试方法-Ruby / RSpec与Java / Mockito

|| 我正在尝试编写一些代码,如下例所示,但是用Java而不是Ruby和Mockito而不是RSpec。
require \'rubygems\'
require \'rspec\'

class MyUtils
  def self.newest_file(files)
    newest = nil
    files.each do |file|
      if newest.nil? || (File.new(file).mtime > File.new(newest).mtime)
        newest = file
      end
    end
    newest
  end
end

describe MyUtils do
  it \"should return the filename of the file with the newest timestamp\" do
    file_a = mock(\'file\', :mtime => 1000)
    file_b = mock(\'file\', :mtime => 2000)
    File.stub(:new).with(\"a.txt\").and_return(file_a)
    File.stub(:new).with(\"b.txt\").and_return(file_b)
    MyUtils.newest_file([\'a.txt\', \'b.txt\']).should == \'b.txt\'
  end
end
在RSpec中,我可以对File.new存根,但是我认为我不能在Mockito中做到这一点? 我是否应该使用工厂来创建File对象,将工厂作为依赖项注入,然后在工厂中存根进行测试?     
已邀请:
        这样的答案包括使用Mockito模拟File类,也许会有所帮助。     
        是的,您需要注入一些东西。无论是创建文件的工厂还是文件本身,都取决于您。完成此操作后,您可以在测试中模拟工厂。     

要回复问题请先登录注册