1

要件: Java コレクション フレームワークのコンポーネントの 1 つを使用して、複数の作成者に対応します。isbn と著者のコレクションを含む 1 冊の本が必要です。JUnit: testValidate のガイダンス: 少なくとも 2 つのケースをテストします (ブック プロパティが正しいデータ型を保持していて、空でも null 値も保持していないケースと、そうでないケースの 1 つ)。testEquals のガイダンス: 少なくとも 2 つのケース (作成者と isbn が一致するケースと一致しないケース) についてテストします。少なくとも 2 人の著者についてテストします。私の先生は私に言った: testEquals isbn と 2 人の著者を追加する必要があります。ArrayList を作成します。それに 2 人の著者を追加します。Book オブジェクトを作成し、ArrayList インスタンスと isbn を追加します。それが私がやったことだと思います.著者は印刷していますが、ISBNはそうではありません. 私はまったくの初心者で、途方に暮れています!誰でも助けることができますか?

編集/追加ISBN を印刷するようにしましたが、私が持っている 2 番目の ISBN しか印刷していません。両方を印刷するには、何を変更する必要がありますか? それとも関係ありますか?

出力は次のとおりです。

Testsuite: library.domain.BookTest
equals
Author List: [Bob Smith, Jane Doe]
ISBN: 67890
validate
Author List: [Bob Smith, Jane Doe]
ISBN: 67890
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.23 sec

------------- Standard Output ---------------
equals
Author List: [Bob Smith, Jane Doe]
ISBN: 67890
validate
Author List: [Bob Smith, Jane Doe]
ISBN: 67890
------------- ---------------- ---------------
test:
Deleting: /var/folders/k7/wpgy3lw91171qxlzt4pj0cfh0000gn/T/TEST-library.domain.BookTest.xml
BUILD SUCCESSFUL (total time: 1 second)

ここに私の新しいページがあります:

NEW BookTest.java

package library.domain;

import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class BookTest {

    private ArrayList<String> authorList = new ArrayList<>();

    @Test
    public void testEquals()                //test Equals() for accuracy
    {
        System.out.println("equals");
        authorList.add("Bob Smith");
        Book book = new Book("12345", authorList);
        assertEquals("expected true", true, book.equals(book));
        authorList.add("Jane Doe");
        book = new Book("67890", authorList);
        assertEquals("expected true", true, book.equals(book));
        System.out.println("Author List: " + authorList);
        System.out.println("ISBN: " + book.getIsbn());
    }

    @Test
    public void testValidate()          //test Validate() for accuracy
    {
        System.out.println("validate");
        authorList.add("Bob Smith");
        Book book = new Book("12345", authorList);
        assertEquals("expected true", true, book.validate());
        authorList.add("Jane Doe");
        book = new Book("67890", authorList);
        assertEquals("expected true", true, book.validate());
        System.out.println("Author List: " + authorList);
        System.out.println("ISBN: " + book.getIsbn());
    }
}

Book.java

package library.domain;

import java.util.ArrayList;
import java.util.Objects;

public class Book {

    private String isbn;
    private ArrayList<String> authorList;

    public Book(String isbn, ArrayList<String> authorList)
    {
        this.isbn = isbn;
        this.authorList = authorList;
    }

    public String getIsbn()             //access to isbn and manages  next value
    {
        return isbn;
    }

    public void setIsbn(String isbn)            //assigns the input isbn to the data member isbn
    {
        this.isbn = isbn;
    }
//assigns the input author to the data member author

    public ArrayList<String> getAuthorList()
    {
        return authorList;
    }

    public void setAuthorList(ArrayList<String> authorList)
    {
        this.authorList = authorList;
    }

    @Override
    public boolean equals(Object obj)           //checks  equality of two objects - true if same, false if different
    {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Book)) {
            return false;
        }

        Book book = (Book) obj;
        if (!this.isbn.equals(book.isbn)) {
            return false;
        }
        if (!this.authorList.equals(book.authorList)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()           //override hash
    {
        int hash = 7;
        hash = 97 * hash + Objects.hashCode(this.authorList);
        hash = 97 * hash + Objects.hashCode(this.isbn);
        return hash;
    }

    public boolean validate()           //validate isbn and author not null
    {
        if (isbn == null || isbn.equals("")) {
            return false;
        }
        if (authorList == null || authorList.equals("")) {
            return false;
        }
        {
            return true;
        }
    }
}

BookTest.java

package library.domain;

import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class BookTest {

    private ArrayList<String> authorList = new ArrayList<>();
    private String isbn;

    @Test
    public void testEquals()                //test Equals() for accuracy
    {
        System.out.println("equals");
        authorList.add("Bob Smith");
        authorList.add("Jane Doe");
        Book book = new Book("12345", authorList);
        assertEquals("expected true", true, book.equals(book));
        System.out.println("Author List: " + authorList);
        System.out.println("ISBN: " + isbn);
    }

    @Test
    public void testValidate()          //test Validate() for accuracy
    {
        System.out.println("validate");
        authorList.add("Bob Smith");
        authorList.add("Jane Doe");
        Book book = new Book("12345", authorList);
        assertEquals("expected true", true, book.validate());
        System.out.println("Author List: " + authorList);
        System.out.println("ISBN: " + isbn);
    }
}
4

4 に答える 4

0

テストクラスの isbn はローカル変数であり、同じ値を設定していません。オブジェクトが正しく作成されているかどうかを確認するには、book.getAuthorList() と book.getIsbn() を印刷してみてください。

于 2015-07-21T18:30:15.293 に答える
0

BookTest では、BookTest のスコープ内の isbn の値を出力しています。BookTest で初期化したので、isbn への呼び出しはすべてグローバル変数を参照します。

private String isbn;

Book オブジェクトの isbn フィールドを出力する必要があります。

System.out.println("ISBN: " + book.getIsbn());

ゲッター メソッドを作成しましたが、それを使用するのを忘れただけです。

于 2015-07-21T18:32:08.893 に答える