WarrenJB
WarrenJB

Reputation: 129

Java Testing for Maps

I've created a List<Map<String, String>> for a source file. I've succeeded in creating a Map with the header values as keys and the information below them as values. I've tested and verified everything in that method works.

In my next method I've implemented an iterator that will grab the next mapping for processing in a different class. I'm trying to test to make sure that it's doing what I think it's doing and to verify what I want it to do.

Here's the code of the method:

  public Map contact(List<Map<String, String>> list) {
return list.iterator().next(); 
}

And here's my test code in the testing class:

  @Test
public void contact() throws Exception {

List<Map<String, String>> list = this.main.sourceToMap(source1.readLine());
Map nextContact = this.main.contact(list);

System.out.println(list.get(0));

Assert.assertEquals(nextContact,
    "zip=70116, address=6649 N Blue Gum St, city=New Orleans, " +
        "web=http://www.bentonjohnbjr.com, company_name=\"Benton, John B Jr\", " +
        "county=Orleans, phone2=504-845-1427, last_name=Butt, state=LA, first_name=James, " +
        "[email protected], phone1=504-621-8927");
}

Finally, here's the error i'm getting:

java.lang.AssertionError: Expected :{zip=70116, address=6649 N Blue Gum St,city=New Orleans, web=http://www.bentonjohnbjr.com, company_name="Benton, John B Jr", county=Orleans, phone2=504-845-1427, last_name=Butt, state=LA, first_name=James, [email protected], phone1=504-621-8927}

Actual :zip=70116, address=6649 N Blue Gum St, city=New Orleans, web=http://www.bentonjohnbjr.com, company_name="Benton, John B Jr", county=Orleans, phone2=504-845-1427, last_name=Butt, state=LA, first_name=James, [email protected], phone1=504-621-8927

The expected and actual are exactly the same with one one difference. A "{ }" is in front and behind the string.

I think it's because it's expecting a Map instead of a String but I can't figure out what to type in to make it a Map instead of a string.

This is an assignment but not THE assignment. This is just one small test that i'm trying to prove before moving on.

Thanks! :D

Upvotes: 0

Views: 255

Answers (2)

Ivan Pronin
Ivan Pronin

Reputation: 1916

If you would like to compare in that way (whole map contents against the String) - consider two things:

  1. First argument in Assert.assertEquals(..) method is expected value. So, swap your arguments to match the contract
  2. Add curly braces to the expected value: Assert.assertEquals( "{zip=70116, address=6649 N Blue Gum St, city=New Orleans, " + "web=http://www.bentonjohnbjr.com, company_name=\"Benton, John B Jr\", " + "county=Orleans, phone2=504-845-1427, last_name=Butt, state=LA, first_name=James, " + "[email protected], phone1=504-621-8927}", nextContact.toString());
    1. Add toString() method call to the map object so that both objects would have the same type

Upvotes: 0

Andr&#233; Stannek
Andr&#233; Stannek

Reputation: 7863

You assertion is comparing a Map with a String. This fails of course, because the two aren't even the same type. In the error message you see a textual representation of your nextContact object. This seems to be created by invoking the toString() method of Map, which produces the result with the {} around the content.

To fix this you should compare the actual values in the map, e.g.

Assert.assertEquals(nextContact.get("zip"), "70116");

Upvotes: 3

Related Questions