接收tdd url测试的空输出

时间:2014-04-16 20:12:48

标签: java unit-testing junit tdd

当我对我的项目运行junit测试时,我在尝试测试我的项目可以正确构建URL时收到以下错误。我不确定我在下面做错了的是失败的测试运行的跟踪以及distancematrixconnection类和测试类。在尝试编译url字符串时产生空白输出。

org.junit.ComparisonFailure:expected:< [http://maps.googleapis.com/maps/api/distancematrix/xml?origins=albany&destinations=albany%20in&language=en-EN&sensor=false&language=en-EN&units=imperial]>但是:< []>     在org.junit.Assert.assertEquals(Assert.java:115)     在org.junit.Assert.assertEquals(Assert.java:144)     在edu.bsu.cs222.gascalculator.tests.GoogleUrlTests.testAlbanyNYtoAlbanyINURL(GoogleUrlTests.java:26)

public class GoogleDistanceMatrixConnection
{
String startLocation;
String endLocation;
final String urlString = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + startLocation +"&destinations=" + endLocation +"&language=en-EN&sensor=false&language=en-EN&units=imperial";


private static String XMLFile;

public String makeXMLFile(String start, String end) throws IOException
{
    startLocation = start;
    endLocation = end;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    connection.connect();

    BufferedReader reader = new BufferedReader( new InputStreamReader(
            connection.getInputStream()));

    for(String line = reader.readLine(); line != null; line = 
            reader.readLine())
    {
        setXMLFile(line);
    }

    return getXMLFile();
}
//  public static void main(String[] args) throws IOException{
//      GoogleDistanceMatrixConnection c = new GoogleDistanceMatrixConnection();
//  }
public static String getXMLFile() {
    return XMLFile;
}
public static void setXMLFile(String xMLFile) {
    XMLFile = xMLFile;
}

public boolean doesPageExist() {
    if(XMLFile == null)
        return true;
    else    
        return false;
}
}

public class GoogleUrlTests {
private GoogleDistanceMatrixConnection urlString = new GoogleDistanceMatrixConnection();
private String generatedUrl = "";
private String actualUrl = "";

@Test
public void testAlbanyNYtoAlbanyINURL() throws IOException {
    generatedUrl = urlString.makeXMLFile("albany", "albany+in");
    actualUrl = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=albany&destinations=albany%20in&language=en-EN&sensor=false&language=en-EN&units=imperial";
    Assert.assertEquals(actualUrl, generatedUrl);
}

@Test
public void testLosAngelesToNewYorkURL() throws IOException {

    generatedUrl = urlString.makeXMLFile("losangeles", "newyork");
    actualUrl = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=losangeles&destinations=newyork&language=en-EN&sensor=false&language=en-EN&units=imperial";
    Assert.assertEquals(actualUrl, generatedUrl);
}

}

1 个答案:

答案 0 :(得分:1)

makeXMLFile中比较您的测试用例和代码,我对您在此处尝试做的事情感到困惑。

如果你想通过测试,那么我认为这段代码会为你做。您可以使用URLEncoder正确编码您的网址字符串。

public class GoogleDistanceMatrixConnection
{

    public String makeXMLFile(String start, String end) throws IOException
    {
        return "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + URLEncoder.encode(start) +"&destinations=" + URLEncoder.encode(end) +"&language=en-EN&sensor=false&language=en-EN&units=imperial";    
    }
}

否则,您需要澄清您的问题。