编写JavaTestNG测试用例

编写JavaTestNG测试用例,java,unit-testing,testng,Java,Unit Testing,Testng,我从Java和testng测试用例开始 我需要编写一个类,它从文件中读取数据,生成内存中的数据结构,并使用该数据结构进行进一步处理。我想测试一下,这个DS是否被正确填充。这将要求将DS转储到一个文件中,然后将输入文件与转储文件进行比较。是否有任何testNG断言可用于文件匹配?这是一种常见的做法吗?就我个人而言,我会做相反的事情。当然,您需要一种方法来比较Java世界中的这两种数据结构——因此测试将从文件中读取数据,构建DS,进行处理,然后断言它等于您在测试中设置的“预期”DS (使用JUnit

我从Java和testng测试用例开始


我需要编写一个类,它从文件中读取数据,生成内存中的数据结构,并使用该数据结构进行进一步处理。我想测试一下,这个DS是否被正确填充。这将要求将DS转储到一个文件中,然后将输入文件与转储文件进行比较。是否有任何testNG断言可用于文件匹配?这是一种常见的做法吗?

就我个人而言,我会做相反的事情。当然,您需要一种方法来比较Java世界中的这两种数据结构——因此测试将从文件中读取数据,构建DS,进行处理,然后断言它等于您在测试中设置的“预期”DS

(使用JUnit4)


就我个人而言,我会做相反的事情。当然,您需要一种方法来比较Java世界中的这两种数据结构——因此测试将从文件中读取数据,构建DS,进行处理,然后断言它等于您在测试中设置的“预期”DS

(使用JUnit4)


如果这个DS是一个简单的Javabean。然后您可以使用ApacheCommons来比较两个对象。

如果这个DS是一个简单的Java Bean。然后您可以使用ApacheCommons来比较两个对象。

我认为比较数据本身更好,而不是比较写出的数据

因此,我将在类中编写一个方法来返回此数据结构(我们称之为
getDataStructure()
),然后编写一个单元测试来与正确的数据进行比较

这只需要在数据结构类中使用正确的
equals()
方法,并执行以下操作:

Assert.assertEquals(yourClass.getDataStructure(), correctData);

当然,如果需要将数据结构写入文件,则可以分别测试序列化和反序列化。

我认为比较数据本身而不是写入的数据会更好

因此,我将在类中编写一个方法来返回此数据结构(我们称之为
getDataStructure()
),然后编写一个单元测试来与正确的数据进行比较

这只需要在数据结构类中使用正确的
equals()
方法,并执行以下操作:

Assert.assertEquals(yourClass.getDataStructure(), correctData);

当然,如果需要将数据结构写入文件,则可以分别测试序列化和反序列化。

可以将文件比较/匹配提取到实用程序方法或类似方法。 如果您只在测试时需要它,那么有一些jUnit插件

如果需要在测试环境之外进行文件比较,可以使用这个简单的函数

    public static boolean fileContentEquals(String filePathA, String filePathB) throws Exception {
    if (!compareFilesLength(filePathA, filePathB)) return false;

    BufferedInputStream streamA = null;
    BufferedInputStream streamB = null;
    try {
        File fileA = new File(filePathA);
        File fileB = new File(filePathB);

        streamA = new BufferedInputStream(new FileInputStream(fileA));
        streamB = new BufferedInputStream(new FileInputStream(fileB));

        int chunkSizeInBytes = 16384;
        byte[] bufferA = new byte[chunkSizeInBytes];
        byte[] bufferB = new byte[chunkSizeInBytes];

        int totalReadBytes = 0;
        while (totalReadBytes < fileA.length()) {
            int readBytes = streamA.read(bufferA);
            streamB.read(bufferB);

            if (readBytes == 0) break;

            MessageDigest digestA = MessageDigest.getInstance(CHECKSUM_ALGORITHM);
            MessageDigest digestB = MessageDigest.getInstance(CHECKSUM_ALGORITHM);

            digestA.update(bufferA, 0, readBytes);
            digestB.update(bufferB, 0, readBytes);

            if (!MessageDigest.isEqual(digestA.digest(), digestB.digest()))
            {
                closeStreams(streamA, streamB);
                return false;
            }

            totalReadBytes += readBytes;
        }
        closeStreams(streamA, streamB);
        return true;
    } finally {
        closeStreams(streamA, streamB);
    }
}

public static void closeStreams(Closeable ...streams) {
    for (int i = 0; i < streams.length; i++) {
        Closeable stream = streams[i];
        closeStream(stream);
    }
}
public static boolean compareFilesLength(String filePathA, String filePathB) {
    File fileA = new File(filePathA);
    File fileB = new File(filePathB);

    return fileA.length() == fileB.length();
}
private static void closeStream(Closeable stream) {
    try {
        stream.close();
    } catch (IOException e) {
        // ignore exception
    }
}
public静态布尔fileContentEquals(stringfilepatha,stringfilepathb)引发异常{
如果(!CompareFileLength(filePathA,filePathB))返回false;
BufferedInputStream streamA=null;
BufferedInputStream streamB=null;
试一试{
文件fileA=新文件(filePathA);
文件fileB=新文件(filePathB);
streamA=新的BufferedInputStream(新的FileInputStream(fileA));
streamB=新的BufferedInputStream(新的FileInputStream(fileB));
int chunkSizeInBytes=16384;
byte[]bufferA=新字节[chunkSizeInBytes];
byte[]bufferB=新字节[chunkSizeInBytes];
int totalReadBytes=0;
while(totalReadBytes
您可以选择,但是拥有一个具有可重用功能的实用程序类更好


祝你好运,玩得开心。

文件比较/匹配可以提取到实用方法或类似方法。 如果您只在测试时需要它,那么有一些jUnit插件

如果需要在测试环境之外进行文件比较,可以使用这个简单的函数

    public static boolean fileContentEquals(String filePathA, String filePathB) throws Exception {
    if (!compareFilesLength(filePathA, filePathB)) return false;

    BufferedInputStream streamA = null;
    BufferedInputStream streamB = null;
    try {
        File fileA = new File(filePathA);
        File fileB = new File(filePathB);

        streamA = new BufferedInputStream(new FileInputStream(fileA));
        streamB = new BufferedInputStream(new FileInputStream(fileB));

        int chunkSizeInBytes = 16384;
        byte[] bufferA = new byte[chunkSizeInBytes];
        byte[] bufferB = new byte[chunkSizeInBytes];

        int totalReadBytes = 0;
        while (totalReadBytes < fileA.length()) {
            int readBytes = streamA.read(bufferA);
            streamB.read(bufferB);

            if (readBytes == 0) break;

            MessageDigest digestA = MessageDigest.getInstance(CHECKSUM_ALGORITHM);
            MessageDigest digestB = MessageDigest.getInstance(CHECKSUM_ALGORITHM);

            digestA.update(bufferA, 0, readBytes);
            digestB.update(bufferB, 0, readBytes);

            if (!MessageDigest.isEqual(digestA.digest(), digestB.digest()))
            {
                closeStreams(streamA, streamB);
                return false;
            }

            totalReadBytes += readBytes;
        }
        closeStreams(streamA, streamB);
        return true;
    } finally {
        closeStreams(streamA, streamB);
    }
}

public static void closeStreams(Closeable ...streams) {
    for (int i = 0; i < streams.length; i++) {
        Closeable stream = streams[i];
        closeStream(stream);
    }
}
public static boolean compareFilesLength(String filePathA, String filePathB) {
    File fileA = new File(filePathA);
    File fileB = new File(filePathB);

    return fileA.length() == fileB.length();
}
private static void closeStream(Closeable stream) {
    try {
        stream.close();
    } catch (IOException e) {
        // ignore exception
    }
}
public静态布尔fileContentEquals(stringfilepatha,stringfilepathb)引发异常{
如果(!CompareFileLength(filePathA,filePathB))返回false;
BufferedInputStream streamA=null;
BufferedInputStream streamB=null;
试一试{
文件fileA=新文件(filePathA);
文件fileB=新文件(filePathB);
streamA=新的BufferedInputStream(新的FileInputStream(fileA));
streamB=新的BufferedInputStream(新的FileInputStream(fileB));
int chunkSizeInBytes=16384;
byte[]bufferA=新字节[chunkSizeInBytes];
byte[]bufferB=新字节[chunkSizeInBytes];
int totalReadBytes=0;
while(totalReadBytes