bookTest.java prints out author arrayList, but not isbn -
requirements: accommodate multiple authors using 1 of components java collection framework. requires 1 book isbn , collection of authors. junit: guidance testvalidate: test @ least 2 cases (one case book properties hold correct data types , not empty nor hold null value, 1 not). guidance testequals: test @ least 2 cases (one case authors , isbn match, 1 not). test @ least 2 authors. teacher told me: testequals need add isbn , 2 authors. create arraylist. add 2 authors it. create book object , add arraylist instance , isbn. think that's have done, authors printing, isbns not. total newbie , @ loss! can help?
edit/addition got isbn print, printing second isbn have. need change both of them print? or matter?
here output:
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)
here new page:
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() 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() 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 isbn , manages next value { return isbn; } public void setisbn(string isbn) //assigns input isbn data member isbn { this.isbn = isbn; } //assigns input author 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 2 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 , 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() 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() 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 in test class local variable , not setting values same. check if object being created correctly, try printing book.getauthorlist() , book.getisbn()
Comments
Post a Comment