Mockito是一个Java库,用于模拟第三方实用方法和私有方法调用。

huangapple go评论72阅读模式
英文:

Mockito java - mock third party utility methods and private method calls

问题

我有一个遗留代码,其中有一些如下所示的方法:

public void display(String fileName, Path path){
    //做一些操作
    Files.exists(path);
    if(null==privateMethod(fileName))
       //一些操作
}

private Object privateMethod(String fileName){
   // 做一些操作
   return object;
}

我正在为display方法编写测试(使用mockito)。如何处理像是 Files.exists(path) 这样的调用以及内部私有方法的调用,因为我认为普通的存根(stubbing)在私有方法上无法使用(可见性问题)。由于 Files.exists() 是实用方法,所以无法进行模拟。我是否需要在测试方法中实际创建一些测试文件,然后可能将它们删除?或者是否有更好、更清晰的方法?我已经搜索过关于私有方法测试的内容 - 每个人都建议使用 powermock,但我不被允许使用它。无论如何,我不需要测试私有方法,而只需要绕过/存根它们的调用返回

英文:

I have a legacy code which has some methods like below:

public void display(String fileName, Path path){
    //do some stuff
    Files.exists(path);
    if(null==privateMethod(fileName))
       //something
}

private Object privateMethod(String fileName){
   // do something
   return object;
}

I am writing tests(using mockito) for display method. How do I get around calls like Files.exists(path) and internal private method calls because I think normal stubbing cannot used with private method(visibility issue). As Files.exists() is utility method so cannot be mocked. Do I need to actually create some test files in testing method and probably delete them? Or is there a better clean way. Also I have searched regarding private method testing - everyone suggests powermock which Iam not allowed to use. Anyway I don't need to test private method but bypass/stub their call returns.

答案1

得分: 0

在这种情况下,为测试准备一些文件是有意义的。您可以在文件夹 src/test/resources 中生成它们或提前创建它们。

例如,一个测试可能如下所示:

@Test
public void myTest(){
    // MyFile.txt 在 src/test/resources 中
    Path path = Paths.get("src","test","resources"); 
    service.display("MyFile.txt", path);
    // 断言..
}

关于模拟私有方法,一般情况下不推荐,因为您的测试将依赖于类的实现细节,所以我强烈不建议这样做。而且,如果您不允许使用 Powermock,这可能根本不可能实现。

但是有一些替代方法,例如,如果私有方法使用了一些外部服务或库,您可以模拟它们,而不是整个方法。这个想法是模拟类依赖的外部实体,而不是类本身的部分。

英文:

In this case makes sense to have some files for testing. You can generate them or create them in advance in the folder src/test/resources.

For example a test might look like this:

@Test
public void myTest(){
    // MyFile.txt is in src/test/resources
    Path path = Paths.get("src","test","resources"); 
    service.display("MyFile.txt", path);
    // Asserts..
}

Regarding mocking private methods, in general is not recommended because your test will depend on implementation details of your class, so I strongly discourage it. Furthermore, if you are not allowed to use Powermock it might not be possible at all.

But there are alternatives, for example if the private method uses some external services or libraries, than you can mock them instead of the whole method. The idea is to mock the external entities that the class depends on rather than parts of the class itself.

huangapple
  • 本文由 发表于 2020年9月1日 01:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63675380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定