1
1
package com .codedifferently .lesson16 .tiktokvideosystem ;
2
2
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 ;
5
10
import org .junit .jupiter .api .BeforeEach ;
6
11
import org .junit .jupiter .api .Test ;
7
12
@@ -20,30 +25,93 @@ void testConstructor() {
20
25
TiktokVideo video = new TiktokVideo ("Jane" , 3000 , 1000 , VideoCategory .DANCE );
21
26
}
22
27
28
+ @ Test
29
+ void testSetAndGetVideoCategory () {
30
+ tiktokVideo .setVideoCategory (VideoCategory .COMEDY );
31
+ assertEquals (VideoCategory .COMEDY , tiktokVideo .getVideoCategory ());
32
+ }
33
+
23
34
@ Test
24
35
void getVideoCategory () {
25
36
assertEquals (TiktokVideo .VideoCategory .VLOG , tiktokVideo .getVideoCategory ());
26
37
}
27
38
39
+ @ Test
40
+ void testViewsCount () {
41
+ assertEquals (1000000 , tiktokVideo .getViewsCount ());
42
+ }
43
+
28
44
@ Test
29
45
void testIncreaseViewsPositive () {
30
46
tiktokVideo .increaseViews (500 );
31
47
// got the is matcher idea from chat gpt
32
- assertThat ( tiktokVideo .getViewsCount (), is ( 1500 ));
48
+ assertEquals ( 1000500 , tiktokVideo .getViewsCount ());
33
49
}
34
50
35
51
@ Test
36
52
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 ());
39
62
}
40
63
41
64
@ 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!\n So 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
+ }
43
97
44
98
@ Test
45
99
void getCreatorName () {
100
+ assertEquals ("enigivensunday" , tiktokVideo .getCreator ());
101
+ }
46
102
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
48
116
}
49
117
}
0 commit comments