Version of JMockit that was used: 1.40. TestNG: 6.14.3
Description of the problem:
Using the pattern shown in the tutorial for "Instances created with a given constructor", I can't successfully mock separate instances of java.io.File whereas I can successfully mock a dummy class with the same constructor/method. The example included shows that a test fails when an expectation on the File instance is not met for file1.exists(); due to a missing invocation but passes for the DummyFile instance. I have attempted to reduce the code to the minimum amount to reproduce the problem. Apologies if I have made an elementary mistake - I'm new to JMockit. Code and results are below:
package test;
import java.io.File;
import org.testng.Assert;
import org.testng.annotations.Test;
import mockit.Expectations;
import mockit.Mocked;
public class TestFileTest {
static class DummyFile {
final String filename;
DummyFile(String filename) {
this.filename = filename;
}
boolean exists() {
return true;
}
}
public static String fileName1 = "a.txt";
public static String fileName2 = "b.txt";
@Test
public void DoTest(@Mocked File file1, @Mocked File file2) {
new Expectations() {
{
new File(fileName1);
result = file1;
new File(fileName2);
result = file2;
file1.exists();
result = false;
file2.exists();
result = false;
}
};
Assert.assertFalse(new File(fileName1).exists());
Assert.assertFalse(new File(fileName2).exists());
}
@Test
public void DoDummyTest(@Mocked DummyFile file1, @Mocked DummyFile file2) {
new Expectations() {
{
new DummyFile(fileName1);
result = file1;
new DummyFile(fileName2);
result = file2;
file1.exists();
result = false;
file2.exists();
result = false;
}
};
Assert.assertFalse(new DummyFile(fileName1).exists());
Assert.assertFalse(new DummyFile(fileName2).exists());
}
}
[RemoteTestNG] detected TestNG version 6.14.3
PASSED: DoDummyTest
FAILED: DoTest
Missing 1 invocation to:
java.io.File#exists()
on mock instance: java.io.File@17baae6e
Caused by: Missing invocations
at java.io.File.exists(File.java)
at com.ibm.watson.voyager.syncer.health.TestFileTest$1.<init>(TestFileTest.java:37)
at com.ibm.watson.voyager.syncer.health.TestFileTest.DoTest(TestFileTest.java:30)
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================
The text was updated successfully, but these errors were encountered:
Please provide the following information:
Version of JMockit that was used: 1.40. TestNG: 6.14.3
Description of the problem:
Using the pattern shown in the tutorial for "Instances created with a given constructor", I can't successfully mock separate instances of
java.io.File
whereas I can successfully mock a dummy class with the same constructor/method. The example included shows that a test fails when an expectation on theFile
instance is not met forfile1.exists();
due to a missing invocation but passes for theDummyFile
instance. I have attempted to reduce the code to the minimum amount to reproduce the problem. Apologies if I have made an elementary mistake - I'm new to JMockit. Code and results are below:The text was updated successfully, but these errors were encountered: