2016-10-28 6 views
0

ISBN(String)、Title(String)、Author(String)、publishDate(SimpleDateFormat)などの他のクラスから書籍情報を取得して設定する「Book」クラスのテストコードを作成しようとしています。 "MMM.dd.yyy")、Price(Double)、およびSummaryコンテンツ(Blob)。コードの全部または一部に対してtoStringテストを実行しようとすると、回):toString Javaコードをテストする際のエラー

@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:<[[email protected]]> 
    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()); 

何が起こっているかを見るために、私は、ランダムに私はそれを実行するたびに変更(例えば1794d431など)数字で、「[email protected]」のリターンを得ることができます。すべてのテストは、 "ブック"が取得および設定するすべてのクラスに渡され、エラーはマークされません。これは私の2番目の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

正確に何を求めていますか? – ItamarG3

+0

あなたのBookクラスはどこですか? – developer

答えて

1

あなたBookクラスのtoStringメソッドをオーバーライドする必要があります。現在、オブジェクト型とハッシュコードを返すデフォルトのtoString()機能を使用しています。あなたは、これが何を意味するのかcheck out the API for the Object classをクリアしていない場合

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

:ここ

は、あなたの方法は次のようになりことができるものです。 Javaでは、すべてのクラスがObjectを継承しているため、このクラスの多くのメソッドにアクセスしてオーバーライド(置き換え)することができます。 ToStringはそのようなメソッドの1つで、このメソッドのデフォルトの動作は期待どおりではないため、Javaで新しいクラスを作成するときはtoString()をオーバーライドするのが一般的です。あなたが見たいかもしれない別の方法はequals()です。

+0

私はこのメソッドを見るために戻り、オーバーライドを追加しました。スクラップブックで実行すると正しい結果が得られます。 "タイトル:Moby Dick、著者:Mark Twain、ISBN:000-00-000-0000- 0、公開日:2016-10-28、原価計算:$ 2.25 ";しかし、@testを実行すると、私はまだ失敗します。私は今日のために時間を使い果たしましたが、今では私がクラスの作品で私が知っている、私は明日私のテストを見る必要があります。お手伝いありがとう! –

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 
    } 
} 
0

あなたのtoString()メソッドが実装されておらず、デフォルトのObjectバージョンを持っているようです。 Bookコードを投稿すると、さらにお手伝いできるようになります。

また、文字列を比較する場合は、通常は実行する方が良いですAssertTrue(string1.Equals(string2))

関連する問題