Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instances created with a given constructor not working for java.io.File #533

Closed
rjh1970 opened this issue Jul 25, 2018 · 0 comments
Closed

Instances created with a given constructor not working for java.io.File #533

rjh1970 opened this issue Jul 25, 2018 · 0 comments
Assignees
Labels

Comments

@rjh1970
Copy link

@rjh1970 rjh1970 commented Jul 25, 2018

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 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
===============================================
@rliesenfeld rliesenfeld self-assigned this Jul 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.