对日志文件正确写入进行单元测试的最佳方法

时间:2019-01-16 15:09:11

标签: java unit-testing junit

我一般对单元测试和Java还是陌生的,我正在尝试测试信息是否正确写入日志文件。我目前进行的测试有效。但是,我不知道是否有更好/更合适的方法。谢谢!

这是我的记录器课程:

import java.io.*;
import java.time.LocalDateTime;

public class logger {

private String logFilename = null;

public logger(String filename)
{
    this.logFilename = filename;
}

public synchronized void print(String line)
{
    logger.print(logFilename, line, true);
}

public synchronized void println(String line)
{
    logger.println(logFilename, line, true);
}

public synchronized static void print(String file, String line, boolean append)
{

    try {
        FileOutputStream fout = new FileOutputStream(file, append);
        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(fout));
        if(line==null) line = "(null)";
        log.write(line);
        log.flush();
        log.close();

    }
    catch (IOException e) {
        System.out.println (e);
    }
}

public synchronized static void print(String szFile, String line)
{
    logger.print(szFile, line, true);
}

public synchronized static void println(String szFile, String line)
{
    logger.println(szFile, line, true);
}

public synchronized static void println(String file, String line, boolean append)
{
    try {
        FileOutputStream fout = new FileOutputStream(file, append);
        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(fout));

        long threadId = Thread.currentThread().getId();
        log.write(LocalDateTime.now().toString());
        log.write(" THREAD:[" + threadId + "] ");
        log.write(line + "\r\n");
        log.flush();
        log.close();

    }
    catch (Exception e) {
        e.printStackTrace();
        System.out.println (e);
    }
}
public static PrintWriter getWriter(String file, boolean append) {
    FileOutputStream fout = null;
    try {
        fout = new FileOutputStream(file, append);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    PrintWriter log = null;
    if(fout != null) {
        log = new PrintWriter(fout);
    }
    return log;
}
}

这是我的主要文件和测试文件的内容:

主要

public class Main {

private static logger log = new logger("filename1234.log");

public static void main(String[] args) {
}

public static void foo(String uid) {
    log.println("Event triggered by: " + uid);
}

}

测试

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;

 class MainTest {

private static Main tester;
private static BufferedReader br;
private static FileInputStream  fstream;

@BeforeAll
public static void setUp() throws Exception{
    tester = new Main(); // MyClass is tested
    fstream = new FileInputStream("filename1234.log");
    br = new BufferedReader(new InputStreamReader(fstream));
}

@Test
void testFoo() throws Exception{
    String sCurrentLine;
    String lastLine = "";
    tester.foo("3040");
    /* read log line by line */
    while ((sCurrentLine = br.readLine()) != null)
    {
        System.out.println(sCurrentLine);
        lastLine = sCurrentLine;
    }
    fstream.close();

    assertTrue(lastLine.contains("3040"));
}
}

0 个答案:

没有答案