我正在尝试为“Book”类编写一些测试代码,该类从其他类获取和设置书籍信息:ISBN(字符串),标题(字符串),作者(字符串),publishDate(SimpleDateFormat“MMM.dd. yyy“),Price(Double)和Summary content(Blob)。当我尝试对全部或部分代码运行toString测试时(我已经更改了应该无数次设置的格式和字符串):
@Test
public void testToString() {
Book testBook6 = new Book();
Title testTitle6 = new Title("Title: Moby Dick, ");
Author testAuthor6 = new Author("Author: Mark Twain, ");
ISBN testISBN6 = new ISBN("ISBN: 000-00-000-0000-0");
testBook6.setTitle(testTitle6);
testBook6.setAuthor(testAuthor6);
testBook6.setISBN(testISBN6);
assertEquals("Title: Moby Dick, Author: Mark Twain, ISBN: 000-00-000-0000-0", testBook6.toString());
}
我收到错误:
org.junit.ComparisonFailure: expected:<[Title: Moby Dick, Author: Mark Twain, ISBN: 000-00-000-0000-0]> but was:<[book.application.Book@48533e64]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at book.application.BookTest.testToString(BookTest.java:61)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
当我尝试在我的剪贴簿中运行代码时:
Book testBook = new Book();
ISBN testISBN = new ISBN("ISBN: 000-00-000-0000-0");
Title testTitle = new Title("Title: Moby Dick, ");
Author testAuthor = new Author("Author: Mark Twain, ");
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("MMMM.dd.yyyy");
Price testPrice = new Price(2.25);
Blob testBlob = new Blob("Blah, Blah, Blah");
testBook.setISBN(testISBN);
testBook.setTitle(testTitle);
testBook.setAuthor(testAuthor);
testBook.setPrice(testPrice);
testBook.setBlob(testBlob);
System.out.println(testBook.toString());
要查看发生了什么,我只能返回“book.application.Book@1794d431”,每次运行时都会随机更改数字(例如1794d431)。所有测试都通过“Book”获取和设置的每个类,并且没有标记错误。这只是我的第二个Java项目,所以我对此很陌生;但是,我似乎已经撞墙了,当然可以使用一些帮助!
提前致谢!
package book.application;
import java.time.LocalDate;
public class Book {
//fields
private ISBN ISBN;
private Title Title;
private Author Author;
private LocalDate publishDate;
private Price Price;
private Blob Blob;
//getters and setters
public LocalDate getPublishDate() {
return publishDate;
}
public void setPublishDate(LocalDate publishDate) {
this.publishDate = publishDate;
}
public ISBN getISBN() {
return ISBN;
}
public void setISBN(ISBN ISBN) {
this.ISBN = ISBN;
}
public Title getTitle() {
return Title;
}
public void setTitle(Title Title) {
this.Title = Title;
}
public Author getAuthor() {
return Author;
}
public void setAuthor(Author Author) {
this.Author = Author;
}
public Price getPrice() {
return Price;
}
public void setPrice(Price Price) {
this.Price = Price;
}
public Blob getBlob() {
return Blob;
}
public void setBlob(Blob Blob) {
this.Blob = Blob;
}
@Override
public String toString() {
return "Title: " + this.getTitle().getTitle() + ", Author: " + this.getAuthor().getAuthor() + ", ISBN: " + this.getISBN().getISBN() + ", Published on: " + LocalDate.now() + ", costing: $" + this.getPrice().getPrice();
}
}
答案 0 :(得分:1)
错误表示您忘记覆盖正在测试的类的toString
方法。
转到Book
班级,添加toString
的实施方式:
public class Book {
... // things you already have
// Add an implementation:
@Override
public String toString() {
// Add code constructing the output to satisfy your unit test
}
}
答案 1 :(得分:1)
您需要覆盖Book类的toString方法。目前它使用默认的toString()函数,该函数返回对象类型和哈希码。
以下是您的方法的样子:
@Override
public String toString() {
return "Title: '" + this.title + "', Author: '" + this.author + "', ISBN: '" + this.isbn + "'";
}
如果您不清楚这意味着什么,check out the API for the Object class。在Java中,所有类都扩展了Object,因此您可以从此类访问和覆盖(替换)许多方法。 ToString就是这样一种方法,并且此方法的默认行为不是您所期望的,因此在Java中创建新类时,覆盖toString()
是很常见的。您可能想要查看的另一种方法是equals()
。
答案 2 :(得分:0)
您的toString()方法似乎未实现,并且具有默认的if($user->type == "SUPPORT"){
include('path/to/support/template.php');
} elseif($user->type == "CUSTOMER"){
include('path/to/customer/template.php');
} else {
//Default catch bad types?
}
版本。如果您发布了CREATE TABLE `USERS` (
`ID` int(10) NOT NULL PK AUTOINCREMENT,
`TYPEID` int(10) NOT NULL,
`NAME` varchar(99) NOT NULL,
`EMAIL` varchar(99) NOT NULL,
`PASSWORD` text NOT NULL,
)
ALTER TABLE `USERS`
ADD CONSTRAINT `USERS_ibfk_1` FOREIGN KEY (`TYPEID`) REFERENCES `TYPES` (`ID`)
代码,我们可以为您提供进一步的帮助。
此外,为了比较字符串,通常最好做Object