(通過Eclipse中運行)。我已閱讀所有類似的帖子,但沒有找到答案。我有一個類包含兩個測試同樣的方法。我正在使用匹配器。EasyMock的:java.lang.IllegalStateException:1個匹配器預期,2記錄
- 每個測試單獨運行時通過。
- 第一個測試總是通過 - 如果我切換文件中的測試順序,這是真實的。
下面是測試代碼的簡化版本:
private Xthing mockXthing;
private MainThing mainThing;
@Before
public void setUp() {
mockXthing = EasyMock.createMock(Xthing.class);
mainThing = new MainThing();
mainThing.setxThing(mockXthing);
}
@After
public void cleanUp() {
EasyMock.reset(mockXthing);
}
@Test
public void testTwo() {
String abc = "abc";
EasyMock.expect(mockXthing.doXthing((String) EasyMock.anyObject())).andReturn(abc);
EasyMock.replay(mockXthing);
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
assertEquals("abc", testResult);
EasyMock.verify(mockXthing);
}
@Test
public void testOne() {
String xyz = "xyz";
EasyMock.expect(mockXthing.doXthing((String) EasyMock.anyObject())).andReturn(xyz);
EasyMock.replay(mockXthing);
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
assertEquals("xyz", testResult);
EasyMock.verify(mockXthing);
}
第二(或最後)測試總是失敗,錯誤如下:
java.lang.IllegalStateException: 1 matchers expected, 2 recorded
任何瞭解到,這將是不勝感激。
感謝, 安妮
哪一行會拋出異常? –
對不起 - 第二次(或最後一次)測試-EasyMock.expect(mockXthing.doXthing((String)EasyMock.anyObject()))。andReturn(XYZ); – Anne
好吧,我認爲這就是我的答案然後。 –