简体   繁体   English

我应该如何测试调用调用静态/最终方法的构造函数的方法? (模仿)

[英]How should I test a method that calls a constructor that calls static/final methods? (Mockito)

I have a method that creates an object, which the constructor of that object then calls several static/final methods.我有一个创建 object 的方法,该 object 的构造函数然后调用几个静态/最终方法。 Mockito cannot mock a constructor, and it can also not mock static/final methods, so what am I supposed to do? Mockito 不能模拟构造函数,也不能模拟静态/最终方法,那我该怎么办?

--Edit-- - 编辑 -

Apparently people think my question is unclear: I have a method.显然人们认为我的问题不清楚:我有一个方法。 It calls a constructor.它调用构造函数。 Constructor is not mockable.构造函数是不可模拟的。 How do I test this method?我如何测试这种方法?

The reason why I mentioned static/final methods is because I decided I may as well test the constructor as well and mock the methods within that constructor.我之所以提到静态/最终方法是因为我决定我也可以测试构造函数并模拟该构造函数中的方法。 However, it is full of static/final methods which makes this impossible.但是,它充满了静态/最终方法,这使得这不可能。

You can use PowerMockito to mock static methods and also the constructors您可以使用 PowerMockito 来模拟 static 方法以及构造函数

For example: https://automationrhapsody.com/mock-static-methods-junit-powermock-example/例如: https://automationrhapsody.com/mock-static-methods-junit-powermock-example/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM