Skip to content

Commit f8a2c84

Browse files
committed
feat: adds unit test fwhile correcting methods in main
1 parent 76707f4 commit f8a2c84

File tree

2 files changed

+79
-7
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+79
-7
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public void addComments(String comment) {
6969
commentsList.add(comment);
7070
}
7171

72+
public ArrayList<String> getCommentsList() {
73+
return commentsList;
74+
}
75+
7276
public String getCreator() {
7377
return this.creatorName;
7478
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideoTest.java

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.codedifferently.lesson16.tiktokvideosystem;
22

3-
import org.hamcrest.MatcherAssert.assertThat.assertEquals;
4-
import org.hamcrest.Matchers.is;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import com.codedifferently.lesson16.tiktokvideosystem.TiktokVideo.VideoCategory;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import java.util.ArrayList;
510
import org.junit.jupiter.api.BeforeEach;
611
import org.junit.jupiter.api.Test;
712

@@ -20,30 +25,93 @@ void testConstructor() {
2025
TiktokVideo video = new TiktokVideo("Jane", 3000, 1000, VideoCategory.DANCE);
2126
}
2227

28+
@Test
29+
void testSetAndGetVideoCategory() {
30+
tiktokVideo.setVideoCategory(VideoCategory.COMEDY);
31+
assertEquals(VideoCategory.COMEDY, tiktokVideo.getVideoCategory());
32+
}
33+
2334
@Test
2435
void getVideoCategory() {
2536
assertEquals(TiktokVideo.VideoCategory.VLOG, tiktokVideo.getVideoCategory());
2637
}
2738

39+
@Test
40+
void testViewsCount() {
41+
assertEquals(1000000, tiktokVideo.getViewsCount());
42+
}
43+
2844
@Test
2945
void testIncreaseViewsPositive() {
3046
tiktokVideo.increaseViews(500);
3147
// got the is matcher idea from chat gpt
32-
assertThat(tiktokVideo.getViewsCount(), is(1500));
48+
assertEquals(1000500, tiktokVideo.getViewsCount());
3349
}
3450

3551
@Test
3652
void testDecreaseViewsNegative() {
37-
tiktokVideo.InvalidViewIncrementException(-78);
38-
assertThrows(InvalidViewIncrementException.class, () -> tiktokVideo);
53+
// tiktokVideo.InvalidViewIncrementException(-78);
54+
// assertThrows(InvalidViewIncrementException.class, () -> tiktokVideo);
55+
InvalidViewIncrementException exception =
56+
assertThrows(
57+
InvalidViewIncrementException.class,
58+
() -> {
59+
tiktokVideo.increaseViews(-1);
60+
});
61+
assertEquals("View increase amount must be positive.", exception.getMessage());
3962
}
4063

4164
@Test
42-
String showCommennts() {}
65+
void testDisplayComments() {
66+
// used google & chat for this unit test solution
67+
// Arrange
68+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
69+
PrintStream printStream = new PrintStream(outputStream);
70+
PrintStream originalOut = System.out;
71+
System.setOut(printStream);
72+
73+
ArrayList<String> comments = new ArrayList<>();
74+
comments.add("Love this video!");
75+
comments.add("So funny");
76+
tiktokVideo.displayComments(comments);
77+
System.setOut(originalOut);
78+
String output = outputStream.toString().trim();
79+
String expectedOutput = "Love this video!\nSo funny";
80+
assertEquals(expectedOutput, output);
81+
}
82+
83+
@Test
84+
void testDisplayCommentsWithNullList() {
85+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
86+
PrintStream printStream = new PrintStream(outputStream);
87+
PrintStream originalOut = System.out;
88+
System.setOut(printStream);
89+
90+
tiktokVideo.displayComments(null);
91+
92+
System.setOut(originalOut);
93+
94+
String output = outputStream.toString().trim();
95+
assertEquals("No comments yet. Be the first to comment!", output);
96+
}
4397

4498
@Test
4599
void getCreatorName() {
100+
assertEquals("enigivensunday", tiktokVideo.getCreator());
101+
}
46102

47-
assertEquals(TiktokVideo.String, tiktokVideo.getCreator());
103+
@Test
104+
void testAddComments() {
105+
tiktokVideo.addComments("This is awesome!");
106+
107+
assertEquals("This is awesome!", tiktokVideo.getCommentsList().get(0));
108+
}
109+
110+
@Test
111+
void testLikeToViewRatio() {
112+
// Create the object using the constructor with views and likes
113+
double result = tiktokVideo.likeToViewRatio();
114+
assertEquals(
115+
0.5, result, 0.0001); // got the idea to Use delta for comparing doubles from chatGpt
48116
}
49117
}

0 commit comments

Comments
 (0)