bookTest.java 打印作者数组列表,但不打印 isbn
bookTest.java prints out author arrayList, but not isbn
要求: 允许多个作者使用 Java 集合框架中的一个组件。需要一本带有 isbn 和作者合集的书。 JUnit:testValidate 指南:测试至少两种情况(一种情况是书籍属性包含正确的数据类型并且既不为空也不包含空值,另一种情况则不存在)。 testEquals 指南:测试至少两种情况(一种情况下作者和 isbn 匹配,一种情况下不匹配)。测试至少两位作者。我的老师告诉我:testEquals你需要添加isbn和两个作者。创建一个数组列表。添加两个作者。创建一个 Book 对象并添加 ArrayList 实例和 isbn。我认为这就是我所做的,作者正在印刷,但 ISBN 没有。我是一个新手,我很茫然!有人可以帮忙吗?
EDIT/ADDITION 我得到了要打印的 ISBN,但它只打印了我拥有的第二个 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)
这是我的新页面:
新 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);
}
}
isbn 在测试 class 中是一个局部变量,您没有为其设置任何值。要检查对象是否被正确创建,请尝试打印 book.getAuthorList() 和 book.getIsbn()
Book book = new Book("12345", authorList);
此处您将值设置为 isbn
在 Book
class 中可用。
System.out.println("ISBN: " + isbn);
但是您在这里尝试打印当前 class 中存在的 isbn
。所以它不打印任何值。由于您没有向它传递任何值。
要打印 isbn
的值,可在 Book
class 中写入
System.out.println("ISBN: " + book.getIsbn());
System.out.println("Author List: " + authorList);
这里也是authorList
是局部变量。要打印 authorList
的值,可在 Book
class wirte
中使用
System.out.println("Author List: " + book.authorList());
在 BookTest 中,您正在打印 BookTest 范围内的 isbn 值。您在 BookTest 中对其进行了初始化,因此对 isbn 的任何调用都将引用全局变量
private String isbn;
您需要打印出 Book 对象的 isbn 字段。
System.out.println("ISBN: " + book.getIsbn());
你写了 getter 方法,只是忘了使用它。
访问 ISBN
和 authorList
时,您应该使用它们的 getter 方法。在您的测试中,没有名为 ISBN
的 String
对象。而是使用 book.getIsbn();
其他一些事情:
在validate()
方法中; authorList 是 List
而不是 String
所以 authorList.equals("")
永远不会 return 为真。你应该做类似 authorList.size() == 0
的事情。 return true
也不需要在 {}
.
内
此外,您不应该只尝试打印列表 (System.out.println("Author List: " + authorList);
)。您可能会得到意想不到的结果。
要求: 允许多个作者使用 Java 集合框架中的一个组件。需要一本带有 isbn 和作者合集的书。 JUnit:testValidate 指南:测试至少两种情况(一种情况是书籍属性包含正确的数据类型并且既不为空也不包含空值,另一种情况则不存在)。 testEquals 指南:测试至少两种情况(一种情况下作者和 isbn 匹配,一种情况下不匹配)。测试至少两位作者。我的老师告诉我:testEquals你需要添加isbn和两个作者。创建一个数组列表。添加两个作者。创建一个 Book 对象并添加 ArrayList 实例和 isbn。我认为这就是我所做的,作者正在印刷,但 ISBN 没有。我是一个新手,我很茫然!有人可以帮忙吗?
EDIT/ADDITION 我得到了要打印的 ISBN,但它只打印了我拥有的第二个 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)
这是我的新页面:
新 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);
}
}
isbn 在测试 class 中是一个局部变量,您没有为其设置任何值。要检查对象是否被正确创建,请尝试打印 book.getAuthorList() 和 book.getIsbn()
Book book = new Book("12345", authorList);
此处您将值设置为 isbn
在 Book
class 中可用。
System.out.println("ISBN: " + isbn);
但是您在这里尝试打印当前 class 中存在的 isbn
。所以它不打印任何值。由于您没有向它传递任何值。
要打印 isbn
的值,可在 Book
class 中写入
System.out.println("ISBN: " + book.getIsbn());
System.out.println("Author List: " + authorList);
这里也是authorList
是局部变量。要打印 authorList
的值,可在 Book
class wirte
System.out.println("Author List: " + book.authorList());
在 BookTest 中,您正在打印 BookTest 范围内的 isbn 值。您在 BookTest 中对其进行了初始化,因此对 isbn 的任何调用都将引用全局变量
private String isbn;
您需要打印出 Book 对象的 isbn 字段。
System.out.println("ISBN: " + book.getIsbn());
你写了 getter 方法,只是忘了使用它。
访问 ISBN
和 authorList
时,您应该使用它们的 getter 方法。在您的测试中,没有名为 ISBN
的 String
对象。而是使用 book.getIsbn();
其他一些事情:
在validate()
方法中; authorList 是 List
而不是 String
所以 authorList.equals("")
永远不会 return 为真。你应该做类似 authorList.size() == 0
的事情。 return true
也不需要在 {}
.
此外,您不应该只尝试打印列表 (System.out.println("Author List: " + authorList);
)。您可能会得到意想不到的结果。