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

huangapple go评论80阅读模式
英文:

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

问题

Here's the translated content you requested:


The toString method:

@Test
public void testToString() {
    Floor f = new Floor("Ground Floor");
    String toStr = f.toString();
    
    assertTrue("toString方法应符合标准约定格式",
    toStr.startsWith("Floor:[floorName=") &&
    toStr.contains("=" + f.getFloorName() + ", ") &&
    toStr.endsWith("rooms=" + 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, and Room has an almost identical toString JUnit test:

@Test
public void testToString() {
    Room room = new Room("Bathroom", 1);
    String toStr = room.toString();
    
    assertTrue("toString方法应符合标准约定格式",
    toStr.startsWith("Room:[") &&
    toStr.contains("=" + room.getRoomName() + ", ") &&
    toStr.endsWith("outletCount=" + room.getOutletCount() + "]"));
}

The constructors:

/** 默认构造函数 */
public Floor() {
    floorName = "";
    rooms = new ArrayList<>();
}

/** 自定义构造函数,使用用户提供的值创建新实例。
 * 
 * @param floorName 设置楼层名称。
 */
public Floor(String floorName) {
    this.floorName = floorName;
    rooms = new ArrayList<>();
}

The errors:

java.lang.AssertionError: toString方法应符合标准约定格式
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at lib.FloorTest.testToString(FloorTest.java:65)
    ...

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.


英文:

The toString method:

@Test

	public void testToString() {

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

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(&quot;Bathroom&quot;, 1);
		String toStr = room.toString();
		
		assertTrue(&quot;The toString method should be in standard convention format&quot;,
		toStr.startsWith(&quot;Room:[&quot;) &amp;&amp; 
		toStr.contains(&quot;=&quot; + room.getRoomName() + &quot;, &quot;) &amp;&amp;
		toStr.endsWith(&quot;=&quot; + room.getOutletCount() + &quot;]&quot;));
	}

The constructors:

/** The default constructor */

public Floor() {
	floorName = &quot;&quot;;
	rooms = new ArrayList&lt;&gt;();
}

/** 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&lt;&gt;();
}

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 &quot;Floor:[floorName=&quot; + floorName + &quot;, rooms=&quot; + rooms + &quot;]&quot;;
    }

What's going wrong?

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

答案1

得分: 1

你正在Floor类中重写toString()方法。toString方法创建了一个格式为:

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

然而,在你的断言语句中,你检查的前缀是"Room:[. 这就是为什么你的单元测试失败的原因。另外,你在断言中验证了房间数量,但是你没有在构造函数中设置房间数量。

针对这种情况,我建议你可以为每个检查使用不同的断言语句,这样可以指出哪个检查失败了。

英文:

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

&quot;Floor:[floorName=&quot; + floorName + &quot;, rooms=&quot; + rooms + &quot;]&quot;;

Whereas in your assert statement you are checking that the prefix is &quot;Room:[&quot;. 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.

答案2

得分: 0

在你的testToString方法中测试了Floor类的toString方法,Floor类的“test”实例不包含任何房间。因此,根据你问题中的代码,Floor类的toString方法返回如下内容:

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

你在Floor类的toString方法中没有返回房间数量,那么为什么要测试它是否以房间数量结尾呢?

为了使你的测试代码有效,你需要更改测试代码或更改Floor类中的toString方法。对于后者,Floor类中的toString方法应为:

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

然后对于Floor类的测试实例,toString方法将返回:

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

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 &quot;Floor:[floorName=&quot; + floorName + &quot;, rooms=&quot; + getRoomsTotal() + &quot;]&quot;;

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

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

huangapple
  • 本文由 发表于 2020年8月14日 19:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定