2014-08-28 42 views
0

我目前正在開發一個程序,它爲我的工作變得越來越廣闊。每次我想測試我建立我必須添加和刪除我的Main類的方法這樣做的一個新的Class。隨着程序的增長,這變得越來越麻煩。如何運行在Java中一個類/使用JUnit

我一直想寫JUnit試驗,我建我去檢查所需的方法,新的類。我看過的任何教程介紹如何編寫基本的測試方法來斷定方法返回一個預期的數學結果或布爾等,但是這不是我要找檢查。我已經看了一會兒,閱讀了一下,但我找不到任何東西來詳細說明如何編寫自定義測試方法,比如我正在尋找的方法。

考慮下面的類:

loadFromFile()方法的情況下,預期的結果將是,它已經成功加載一個文本文件,並解析它變成一個Map<String, String>,但我無法弄清楚如何去做這個。

當完全完成時,我的checkDuplicates()方法的當前預期結果將是交叉檢查一個數據列表,該數據列表將在創建對象時傳遞給構造函數,並與其他列表中的數據相互移除。我還沒有完全完成這個方法,因爲我希望確保在每一個階段我都能正確地編寫它的工作。

我怎麼會寫這種方法的?

package com.airport.twitter; 

import java.io.*; 
import java.util.*; 

public class FileHandler { 
    private Map<String, String> arrivalsMap = new TreeMap<String, String>(); 
    private Map<String, String> departuresMap = new TreeMap<String, String>(); 
    private ArrayList<String> arrivals = new ArrayList<String>(); 
    private ArrayList<String> departures = new ArrayList<String>(); 
    private String arrLocation, depLocation; 

    public FileHandler(ArrayList<String> arrivals, ArrayList<String> departures) { 
     this.arrivals = arrivals; 
     this.departures = departures; 
     this.arrLocation = "src/main/resources/arrivals.txt"; 
     this.depLocation = "src/main/resources/departures.txt"; 
    } 

    public Map<String, String> loadFromFile(Map<String, String> list, 
      String location) throws IOException { 
     String str = null; 
     BufferedReader br = new BufferedReader(new FileReader(location)); 
     while ((str = br.readLine()) != null) { 
      String[] parts = str.split("="); 
      list.put(parts[0], parts[1]); 
     } 
     br.close(); 
     return list; 
    } 

    public void checkDuplicateFlights() throws IOException { 
     arrivalsMap = loadFromFile(arrivalsMap, arrLocation); 
     ConsolePrinter.printMap(arrivalsMap); 
     System.out.println("\n"); 
     departuresMap = loadFromFile(departuresMap, depLocation); 
     ConsolePrinter.printMap(departuresMap); 
    } 
} 
+0

爲什麼你不能檢查結果相同的數學結果或布爾?只需使用assertEquals而不是斷言。 – Burkhard 2014-08-28 11:17:08

+0

也許它對我的Junit缺乏瞭解,但我不需要檢查方法中Map的內容需要等於說50行文字。我需要檢查是否已成功連接到該文件,並在正確的情況下將其正確加載到類中。我希望能用打印方法或其他方法查看內容。你是否建議類似assertNotNull()? – user3363286 2014-08-28 11:34:39

+0

如果找不到文件,您的loadFromFile將拋出IOException。如果結果如預期的那樣,你的程序完成它應該做的事情:即。如果找到文件,返回的映射是正確的,並且在找不到該文件時引發IOException(您至少需要2個UnitTests)。 – Burkhard 2014-08-28 11:41:59

回答

0

爲了測試你的loadFromFile,我將一個帶有一些值的文件檢查結果。由於您知道文件的內容,因此您知道該方法必須返回並可以對其進行測試。

例如,如果該文件包含:

x=1 
y=2 
z=3 

我會在這樣一個基於JUnit測試類運行此方法:

@Test 
public void testLoadFromFile() 
{ 
    //...init 
    FileHandler fh = new FileHandler(new ArrayList<>(), new ArrayList<>()); 
    result = fh.loadFromFile(known_path_to_File); 
    assertEquals(expectedMap, result); 
    // **here you can be sure that the file was read correctly!** 
} 

assertMap會比較兩張地圖。
expectedMap將包含X = 1,Y = 2和z = 3,可以這樣初始化:

private static final Map<String, String> expectedMap; 
static { 
    Map<String, String> aMap = new HashMap<>(); 
    aMap.put("x", "1"); 
    aMap.put("y", "2"); 
    aMap.put("z", "3"); 
    expectedMap = Collections.unmodifiableMap(aMap); 
} 

順便說一句:

void org.junit.Assert.assertEquals(Object expected, Object actual) 

所以你需要導入這樣的:

import static org.junit.Assert.assertEquals; 

你必須運行(失敗如果找不到該文件)第二次測試:

@Test(expected=IOException.class) 
public void testFileNotFound() 
{ 
    FileHandler fh = ...//same as previous 
    fh = loadFromFile("no real file here.txt"); 
} 
相關問題