2011-09-29 93 views
0

我是从Java和testng测试用例开始的。写java testng测试用例

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

回答

0

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

(使用JUnit4)

@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

如果DS是一个简单的Java Bean。那么你可以使用Apache Commons的EqualsBuilder来比较2个对象。

3

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

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

这只需要在你的数据结构类正确equals()方法做:

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

当然,如果你需要将数据结构写入到一个文件,那么你可以测试序列化和反序列化分开。

1

文件比较/匹配可以提取到实用程序方法或类似的东西。 如果你需要它仅用于测试有针对JUnit 插件http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html

如果你需要的文件比较测试环境外,你可以使用这个简单的功能

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 
    } 
} 

您的选择,但与一个实用工具类那些可以重用的功能更好。

祝你好运,玩得开心。

0

比较从文件系统加载的字节,字节你要写入文件系统

伪代码

byte[] loadedBytes = loadFileContentFromFile(file) // maybe apache commons IOUtils.toByteArray(InputStream input) 

byte[] writeBytes = constructBytesFromDataStructure(dataStructure) 

Assert.assertTrue(java.util.Arrays.equals(writeBytes ,loadedBytes));