简体   繁体   English

轻松获得模拟行为

[英]Easy mock behaviour while requested

I was thinking, is it possible to mock whole object behavior with EasyMock, but in a way that once declared mock with all expected values and results is used several times without caring about the order of the requests ? 我当时在想,是否可以用EasyMock模拟整个对象的行为,但是一旦声明了带有所有期望值和结果的模拟,就可以使用多次而无需关心请求的顺序?

The purpose for this is to create an instance of mock for example in JUnit test @BeforeClass and use it in several @Test methods. 这样做的目的是在JUnit test @BeforeClass中创建一个模拟实例,并在多个@Test方法中使用它。

Thank you in advance for any input, 预先感谢您的任何投入,

Regards, P. 问候,体育。

如果您不希望验证对模拟的调用,并且唯一的目的是确保无论何时调用模拟上的特定方法,该方法都会始终返回相同的所需结果,您可以使用andStubReturn()对其进行配置,例如

expect(mock.getMeaningOfLifeUniverseAndEverything()).andStubReturn(42);

I think you're really wanting two things: 我认为您确实想要两件事:

  1. The ability to use mocked methods out of order, which is the default easymock (non-strict mode), 乱序使用模拟方法的能力,这是默认的easymock(非严格模式),
  2. The ability to use mocked methods any number of times. 可以多次使用模拟方法。

You do the latter like this: 您可以像这样进行后者:

expect(someMock.someMethod()).anyTimes().andReturn(someValue);

If your method will get different arguments each time it is called, you can use the anyObject() method to ignore the provided argument. 如果您的方法每次调用都会得到不同的参数,则可以使用anyObject()方法忽略提供的参数。

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

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