简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM