要求:使用Java Collection Framework中的一个组件来容纳多个作者。需要一本带有isbn和一组作者的书。 JUnit:testValidate指南:测试至少两种情况(一种情况是书籍属性包含正确的数据类型且不为空也不保留空值,一种情况不存在)。 testEquals指南:测试至少两个案例(一个案例是作者和isbn匹配,一个是他们不匹配的案例)。测试至少两位作者。我的老师告诉我:testEquals你需要添加isbn和两位作者。创建一个ArrayList。添加两位作者。创建一个Book对象并添加ArrayList实例和isbn。我认为这就是我所做的,作者正在打印,但ISBN不是。我是个新手,我不知所措!有人可以帮忙吗?
编辑/添加我打印了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);
}
}
答案 0 :(得分:0)
isbn是一个局部变量,并且您没有为它设置任何值。要检查对象是否正确创建,请尝试打印book.getAuthorList()和book.getIsbn()
答案 1 :(得分:0)
Book book = new Book("12345", authorList);
您可以在isbn
课程中设置Book
的价值。
System.out.println("ISBN: " + isbn);
但是你在这里尝试打印当前类中的isbn
。因此它不会打印任何值。因为你没有传递任何价值。
打印isbn
类值Book
中可用的值{/ 1}}
System.out.println("ISBN: " + book.getIsbn());
System.out.println("Author List: " + authorList);
此处authorList
也是一个局部变量。打印authorList
Book
的值可在 System.out.println("Author List: " + book.authorList());
类wirte
jasypt {
algorithm = "PBEWithMD5AndTripleDES"
password = "password"
keyObtentionIterations = 1
saltGenerator = new org.jasypt.salt.ZeroSaltGenerator()
}
答案 2 :(得分:0)
在BookTest中,您将在BookTest范围内打印出isbn的值。您在BookTest中初始化它,因此对isbn的任何调用都将引用全局变量
private String isbn;
您需要打印Book对象的isbn字段。
System.out.println("ISBN: " + book.getIsbn());
你写了getter方法,你只是忘了使用它。
答案 3 :(得分:0)
访问ISBN
和authorList
时,您应该使用他们的getter方法。在您的测试中,没有名为String
的{{1}}对象。而是使用ISBN
其他一些事情:
在book.getIsbn();
方法中; authorList是validate()
而不是List
,因此String
永远不会返回true。你应该做authorList.equals("")
之类的事情。 authorList.size() == 0
也不需要在return true
范围内。
此外,您不应该只是尝试打印列表({}
)。你可能会得到意想不到的结果。