在套件测试中,EasyMock表示预期有0位匹配者被记录1位 [英] During suite tests EasyMock says 0 matchers expected 1 recorded

查看:59
本文介绍了在套件测试中,EasyMock表示预期有0位匹配者被记录1位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经使用EasyMock的班级扩展已有一段时间了。突然我得到了这个异常,但是只有当我运行整个测试套件时:

So I've been using EasyMock's class extension for a while now. All of a sudden I'm getting this exception, but only when I run the entire test suite:

java.lang.IllegalStateException: 0 matchers expected, 1 recorded.
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:42)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:34)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:26)
at org.easymock.internal.RecordState.invoke(RecordState.java:64)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:24)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56)
at org.easymock.classextension.internal.ClassProxyFactory$1.intercept(ClassProxyFactory.java:74)
at com.protrade.soccersim.data.emulator.matrix.PositionCategoryMatrix$$EnhancerByCGLIB$$c5298a7.getPossession(<generated>)
at com.protrade.soccersim.data.emulator.stats.team.PossessionCalculatorUnitTest.testDeterminePossessionHomeWin(PossessionCalculatorUnitTest.java:45)

涉及的代码是这个小美女(修剪了一个位):

The code involved is this little beauty (trimmed a bit):

    @Before
public void setUp() throws Exception {
    homeTeam = createMock( PositionCategoryMatrix.class );
    awayTeam = createMock( PositionCategoryMatrix.class );
    ...
}

@Test
public void testDeterminePossessionHomeWin() {
    expect(homeTeam.getPossession()).andReturn( 0.15151515 );
    expect(awayTeam.getPossession()).andReturn( 0.01515152 );
    replay( homeTeam, awayTeam );
    ...
}

第一个期望被抛出异常。而且确实没有任何意义。它说正在得到一个匹配器,但是该方法甚至没有参数。奇怪的是,仅在测试套件中!我正在@Before中创建一个新的模拟,因此它不应该从其他地方继承任何东西(不是其他方法可以在上面带有匹配器)

The exception is being thrown on the first expect. And it really doesn't make sense. It says it's getting a matcher, but the method doesn't even take an argument. And odd enough it's only during test suites! I'm creating a new mock in the @Before, so it shouldn't be inheriting anything from somewhere else (not that some other method would have a matcher on it)

那么,有什么想法吗?

推荐答案

我厌倦了在EasyMock的每一个新的旧代码库中看到这一点一起工作。编写一本新的EasyMock测试,由于未捕获Matchers,所有突然的随机测试开始失败。因此,我开始研究EasyMock如何存储那些Matchers。它使用了最后一个类LastControl,该类中有一些threadlocals,用于存储不同的内容。其中之一是为比赛者。幸运的是,那里有一个静态方法可以将所有Matchers从仍位于那里的threadlocal中拉出。因此,这给了我这个主意(在同事的帮助下,感谢斯文,他想得到信贷)

I was sick and tired of seeing this with each new legacy code base with EasyMock I had to work with. Write one new EasyMock test by the book and all of the sudden random tests start failing because of Matchers never captured. So I went looking how EasyMock stores those Matchers. It makes use of a final class LastControl, in that class are a few threadlocals where different things get stored. One of those was for the Matchers. Luck has it that there is a static method on there to pull all the Matchers from the threadlocal that where still on there. So this gave me this idea (with help of a collegue, thanks Sven, he wanted credit)

/**
 * Base class to make sure all EasyMock matchers are cleaned up.  This is not pretty but it will work
 * 
 * @author N069261KDS
 *
 */
public class BaseTest {

  @Before
  public void before(){
    LastControl.pullMatchers();
  }

  @After
  public void after(){
    LastControl.pullMatchers();
  }

}

基本上让失败的测试Matchers错误从此类扩展而来,您将确保Matchers被清除。请注意这是一种解决方法。令人讨厌的测试应该首先写好。但是,如果您必须经过5000多次测试,这是两个弊端中的较小者。我希望这会帮助人们!

Basicly let your test that fail with the Matchers error extend from this class and you'll be sure the Matchers are cleaned. Note this IS A WORKAROUND. The offending tests should have been written right in the first place. But if you have to wade through 5000+ tests , this is the lesser of two evils. I hope this will help people out !

这篇关于在套件测试中,EasyMock表示预期有0位匹配者被记录1位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆