Mário Marques
Mário Marques

Reputation: 9

Unit testing: toString method. Possible override?

My professor gave us this homework exercise and has created a project with a bunch of unit tests. Our goal is to make sure we can pass those unit tests. We have two classes. A class called Building, that has a name, a minimum floor and a max floor and a class Room, that has a building, a name and a floor. I do not understand one of the test cases and how to make sure my program passes it.

I have already created the class building and room with its getters and setters.

This is the test our professor is gives


    @Test
    public void testToString() throws Exception {
        Building b = new Building("B", -1, 4);
        Room b104 = new Room(b, "104", 1);
        assertEquals("Building(B)", b + "");
        assertEquals("Room(B,104)", b104 + "");
}

This is what junit tells:

org.junit.ComparisonFailure: 
Expected :Building(B)
Actual   :Building@4e718207

org.junit.ComparisonFailure: 
Expected :Room(B,104)
Actual   :Room@4e718207

Upvotes: 0

Views: 1083

Answers (1)

embie27
embie27

Reputation: 104

In your classes Building and Room you have to override toString(). In this method you have to build a string according to the test your professor gave you and then return it.

Upvotes: 2

Related Questions