2014-08-28 44 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"); 
} 
相关问题