java – EasyMock. expect(…). Times (…) and using easymock The difference between expect (…) and several times?
What's the difference:
ResultSet set = EasyMock.createNiceMock(ResultSet.class); EasyMock.expect(set.getInt("col1")).andReturn(1); EasyMock.expect(set.wasNull()).andReturn(false); EasyMock.expect(set.getInt("col2")).andReturn(2); EasyMock.expect(set.wasNull()).andReturn(false); EasyMock.replay(set); assertEquals(1,set.getInt("col1")); assertEquals(false,set.wasNull()); assertEquals(2,set.getInt("col2")); assertEquals(false,set.wasNull());
And this:
ResultSet set = EasyMock.createNiceMock(ResultSet.class); EasyMock.expect(set.getInt("col1")).andReturn(1); EasyMock.expect(set.getInt("col2")).andReturn(2); EasyMock.expect(set.wasNull()).andReturn(false).times(2); EasyMock.replay(set); assertEquals(1,set.wasNull());
?
Note: both sets of code can be successfully compiled and run as JUnit tests In addition, please note that the "beautiful" simulation is deliberately used here
Solution
Answer the questions in your title – no difference Call X. expect (y) Times (3) is exactly the same as the call
x.expect(y); x.expect(y); x.expect(y);
(note that as Andy Thomas Cramer pointed out, your specific example is not completely equivalent because the call order is different.)
It seems to be just a matter of convenience However, there is another obvious difference: in the case of times (), you can pass the expected number of calls as a variable Therefore, you can configure through some external files, even through the public constant int, and you can use it to trigger the test tool It is much more flexible than explicitly compiling the correct number of calls to expect () (update your code if you now need to test with five workers instead of three)