2013-03-05 2 views
1
public void createRootElement() throws FileNotFoundException, IOException 
    { 
    Properties prop = new Properties(); 
    prop.load(new FileInputStream("/home/asdf/Desktop/test.properties")); 
     File file = new File(prop.getProperty("filefromroot")); 
     try 
      { 
       // if file doesn't exists, then create it 
       if (!file.exists()) 
        { 
         file.createNewFile(); 
        } 
       FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
       BufferedWriter bw = new BufferedWriter(fw); 
       bw.write("<root>"); //create the root tag for the XML File. 
       bw.close(); 
      } 
     catch(Exception e) 
      { 
      writeLog(e.getMessage(),false); 
      } 
    } 

저는 junit 테스트를 처음 사용했습니다.이 테스트 케이스를 작성하는 방법을 모두 알고 싶습니다. 메서드 호출 방법은이 테스트에서 호출됩니다.하나의 테스트에서 메소드가 호출 될 때 junit 테스트

import static org.junit.Assert.assertTrue; 
import org.junit.Test; 

public class ClassToBeTestedTest { 

    @Test 
    public void test() { 
     ClassToBeTested c = new ClassToBeTested(); 
     c.createRootElement(); 
     assertTrue(c.rootElementExists()); 
    } 

} 

당신은 @Test 주석과 시험 방법을 표시하고 테스트 할 것을 실행하는 코드를 작성 :

+0

시작해야합니다. http://junit.sourceforge.net/doc/faq/faq.htm – Aboutblank

답변

2

JUnit의 테스트 케이스는 다음과 같이한다.

이 예제에서는 클래스의 인스턴스를 만들고 createRootElement 메서드를 호출했습니다.

그 후, 나는 모든 것이 예상대로 작동하는지 확인하기 위해 주장을했다.

당신이 주장 할 수있는 많은 것들이 있습니다. 자세한 정보는 JUnit 문서를 읽으십시오.

실제로 코드를 작성하기 전에 테스트를 작성하는 것이 좋습니다. 따라서이 테스트는 더 나은 코드를 작성하는 방법을 안내합니다. 이를 TDD라고합니다. 그것을위한 Google.

관련 문제