测试 toString Java 代码时出错

Errors testing toString Java code

我正在尝试为 "Book" class 编写一些测试代码,从其他 classes 获取和设置图书信息:ISBN(字符串)、标题(字符串)、作者 (String)、发布日期 (SimpleDateFormat "MMM.dd.yyy")、价格 (Double) 和摘要内容 (Blob)。当我尝试 运行 对全部或部分代码(我有改了无数次应该设置的format和String):

@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.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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" 的 return,每次我 运行 时数字(例如 1794d431)都会随机变化。 "Book" 从中获取和设置的每个 class 的所有测试都通过了,并且没有标记错误。这只是我的第二个 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();
    }

}

该错误表示您忘记覆盖正在测试的 class 的 toString 方法。

转到您的 Book class,并添加 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
    }
}

您需要重写 Book 上的 toString 方法 class。目前它使用默认的 toString() 功能,其中 returns 对象类型和哈希码。

您的方法可能如下所示:

@Override
public String toString() { 
    return "Title: '" + this.title + "', Author: '" + this.author + "', ISBN: '" + this.isbn + "'";
} 

如果您不清楚这意味着什么,check out the API for the Object class。在 Java 中,所有 class 都扩展了 Object,因此您可以访问和覆盖(替换)此 class 中的许多方法。 ToString 就是这样一种方法,该方法的默认行为不是您所期望的,因此在 Java 中创建新的 class 时覆盖 toString() 是很常见的。您可能想要查看的另一种方法是 equals().

您的 toString() 方法似乎未实现并且具有默认的 Object 版本。如果您 post 您的 Book 代码,我们可以进一步帮助您。

此外,为了比较字符串,通常最好做 AssertTrue(string1.Equals(string2))