Reputation: 699
I'm currently having a problem with a Unit test using EasyMock.
Expectation failure on verify:
FileConverter.convert(file, file2): expected: 1, actual: 1
This is the only failure in the class and it fails on the verify method below. I have tried to Google the message, but this only brings up results for "expected: 1, actual: 1 (+1)", and the +1 implies the error is different.
I have tried to simplify the structure of the failing EasyMock test for demonstration. Please forgive any typos:
@Test
public void testScan() {
String[] testFiles = { "file", "file2" };
FileConverter converterMock = EasyMock.createMock(FileConverter.class);
Poller poller = new Poller(new File("testFolder"), converterMock);
for (String testFile : testFiles) {
converterMock.convert(new File(testFile));
EasyMock.expectLastCall().once();
}
EasyMock.replay(converterMock);
for (String testFile : testFiles) {
poller.scan();
}
EasyMock.verify(converterMock);
}
I don't think the code itself is particularly relevant but I have included it for completeness - what I am really looking for is an explanation of what "expected 1, actual 1" could mean in the context of the EasyMock.verify method.
Thanks in advance!
Upvotes: 0
Views: 2434
Reputation: 17495
Error messages can be very unclear from these test runs. I do see it fails on expecting a convert
method call signature with 2 File
arguments. So try expecting that by completing/replacing the following line in your test:
converterMock.convert(new File(testFile));
with something like:
File file1 = new File(testFile1);
File file2 = new File(testFile2);
converterMock.convert(testFile1, testFile2);
Also experiment with less specific matching like:
converterMock.convert(EasyMock.isA(File.class), EasyMock.isA(File.class));
or
File file1 = new File(testFile1);
File file2 = new File(testFile2);
converterMock.convert(EasyMock.eq(file1), EasyMock.eq(file2));
Upvotes: 1
Reputation: 135
Another optional case is when using a multithreaded environment, it might occur that the desired method has invoked on the mock object after the test has already ended.
Upvotes: 5
Reputation: 5721
Are you using a recent version of EasyMock? Because I do remember old version performing strange calculation sometimes.
I tried your code and if I guessed correctly the content of the scan method.
void scan(String file) {
converter.convert(new File(file));
}
It should work perfectly.
Upvotes: 0