首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写java测试用例

编写java测试用例
EN

Stack Overflow用户
提问于 2011-09-29 16:13:35
回答 5查看 1.4K关注 0票数 1

我从Java和testng测试用例开始。

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

EN

回答 5

Stack Overflow用户

发布于 2011-09-29 16:17:54

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

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

这只需要在数据结构类中使用正确的equals()方法:

代码语言:javascript
复制
Assert.assertEquals(yourClass.getDataStructure(), correctData);

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

票数 3
EN

Stack Overflow用户

发布于 2011-09-29 16:25:44

可以将文件比较/匹配提取为实用方法或类似的方法。如果您只需要它来进行测试,可以使用jUnit http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html插件

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

代码语言:javascript
复制
    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
    }
}

这是您的选择,但是拥有一个具有可重用功能的实用程序类会更好。

祝你好运,玩得开心。

票数 1
EN

Stack Overflow用户

发布于 2011-09-29 16:16:53

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

(使用JUnit4)

代码语言:javascript
复制
@Test
public void testProcessingDoesWhatItShould() {
    final DataStructure original = readFromFile(filename);
    final DataStructure actual = doTheProcessingYouNeedToDo(original);
    final DataStructure expected = generateMyExpectedResult();

    Assert.assertEquals("data structure", expected, actual);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7594316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档