DriftingSands
DriftingSands

Reputation: 29

My toString junit test is failing, I have an almost identical toString junit test that runs fine, I don't know what the problem is

The toString method:

@Test

    public void testToString() {

        Floor f = new Floor("Ground Floor");
        String toStr = f.toString();
        
        assertTrue("The toString method should be in standard convention format",
        toStr.startsWith("Room:[") && 
        toStr.contains("=" + f.getFloorName() + ", ") &&
        toStr.endsWith("=" + f.getRoomsTotal() + "]"));
    }

The get methods:

public String getFloorName() {
        return floorName;
    }

public int getRoomsTotal() {
        return rooms.size();
    }

I don't understand why it's failing, Floor holds an arraylist of Room which Room has an almost identical toString junit test:

@Test

    public void testToString() {
        Room room = new Room("Bathroom", 1);
        String toStr = room.toString();
        
        assertTrue("The toString method should be in standard convention format",
        toStr.startsWith("Room:[") && 
        toStr.contains("=" + room.getRoomName() + ", ") &&
        toStr.endsWith("=" + room.getOutletCount() + "]"));
    }

The constructors:

/** The default constructor */

public Floor() {
    floorName = "";
    rooms = new ArrayList<>();
}

/** The custom constructor, create a new instance with user given values.
 * 
 * @param floorName Set the floor name.
 */
public Floor(String floorName) {
    this.floorName = floorName;
    rooms = new ArrayList<>();
}

The errors:

java.lang.AssertionError: The toString method should be in standard convention format
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at lib.FloorTest.testToString(FloorTest.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.Iterator.forEachRemaining(Unknown Source)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.forEach(Unknown Source)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:83)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:74)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

The override:

@Override
    public String toString() {
        return "Floor:[floorName=" + floorName + ", rooms=" + rooms + "]";
    }

What's going wrong?

Updated this post with the constructors of floor and the errors printed out from the testing.

Upvotes: 0

Views: 218

Answers (2)

Deepak Patankar
Deepak Patankar

Reputation: 3282

You are overring the toString() method in the Floor class. The toString creates a string of format

"Floor:[floorName=" + floorName + ", rooms=" + rooms + "]";

Whereas in your assert statement you are checking that the prefix is "Room:[". That's why your unit test is failing. Also, you are asserting on the room count, but you haven't set the room count in the constructor.

Here’s what I would recommend for this situation, you can use different assert statement for each check, this will then point you to the check which is failing.

Upvotes: 1

Abra
Abra

Reputation: 20914

In your testToString method that tests toString method of class Floor, the "test" instance of Floor contains no rooms. Hence, according to the code in your question, the toString method of class Floor returns the following:

Floor:[floorName=Ground Floor, rooms=[]]

Your toString method in class Floor does not return the number of rooms, so why do you test whether it ends with the number of rooms?

In order for your test code to work you need to either change the test code or change method toString in class Floor. For the latter, method toString in class Floor should be:

return "Floor:[floorName=" + floorName + ", rooms=" + getRoomsTotal() + "]";

Then for the test instance of Floor, the toString method would return:

Floor:[floorName=Ground Floor, rooms=0]

Upvotes: 0

Related Questions