英文:
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("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.
答案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
"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.
答案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 "Floor:[floorName=" + floorName + ", rooms=" + getRoomsTotal() + "]";
Then for the test instance of Floor
, the toString
method would return:
Floor:[floorName=Ground Floor, rooms=0]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论