简体   繁体   English

EasyMock-随机测试失败-预期匹配

[英]EasyMock - Random tests failing - Matcher Expected

I have a strange problem with EasyMock 我对EasyMock有一个奇怪的问题

This is the call I'm making which throws an IllegalStateException : Matcher expected as expected 这是我正在进行的调用,抛出IllegalStateException : Matcher expected

expect(this.mock.expectedOperation(gt(0l), MyClass.class)).andReturn(createClassObject());

If I replace the above call with 如果我将上述通话替换为

expect(this.mock.expectedOperation(gt(0l), createClass(MyClass.class))).andReturn(createClassObject());

@SuppressWarnings("unchecked")
public static <T> Class<T> createClass(Class<T> clazz)
{
    return (Class<T>) EasyMock.anyObject();
}

Most times i don't get an error , but sometimes I do. 大多数情况下,我不会出错 ,但是有时候我会出错 It stays IllegalStateException : Matcher expected .. 它保持IllegalStateException : Matcher expected ..

Sometimes I get the IllegalStateException : 2 Matchers expected 1 recorder error for doing this: 有时我得到IllegalStateException : 2 Matchers expected 1 recorder这样做IllegalStateException : 2 Matchers expected 1 recorder错误:

MyClass object = createClassObject();
expect(this.mock.expectedOperation(anyLong(), anyLong()).andReturn(object);

public MyClass createClassObject() {
    // Actually sets properties and then returns
    return new MyClass();
}

But it runs when I do this: 但是它在我这样做时运行:

expect(this.mock.expectedOperation(anyLong(), anyLong()).andReturn(createClassObject());

In the above example, sometimes the former runs and latter fails. 在上面的示例中,有时前者运行而后者失败。

Sometimes this fails : 有时这会失败:

MyClass object = createClassObject();
expect(this.mock.expectedOperation(1, MyClass.class)).andReturn(object);

I have quadruple checked the reset, replay, verify calls. 我已经四重检查了重置,重播,验证电话。 These tests run sometimes and sometimes fail. 这些测试有时会运行,有时会失败。

If i run my unit test, it randomly fails at least once in one of the above listed situations. 如果我运行单元测试,则在上述情况之一中,它随机至少失败一次。 Why? 为什么? How do I get it running? 我如何运行它?

EDIT : I'm using EasyMock version 3.1 and 编辑:我正在使用EasyMock版本3.1和

MockedClass mock = EasyMock.createMock(MockedClass.class);

Found the problem. 找到了问题。 We can't use the gt(0) etc methods to pass parameters into the unit being tested. 我们不能使用gt(0)等方法将参数传递到要测试的单元中。

In another test by my mistake had used : 在我的错误的另一个测试中使用了:

service.performOperation(1, gt(0l));

service was not a mock, but the unit i was testing. 服务不是一个模拟,但我正在测试的单元。

On checking the documentation I saw that gt(0l) returns 0, which was causing this test to pass by others to fail. 在检查文档时,我看到gt(0l)返回0,这导致该测试通过其他人失败。 (Dunno why?) Since tests are executed randomly, random conditions were failing. (不知道为什么吗?)由于测试是随机执行的,因此随机条件失败了。

Using the reset(mocks..) at the beginning of the tests did nothing to help. 在测试开始时使用reset(mocks..)没有任何帮助。

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

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