Pertemuan 10 OOP - Java Unit Testing

Nama : Kadek Fajar Pramartha Yasodana
NRP : 5025231185
Kelas : PBO A

Java Unit Testing

Untuk melakukan testing kita menggunakan library JUnit 5.8.1. Untuk mendownloadnya di intellij kita hanya perlu melakukan download pada package managernya langsung. Setelah itu untuk melakukan testing ditambahkan atribut @test untuk memberikan informasi bahwa fungsi tersebut akan menjalankan test.

Source Code : GitHub

SalesItem.java
import java.util.ArrayList;
import java.util.Iterator;

public class SalesItem {
private String name;
private int price; // in cents
private ArrayList < Comment > comments;

public SalesItem(String name, int price) {
this.name = name;
this.price = price;
comments = new ArrayList < Comment > ();
}

public String getName() {
return name;
}

public int getPrice() {
return price;
}

public int getNumberOfComments() {
return comments.size();
}

public boolean addComment(String author, String text, int rating) {
if (ratingInvalid(rating)) { // reject invalid ratings
return false;
}
if (findCommentByAuthor(author) != null) {
// reject multiple comments by same author

return false;
}
comments.add(new Comment(author, text, rating));
return true;
}

public void removeComment(int index) {
if (index >= 0 && index < comments.size()) { // index is valid
comments.remove(index);
}
}

public void upvoteComment(int index) {
if (index >= 0 && index < comments.size()) { // index is valid
comments.get(index).upvote();
}
}

public void downvoteComment(int index) {
if (index >= 0 && index < comments.size()) { // index is valid
comments.get(index).downvote();
}
}

public void showInfo() {
System.out.println("*** " + name + " ***");
System.out.println("Price: " + priceString(price));
System.out.println();
System.out.println("Customer comments:");
for (Comment comment: comments) {
System.out.println("-----------------------------------");
System.out.println(comment.getFullDetails());
}
System.out.println();
System.out.println("=====================================");
}

public Comment findMostHelpfulComment() {
Iterator < Comment > it = comments.iterator();
Comment best = it.next();
while (it.hasNext()) {
Comment current = it.next();
if (current.getVoteCount() > best.getVoteCount()) {
best = current;
}
}
return best;
}

private boolean ratingInvalid(int rating) {
return rating < 0 || rating > 5;
}

private Comment findCommentByAuthor(String author) {
for (Comment comment: comments) {
if (comment.getAuthor().equals(author)) {
return comment;
}
}
return null;
}

private String priceString(int price) {
int dollars = price / 100;
int cents = price-(dollars * 100);
if (cents <= 9) {
return "$" + dollars + ".0" + cents; // zero padding
} else {
return "$" + dollars + "." + cents;
}
}
}

Comment.java
public class Comment {
private String author;
private String text;
private int rating; // rating between 1 and 5
private int voteCount;

public Comment(String author, String text, int rating) {
this.author = author;
this.text = text;
this.rating = rating;
this.voteCount = 0; // initialize vote count to 0
}

public String getAuthor() {
return author;
}

public String getText() {
return text;
}

public int getRating() {
return rating;
}

public int getVoteCount() {
return voteCount;
}

public void upvote() {
voteCount++;
}

public void downvote() {
voteCount--;
}

public String getFullDetails() {
return "Author: " + author + "\n" +
"Rating: " + rating + "\n" +
"Vote Count: " + voteCount + "\n" +
"Comment: " + text;
}
}

SalesItemTest.java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class SalesItemTest {
@Test
public void testComment() {
SalesItem item = new SalesItem("Innova 2.4V", 1000000000);
assertTrue(item.addComment("Fajar", "A very reliable car", 5));
assertFalse(item.addComment("Randi", "Bad car not good on gas mileage", -1));
assertTrue(item.addComment("Arya", "A good car but i think avanza is better", 4));

assertEquals(item.getNumberOfComments(), 2);
}

@Test
public void testUpvote()
{
SalesItem item = new SalesItem("Innova 2.4V", 1000000000);
item.addComment("Fajar", "A very reliable car", 5);
item.addComment("Arya", "A good car but i think avanza is better", 4);

for(int i = 0; i < 5; i++)
{
item.upvoteComment(0);
item.upvoteComment(1);
}

item.downvoteComment(1);
item.downvoteComment(1);
item.upvoteComment(0);

assertEquals(6, item.findMostHelpfulComment().getVoteCount());
}

@Test
public void testRemoveComment()
{
SalesItem item = new SalesItem("Innova 2.4V", 1000000000);
assertTrue(item.addComment("Fajar", "A very reliable car", 5));
assertTrue(item.addComment("Arya", "A good car but i think avanza is better", 4));
assertTrue(item.addComment("Nanda", "Bad car", 0));

item.removeComment(2);

assertEquals(2, item.getNumberOfComments());
}
}

Output


Comments

Popular posts from this blog

Pertemuan 13 OOP - Abstraksi & Simulasi Fox & Rabit

Pertemuan 11 PWEB - Membuat Website CRUD PHP MySQL

Pertemuan 11 PBO - Studi Kasus Network Project (Inheritance)