JMockit: Mocking same class twice in same test case

Below is my test class. In which I mock File class for 2 times. but it is giving error at second mock of File class.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class MyClass {
public void myMethod() {
String filePath = "";
filePath = Utility.getFilePath();
File jsonfile = new File(filePath);
if (!jsonfile.exists()) {
String newXmlPath = filePath.replace(".json", ".xml");
File xmlFile = new File(newXmlPath);
if (xmlFile.exists()) {
try(InputStream inputStream = new FileInputStream(xmlFile)) {
// file operation
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
} else {
if (jsonfile.exists()) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(jsonfile);
} catch (FileNotFoundException e) {
}
DataInputStream dis = new DataInputStream(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dis));
// BufferedReader operation
}
}
}
}
</code>
<code>public class MyClass { public void myMethod() { String filePath = ""; filePath = Utility.getFilePath(); File jsonfile = new File(filePath); if (!jsonfile.exists()) { String newXmlPath = filePath.replace(".json", ".xml"); File xmlFile = new File(newXmlPath); if (xmlFile.exists()) { try(InputStream inputStream = new FileInputStream(xmlFile)) { // file operation } catch (FileNotFoundException e) { } catch (IOException e) { } } } else { if (jsonfile.exists()) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(jsonfile); } catch (FileNotFoundException e) { } DataInputStream dis = new DataInputStream(fileInputStream); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dis)); // BufferedReader operation } } } } </code>
public class MyClass {

    public void myMethod() {

        String filePath = "";

        filePath = Utility.getFilePath();

        File jsonfile = new File(filePath);

        if (!jsonfile.exists()) {
            String newXmlPath = filePath.replace(".json", ".xml");
            File xmlFile = new File(newXmlPath);
            if (xmlFile.exists()) {
                try(InputStream inputStream = new FileInputStream(xmlFile)) {
                    // file operation
                } catch (FileNotFoundException e) {

                } catch (IOException e) {
                }
            }
        } else {
            if (jsonfile.exists()) {
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(jsonfile);
                } catch (FileNotFoundException e) {

                }
                DataInputStream dis = new DataInputStream(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dis));

                // BufferedReader operation
            }
        }

    }

}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class MyClassTest {
MyClass myClass = new MyClass();
@Test
public void myMethodTest() {
new MockUp<Utility>() {
@Mock
public String getFilePath() {
return "src/test/resources/file.json";
}
};
new MockUp<FileInputStream>() {
@Mock
public void $init() {
}
};
new MockUp<File>() {
private String name;
@Mock
public void $init(String name) {
this.name = name;
}
@Mock
boolean exists() {
if(this.name.contains(".json")) {
return false;
} else {
return true;
}
}
};
myClass.myMethod();
// Line no - 53
new MockUp<File>() {
private String name;
@Mock
public void $init(String name) {
this.name = name;
}
@Mock
boolean exists() {
if(this.name.contains(".json")) {
return true;
} else {
return false;
}
}
};
new MockUp<BufferedReader>() {
@Mock
String readLine() throws IOException {
return null;
}
};
myClass.myMethod();
}
}
</code>
<code>public class MyClassTest { MyClass myClass = new MyClass(); @Test public void myMethodTest() { new MockUp<Utility>() { @Mock public String getFilePath() { return "src/test/resources/file.json"; } }; new MockUp<FileInputStream>() { @Mock public void $init() { } }; new MockUp<File>() { private String name; @Mock public void $init(String name) { this.name = name; } @Mock boolean exists() { if(this.name.contains(".json")) { return false; } else { return true; } } }; myClass.myMethod(); // Line no - 53 new MockUp<File>() { private String name; @Mock public void $init(String name) { this.name = name; } @Mock boolean exists() { if(this.name.contains(".json")) { return true; } else { return false; } } }; new MockUp<BufferedReader>() { @Mock String readLine() throws IOException { return null; } }; myClass.myMethod(); } } </code>
public class MyClassTest {

    MyClass myClass = new MyClass();

    @Test
    public void myMethodTest() {

        new MockUp<Utility>() {
            @Mock
            public String getFilePath() {
                return "src/test/resources/file.json";
            }
        };

        new MockUp<FileInputStream>() {
            @Mock
            public void $init() {

            }
        };

        new MockUp<File>() {
            private String name;

            @Mock
            public void $init(String name) {
                this.name = name;
            }

            @Mock
            boolean exists() {
                if(this.name.contains(".json")) {
                    return false;
                } else {
                    return true;
                }
            }
        };

        myClass.myMethod();

        // Line no - 53
        new MockUp<File>() {
            private String name;

            @Mock
            public void $init(String name) {
                this.name = name;
            }

            @Mock
            boolean exists() {
                if(this.name.contains(".json")) {
                    return true;
                } else {
                    return false;
                }
            }
        };

        new MockUp<BufferedReader>() {
            @Mock
            String readLine() throws IOException {
                return null;
            }
        };

        myClass.myMethod();
    }
}

Now, every time I run this test it is giving below error.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>java.lang.NullPointerException
at java.base/java.io.UnixFileSystem.getBooleanAttributes0(Native Method)
at java.base/java.io.UnixFileSystem.hasBooleanAttributes(UnixFileSystem.java:194)
at java.base/java.io.File.isDirectory(File.java:867)
at java.base/java.net.URL.openStream(URL.java:1325)
at java.base/java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1764)
at com.testing.code.MyClassTest$123.<init>(MyClassTest.java:53)
at com.testing.code.MyClassTest.myMethodTest(MyClassTest.java:53)
</code>
<code>java.lang.NullPointerException at java.base/java.io.UnixFileSystem.getBooleanAttributes0(Native Method) at java.base/java.io.UnixFileSystem.hasBooleanAttributes(UnixFileSystem.java:194) at java.base/java.io.File.isDirectory(File.java:867) at java.base/java.net.URL.openStream(URL.java:1325) at java.base/java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1764) at com.testing.code.MyClassTest$123.<init>(MyClassTest.java:53) at com.testing.code.MyClassTest.myMethodTest(MyClassTest.java:53) </code>
java.lang.NullPointerException
    at java.base/java.io.UnixFileSystem.getBooleanAttributes0(Native Method)
    at java.base/java.io.UnixFileSystem.hasBooleanAttributes(UnixFileSystem.java:194)
    at java.base/java.io.File.isDirectory(File.java:867)
    at java.base/java.net.URL.openStream(URL.java:1325)
    at java.base/java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1764)
    at com.testing.code.MyClassTest$123.<init>(MyClassTest.java:53)
    at com.testing.code.MyClassTest.myMethodTest(MyClassTest.java:53)

I don’t know what I am doing wrong.

I want to know can we mock same class in same test case multiple times. And If yes then what are the things we need to take care of and If no then what are other alternatives to resolve this problem?

Also, I don’t want to use Mockito, PowerMockito like frameworks and don’t want to use actual file to in this test case.

Thank you in advance.

New contributor

harshil-padasala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật