[英]Mockito JUnit testing: check method call in constructor
I have a class Foo with private and public or protected methods. 我有一个带有私有和公共或受保护方法的Foo类。 For example:
例如:
public class Foo{
private int number;
public Foo(){
setup();
doSthing();
}
private void doSthing(){
number=10;
}
public void setup(){
}
protected int getNumber(){
return number;
}
public void run(){
getNumber();
//Do blah blah...
}
}
And I am trying to test this class using Mockito. 我正在尝试使用Mockito测试此类。
public class FooTest
{
public void testMethods()
{
Foo foo = PowerMockito.mock (Foo.class);
//1 What should I do to see that setup() and doSthing()
//has been called on Foo construction
mock.run();
//2 What should I do to see that getNumber()
//has been called on run() method call
}
}
I am also hoping to include "times(1)" if possible. 我还希望尽可能包含“ times(1)”。 I'd appreciate your help.
多谢您的协助。
You are getting this wrong: 您错了:
First of all: please do not use any of the PowerMock ... frameworks. 首先:请不要使用任何PowerMock ...框架。 They rely on byte code manipulation;
他们依靠字节码操作; and sooner or later that calls for trouble.
迟早会带来麻烦。 Believe me, I have spent many hours hunting really strange errors for no good reasons.
相信我,我花了很多时间来寻找无缘无故的奇怪错误。
Then: do not test your code this way! 然后:不要以这种方式测试您的代码!
You use a mocking framework to create/control those objects that you pass to your "class under test". 您使用模拟框架来创建/控制传递给“被测类”的那些对象。 You don't use the framework to directly test your "class under test"!
您不使用该框架直接测试“被测类”!
You also don't want to write test code that knows about private methods - those are implementation details. 您也不想编写知道私有方法的测试代码-这些是实现细节。
What you do instead: 您要做的是:
a) as said, you can use mocking to pass objects into your class under test. a)如前所述,您可以使用模拟将对象传递到要测试的类中。 And then you can control/verify those mocks see the calls that you expect your "class under test" to make
然后,您可以控制/验证那些模拟,以查看您希望“测试中的类”进行的调用
b) you create objects of your "class under test" ... and then "assert" on the properties of those objects; b)创建“被测类”的对象……然后在那些对象的属性上“断言”; or on results that method calls return.
或方法调用返回的结果。
Just to be precise: a mocked object ... doesn't know anything about the code in the "original" class. 只是要准确:一个嘲笑的对象......不知道在“原始”类的代码什么 。 It is a mock !
这是一个模拟 ! You can't use it for the kind of testing that your question implies you want to do.
您不能将其用于您的问题暗示您想要做的那种测试。 Either you call methods on your real class;
您可以在真实的类中调用方法; or you specify something for a mock.
或者您为模拟指定内容。 But creating a mock to then "associate" it to the concrete class implementation ... is simply impossible.
但是创建一个模拟然后将其“关联”到具体的类实现上……是完全不可能的。
Finally: if you think that your concrete class is actually "too big" to just be created the normal way for tests ... then chances are: your class is simply too big for anything else. 最后:如果您认为您的具体类实际上“太大”而不能以正常的测试方式创建……那么机会就在于:您的类对于其他任何事情都太大了。 Classes should be small;
班级应小; and their design should follow SOLID principles.
并且其设计应遵循SOLID原则。
Long story short: don't try to force un-testable code into "tests", by using PowerMock... You better invest some more time to improve the quality of your production code; 长话短说:不要通过使用PowerMock来将无法测试的代码强加到“测试”中。您最好花更多的时间来提高生产代码的质量。 that will result in much higher "return on investment".
这将导致更高的“投资回报率”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.