如何测试写入Java文件? [英] How to test write to file in Java?

查看:197
本文介绍了如何测试写入Java文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,请保持自己的手。
我有一些简单的程序,我需要为写方法做junit测试。我有一些收集输入。我怎么能做到这一点?这是我的代码:

  //写入文件
public void write(String fileName,
List< FigureGeneral> figuresList){
try {
PrintWriter out = new PrintWriter(
new File(fileName).getAbsoluteFile());
(int i = 0; i< figureList.size(); i ++){
out.println(figuresList.get(i).toString());
}
} finally {
out.close();
}
} catch(IOException e){
System.out.println(Can not write to file!);






$ b我想知道,从文件,我们可以加入两个测试(写/读)或更好地单独做(如果我们的测试下降,我们不知道哪里是问题 - 在读或写)?如何在junit上做到这一点(准备测试和测试自己)?
更好的示例,这样更好地理解。



谢谢,纳扎尔。

解决方案

我建议你在你的IO类( PrintWriter 类在你的情况),所以你可以使用模拟对象输出。你不必测试Java PrintWriter ,你想测试你的功能,对吧?

将是

  class MyClass {

MyWriter out;

public void setOut(MyWriter out){
this.out = out;


写入文件
public void write(String fileName,List< FigureGeneral> figuresList){
try {
try {
for(int i = 0; i< figureList.size(); i ++){
out.println(figuresList.get(i).toString());
}
} finally {
out.close();
}
} catch(IOException e){
System.out.println(Can not write to file!);


$ b $ / code $ / pre
$ b $ <$ $的签名c $ c> MyWriter
接口非常简单。

 接口MyWriter {

void println(Object x); //你可以在这里添加其他的println方法。

void close();




$ b $ p
$ b

然后你可以使用


public void testWrite(){
MyWriter out = EasyMock.createMock(MyWriter.class);
EasyMock.expect(mock.println(EasyMock.anyObject()))。times(3);
EasyMock.expect(mock.close())。times(1);

清单<图一般> list = ...
list.add(...);
list.add(...);
list.add(...);

replay(mock);

MyClass myClass = new MyClass();
myClass.setOut(out);
myClass.write(mockFileName,list);

验证(模拟);
}


I'm beginner, and keep yourself in hands. I have some easy program, and I need do junit test for write method. I have some collection in input. How I can do this? This my code:

// write to file
public void write(String fileName, 
        List<FigureGeneral> figuresList) {
    try {
        PrintWriter out = new PrintWriter(
                new File(fileName).getAbsoluteFile());
        try {
            for (int i = 0; i < figuresList.size(); i++) {
                out.println(figuresList.get(i).toString());
            }
        } finally {
            out.close();
        }
    } catch (IOException e) {
        System.out.println("Cannot write to file!");
    }
}

And I want to know, coz after this I read from file, can we join both tests(write/read) or better do this individually(coz if our test fall we don't know where is problem - in read or write)? How should make this correctly at junit(with prepare to test, and test itself)? Better show on example, this way better to understand.

Thanks, Nazar.

解决方案

I'll suggest you making an interface wrapper around you IO classes (the PrintWriter class in your case) so you can use mock objects for output. You don't have to test Java PrintWriter, you want to test your functionality, right?

So your class will be

class MyClass {

    MyWriter out;

    public void setOut(MyWriter out) {
        this.out = out;
    }

    // write to file
    public void write(String fileName, List<FigureGeneral> figuresList) {
        try {
            try {
                for (int i = 0; i < figuresList.size(); i++) {
                    out.println(figuresList.get(i).toString());
                }
            } finally {
                out.close();
            }
        } catch (IOException e) {
            System.out.println("Cannot write to file!");
        }
    }
}

The signature of the MyWriter interface is pretty straightforward.

interface MyWriter {

    void println(Object x); // You can add other println methods here.

    void close();

}

Then you can use EasyMock to write a test. The test method will be something like

@Test
public void testWrite() {
    MyWriter out = EasyMock.createMock(MyWriter.class);
    EasyMock.expect(mock.println(EasyMock.anyObject())).times(3);
    EasyMock.expect(mock.close()).times(1);

    List<FigureGeneral> list = ...
    list.add(...);
    list.add(...);
    list.add(...);

    replay(mock);

    MyClass myClass = new MyClass();
    myClass.setOut(out);
    myClass.write("mockFileName", list);        

    verify(mock);
}

这篇关于如何测试写入Java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆