Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to design a JUnit test that will test the functionality of the file Book.

ID: 3850463 • Letter: I

Question

I need to design a JUnit test that will test the functionality of the file Book.Java. I have included the test file that I have made thus far, but it is coming back as failing the necessary tests, and I am not sure as to why. This is for a Test Driven Development. I think I have the implementation mostly correct, but I need to know where I am lacking. Also, I am not sure how he is going to implement the boolean, so it leaves me unsure as to how to set up the test. I have completed parts of the program so that I can test my code, and most of it is working. I just need some advice on where I need to go from here.

Book.java

package edu.odu.cs.cs350;

import java.util.Iterator;

/**
* A book object as it might be recorded in a publisher's catalog or a
library.
*
* <p>
* This is not a capture of the contents of the book, but of the metadata
that
* describes the book: authors, title, etc.
*
* @author zeil
*
*/
public class Book implements Cloneable, Iterable<Author> {

   private String title1;
   String publisher1;
   String isbn1;

   /**
   * Create a "blank" book with empty strings for title, publisher, ISBN and
   * an empty (zero-length) list of authors.
   */
   public Book() {
       title1 = "";
       publisher1 = "";
       isbn1 = "";
   }

   /**
   * Create a new book.
   *
   * @param title
   * title of the book
   * @param publisher
   * publisher of the book.
   * @param isbn
   * ISBN identifier for the book.
   * @param authors
   * list of authors for this book.
   */
   public Book(String title, String publisher, String isbn, Author[] authors) {
       // TODO
   }

   /**
   * Get the title of this book.
   *
   * @return the title
   */
   public String getTitle() {

       return this.title1;
   }

   /**
   * Set the title of this book.
   *
   * @param title
   * the title to set
   */
   public void setTitle(String title) {
       this.title1 = title;
   }

   /**
   * Get the ISBN of this book.
   *
   * @return the ISBN
   */
   public String getISBN() {

       return this.isbn1;
   }

   /**
   * Set the ISBN of this book.
   *
   * @param isbn
   * the isbn to set
   */
   public void setISBN(String isbn) {
       this.isbn1 = isbn;
   }

   /**
   * Get the publisher of this book.
   *
   * @return the publisher
   */
   public String getPublisher() {
       // TODO
       return this.publisher1;
   }

   /**
   * Set the publisher of this book.
   *
   * @param publisher
   * the publisher to set
   */
   public void setPublisher(String publisher) {
       this.publisher1 = publisher;
   }

   /**
   * How many authors does this book have?
   *
   * @return number of authors
   */
   public int numAuthors() {
       // TODO
       return 0;
   }

   /**
   * Add an author to the end of the books's list (if that author is not
   * already in there).
   *
   * @param au
   * author to be added
   */
   public void addAuthor(Author au) {
       // TODO
   }

   /**
   * Render the book as a string in a format guaranteed to contain all fields.
   */
   public String toString() {
       // TODO
       return "";
   }

   // Comparison and hashing

   /**
   * Compares two books for equality. They are considered equal if they have
   * the same ISBN.
   *
   * @param obj
   * object to be compared for equality with this duration
   * @return <tt>true</tt> if the specified object is equal to this one
   */
   public boolean equals(Object obj) {
       // TODO
       return false;
   }

   /**
   * Returns the hash code value for this object.
   *
   * @return the hash code value for this book
   */
   public int hashCode() {

       return (2 * this.title1.length() + 4 * this.isbn1.length() + 8 * this.publisher1.length());

   }

   /**
   * Return a (deep) copy of this object.
   */
   @Override
   public Object clone() {
       // TODO
       return null;
   }

   /**
   * Provide access to the list of authors. e.g., Book book = new Book(...);
   * for (Author au: book) { doSomethingWithAuthor (au); }
   *
   * @return iterator over the authors.
   */
   public Iterator<Author> iterator() {
       // TODO
       return null;
   }

}

Author.java

package edu.odu.cs.cs350;

/**
* An author of a book. Currently, this contains only the author's name.
*
* @author zeil
*
*/
public class Author implements Cloneable, Comparable<Author> {

   /**
   * The surname ("family name") of this author.
   */
   private String surname;

   /**
   * The given name ("personal name" or "first name") of the author.
   */
   private String givenName;

   /**
   * Create an author.
   *
   * @param givenName
   * the personal name of the author
   * @param surname
   * the family name of the author
   */
   public Author(String givenName, String surname) {
       this.givenName = givenName;
       this.surname = surname;
   }

   /**
   * Compare two authors for equality.
   *
   * @param obj
   * another author
   * @return true ff they have the same name
   */
   public boolean equals(Object obj) {
       if (obj instanceof Author) {
           Author au = (Author) obj;
           return givenName.equals(au.givenName) && surname.equals(au.surname);
       } else {
           return false;
       }
   }

   /**
   * Get the name of the author in a form suitable for sorting. surname,
   * givenName
   *
   * @return sortable name of the author
   */
   public String getSortingName() {
       return surname + ", " + givenName;
   }

   /**
   * Return the author's name in conventional form. givenName surname
   */
   public String toString() {
       return givenName + " " + surname;
   }

   /**
   * Return the surname of this author
   *
   * @return the surname
   */
   public String getSurname() {
       return surname;
   }

   /**
   * Return the given name of this author
   *
   * @return the given name
   */
   public String getGivenName() {
       return givenName;
   }

   /**
   * Compare this author to another, returning a negative value if this author
   * should follow the other in a sorted list, zero if they are equal, and a
   * positive value if this author should precede the other in a sorted list.
   *
   * @param au
   * the other author to be compared against.
   * @return number whose sign indicates the comparison result
   */
   public int compareTo(Author au) {
       return getSortingName().compareTo(au.getSortingName());
   }

}

TestBook.java

package edu.odu.cs.cs350;

import static org.junit.Assert.*;
import org.junit.Test;
public class TestBook {

   @Test
  
  
   public void testSetTitle()
{
Book title = new Book();
int oldHashCode = title.hashCode();
title.setTitle("abcde");
assertEquals( "abcde", title.getTitle());
assertEquals("", title.getISBN());
assertEquals("", title.getPublisher());
assertNotEquals(oldHashCode, title.hashCode());

}
  
   @Test
  
   public void testSetISBN()
{
Book ISBN = new Book();
int oldHashCode = ISBN.hashCode();
ISBN.setISBN("123-456-789");
assertEquals("123-456-789", ISBN.getISBN());
assertEquals("", ISBN.getTitle());
       assertEquals("", ISBN.getPublisher());      
       assertNotEquals(oldHashCode, ISBN.hashCode());
      

}
  
  
  
@Test
  
   public void testDuplicateISBN()
{
Book newBook = new Book();
int oldHashCode = newBook.hashCode();
newBook.setISBN("123-456-789");
Book Repeat = new Book();
Repeat.setISBN("123-456-789");
  
       assertNotEquals(oldHashCode, newBook.hashCode());
       assertNotEquals(Repeat.getISBN(), newBook.getISBN());

}
  
@Test
  
   public void testSetPublisher()
{
Book Publisher = new Book();
int oldHashCode = Publisher.hashCode();
Publisher.setPublisher("abcde");

assertEquals("abcde", Publisher.getPublisher());
assertEquals("", Publisher.getTitle());
       assertEquals("", Publisher.getISBN());
       assertNotEquals(oldHashCode, Publisher.hashCode());

}


@Test

public void testAddAuthor()
{
Author Auth = new Author("abc", "def");
Book test = new Book();
test.addAuthor(Auth);
  
assertEquals("abc", Auth.getGivenName());
assertEquals("def", Auth.getSurname());
assertEquals("def, abc", Auth.getSortingName());
}

}

Explanation / Answer

Hi,

Please find the answer for the question above.

Please note that I have used JUnit jar version 4.12 for this program.I have made few changes in Book.java and TestBook.java files.

Book.java:

package edu.odu.cs.cs350;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* A book object as it might be recorded in a publisher's catalog or a library.
*
* <p>
* This is not a capture of the contents of the book, but of the metadata that
* describes the book: authors, title, etc.
*
* @author zeil
*
*/
public class Book implements Cloneable, Iterable<Author> {
   private String title1;
   private String publisher1;
   private String isbn1;
   private List<Author> authors;

   /**
   * Create a "blank" book with empty strings for title, publisher, ISBN and
   * an empty (zero-length) list of authors.
   */
   public Book() {
       title1 = "";
       publisher1 = "";
       isbn1 = "";
       authors = new ArrayList<Author>();
   }

   /**
   * Create a new book.
   *
   * @param title
   * title of the book
   * @param publisher
   * publisher of the book.
   * @param isbn
   * ISBN identifier for the book.
   * @param authors
   * list of authors for this book.
   */
   public Book(String title, String publisher, String isbn, Author[] authors) {
       this.title1 = title;
       this.publisher1 = publisher;
       this.isbn1 = isbn;
       this.authors = Arrays.asList(authors);
   }

   /**
   * Get the title of this book.
   *
   * @return the title
   */
   public String getTitle() {
       return this.title1;
   }

   /**
   * Set the title of this book.
   *
   * @param title
   * the title to set
   */
   public void setTitle(String title) {
       this.title1 = title;
   }

   /**
   * Get the ISBN of this book.
   *
   * @return the ISBN
   */
   public String getISBN() {
       return this.isbn1;
   }

   /**
   * Set the ISBN of this book.
   *
   * @param isbn
   * the isbn to set
   */
   public void setISBN(String isbn) {
       this.isbn1 = isbn;
   }

   /**
   * Get the publisher of this book.
   *
   * @return the publisher
   */
   public String getPublisher() {
       return this.publisher1;
   }

   /**
   * Set the publisher of this book.
   *
   * @param publisher
   * the publisher to set
   */
   public void setPublisher(String publisher) {
       this.publisher1 = publisher;
   }

   /**
   * How many authors does this book have?
   *
   * @return number of authors
   */
   public int numAuthors() {
       return authors.size();
   }

   /**
   * Add an author to the end of the books's list (if that author is not
   * already in there).
   *
   * @param au
   * author to be added
   */
   public void addAuthor(Author au) {
       if (!authors.contains(au)) {
           authors.add(au);
       }
   }

   /**
   * Render the book as a string in a format guaranteed to contain all fields.
   */
   @Override
   public String toString() {
       return "Book [title1=" + title1 + ", publisher1=" + publisher1 + ", isbn1=" + isbn1 + ", authors=" + authors
               + "]";
   }

   // Comparison and hashing
   /**
   * Compares two books for equality. They are considered equal if they have
   * the same ISBN.
   *
   * @param obj
   * object to be compared for equality with this duration
   * @return <tt>true</tt> if the specified object is equal to this one
   */
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((isbn1 == null) ? 0 : isbn1.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Book other = (Book) obj;
       if (isbn1 == null) {
           if (other.isbn1 != null)
               return false;
       } else if (!isbn1.equals(other.isbn1))
           return false;
       return true;
   }


   /**
   * Return a (deep) copy of this object.
   */
   @Override
   public Object clone() {
       return new Book(this.title1, this.publisher1, this.isbn1, new Author[10]);
      
   }

       /**
   * Provide access to the list of authors. e.g., Book book = new Book(...);
   * for (Author au: book) { doSomethingWithAuthor (au); }
   *
   * @return iterator over the authors.
   */
   public Iterator<Author> iterator() {
       return this.authors.iterator();
   }
}

Author.java:

package edu.odu.cs.cs350;

/**
* An author of a book. Currently, this contains only the author's name.
*
* @author zeil
*
*/
public class Author implements Cloneable, Comparable<Author> {
   /**
   * The surname ("family name") of this author.
   */
   private String surname;
   /**
   * The given name ("personal name" or "first name") of the author.
   */
   private String givenName;

   /**
   * Create an author.
   *
   * @param givenName
   * the personal name of the author
   * @param surname
   * the family name of the author
   */
   public Author(String givenName, String surname) {
       this.givenName = givenName;
       this.surname = surname;
   }

   /**
   * Compare two authors for equality.
   *
   * @param obj
   * another author
   * @return true ff they have the same name
   */
   public boolean equals(Object obj) {
       if (obj instanceof Author) {
           Author au = (Author) obj;
           return givenName.equals(au.givenName) && surname.equals(au.surname);
       } else {
           return false;
       }
   }

   /**
   * Get the name of the author in a form suitable for sorting. surname,
   * givenName
   *
   * @return sortable name of the author
   */
   public String getSortingName() {
       return surname + ", " + givenName;
   }

   /**
   * Return the author's name in conventional form. givenName surname
   */
   public String toString() {
       return givenName + " " + surname;
   }

   /**
   * Return the surname of this author
   *
   * @return the surname
   */
   public String getSurname() {
       return surname;
   }

   /**
   * Return the given name of this author
   *
   * @return the given name
   */
   public String getGivenName() {
       return givenName;
   }

   /**
   * Compare this author to another, returning a negative value if this author
   * should follow the other in a sorted list, zero if they are equal, and a
   * positive value if this author should precede the other in a sorted list.
   *
   * @param au
   * the other author to be compared against.
   * @return number whose sign indicates the comparison result
   */
   public int compareTo(Author au) {
       return getSortingName().compareTo(au.getSortingName());
   }
}

TestBook.java:

package edu.odu.cs.cs350;

import static org.junit.Assert.*;
import org.junit.Test;

public class TestBook {
   @Test
   public void testSetTitle() {
       Book title = new Book();
       title.setTitle("abcde");
       //setting title "abcde", so expecting should be "abcde"
       assertEquals("abcde", title.getTitle());
       assertEquals("", title.getISBN());
       assertEquals("", title.getPublisher());
   }

   @Test
   public void testSetISBN() {
       Book isbn = new Book();
       isbn.setISBN("123-456-789");
       //setting isbn "123-456-789", so expecting should be "123-456-789"
       assertEquals("123-456-789", isbn.getISBN());
       assertEquals("", isbn.getTitle());
       assertEquals("", isbn.getPublisher());
   }

   @Test
   public void testDuplicateISBN() {
       Book newBook = new Book();
       newBook.setISBN("123-456-789");
      
       Book repeat = new Book();
       repeat.setISBN("123-456-789");
//if both are equal it will be duplicate only
       assertEquals(newBook, repeat);
       assertEquals(newBook.getISBN(),repeat.getISBN());

   }

   @Test
   public void testSetPublisher() {
       Book publisher = new Book();
       publisher.setPublisher("abcde");
       //setting publisher "abcde", so expecting should be "abcde"
       assertEquals("abcde", publisher.getPublisher());
       assertEquals("", publisher.getTitle());
       assertEquals("", publisher.getISBN());

   }

   @Test
   public void testAddAuthor() {
       Author auth = new Author("abc", "def");
       Book test = new Book();
       test.addAuthor(auth);
      
       Author actualAut =test.iterator().next();

       assertEquals("abc", auth.getGivenName());
       assertEquals("def", auth.getSurname());
       assertEquals("def, abc", auth.getSortingName());
       //checking added Author and actualAut which is return from book should same
       assertEquals(actualAut, auth);
   }
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>edu.odu.cs.cs350</groupId>
   <artifactId>sample</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <dependencies>
       <!-- https://mvnrepository.com/artifact/junit/junit -->
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>

   </dependencies>
</project>

Hope this was helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote