2011-09-29 4 views
0

Java 및 testng 테스트 케이스부터 시작합니다.java testng 테스트 케이스 작성

파일에서 데이터를 읽고 메모리 내 데이터 구조를 만들고 추가 데이터 처리를 위해이 데이터 구조를 사용하는 클래스를 작성해야합니다. 이 DS가 올바르게 채워지는지 테스트하고 싶습니다. DS를 파일로 덤핑하고 입력 파일을 덤프 된 파일과 비교할 것을 요구합니다. 파일 일치를 위해 사용 가능한 testNG 어설 션이 있습니까? 이것은 일반적인 관행입니까?

답변

0

개인적으로 나는 그 반대입니다. 물론 자바 환경에서 두 가지 데이터 구조를 비교할 수있는 방법이 필요합니다. 테스트에서 파일을 읽고, 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는 간단한 자바 빈이다. Apache Commons의 EqualsBuilder을 사용하여 2 개의 오브젝트를 비교할 수 있습니다.

3

필자는 쓰여진 데이터가 아닌 데이터 자체를 비교하는 것이 더 좋을 것이라고 생각합니다.

그래서이 데이터 구조를 반환하는 클래스의 메서드를 작성한 다음 (getDataStructure()이라고 부름) 올바른 데이터와 비교할 단위 테스트를 작성합니다.

이는 데이터 구조 클래스의 올바른 equals() 방법을 필요로하고 할

: 파일에 데이터 구조를 작성해야하는 경우

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

물론, 당신은 별도로 직렬화 직렬화를 테스트 할 수 있습니다 .

1

파일 비교/일치는 유틸리티 메소드 등으로 추출 할 수 있습니다. 당신은 단지 테스트를 위해 필요한 경우 이 당신이 시험 환경 외부에 비교 파일이 필요하면 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 
    } 
} 

선택을 사용할 수 있습니다 JUnit을 에 대한 애드온하지만이있는 유틸리티 클래스를 가진 재사용 할 수있는 기능이 더 나은 것입니다.

행운을 빈다.

0

은 파일 시스템에서로드 된 바이트를 비교하고 파일 시스템을 작성하려고 바이트

의사 코드

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

byte[] writeBytes = constructBytesFromDataStructure(dataStructure) 

Assert.assertTrue(java.util.Arrays.equals(writeBytes ,loadedBytes)); 
관련 문제