Question

When I run junit tests on my project I receive the following error when trying to test that my project can build a url correctly. I am not sure what I am doing wrong below is the trace of the failed test run as well as the distancematrixconnection class and test class. It is producing a blank output when trying to compile the url string.

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]> but was:<[]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at 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);
}

}

Était-ce utile?

La solution

Comparing your test cases and your code in makeXMLFile, I'm confused of what you are really trying to do here.

If you want to pass you tests, then I think this code will do that for you. You can use URLEncoder to properly encode your URL string.

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";    
    }
}

Otherwise, you need to clarify your question.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top