|
| 1 | +package com.codedifferently.lesson16.tiktokvideosystem; |
| 2 | + |
| 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; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +public class TiktokVideoTest { |
| 14 | + |
| 15 | + private TiktokVideo tiktokVideo; |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + void setUp() { |
| 19 | + tiktokVideo = |
| 20 | + new TiktokVideo("enigivensunday", 1000000, 500000, TiktokVideo.VideoCategory.VLOG); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void testConstructor() { |
| 25 | + TiktokVideo video = new TiktokVideo("Jane", 3000, 1000, VideoCategory.DANCE); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testSetAndGetVideoCategory() { |
| 30 | + tiktokVideo.setVideoCategory(VideoCategory.COMEDY); |
| 31 | + assertEquals(VideoCategory.COMEDY, tiktokVideo.getVideoCategory()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void getVideoCategory() { |
| 36 | + assertEquals(TiktokVideo.VideoCategory.VLOG, tiktokVideo.getVideoCategory()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testViewsCount() { |
| 41 | + assertEquals(1000000, tiktokVideo.getViewsCount()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testIncreaseViewsPositive() { |
| 46 | + tiktokVideo.increaseViews(500); |
| 47 | + // got the is matcher idea from chat gpt |
| 48 | + assertEquals(1000500, tiktokVideo.getViewsCount()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testDecreaseViewsNegative() { |
| 53 | + InvalidViewIncrementException exception = |
| 54 | + assertThrows( |
| 55 | + InvalidViewIncrementException.class, |
| 56 | + () -> { |
| 57 | + tiktokVideo.increaseViews(-1); |
| 58 | + }); |
| 59 | + assertEquals("View increase amount must be positive.", exception.getMessage()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testDisplayComments() { |
| 64 | + // used google & chat for this unit test solution |
| 65 | + // Arrange |
| 66 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 67 | + PrintStream printStream = new PrintStream(outputStream); |
| 68 | + PrintStream originalOut = System.out; |
| 69 | + System.setOut(printStream); |
| 70 | + |
| 71 | + ArrayList<String> comments = new ArrayList<>(); |
| 72 | + comments.add("Love this video!"); |
| 73 | + comments.add("So funny"); |
| 74 | + tiktokVideo.displayComments(comments); |
| 75 | + System.setOut(originalOut); |
| 76 | + String output = outputStream.toString().trim(); |
| 77 | + String expectedOutput = "Love this video!\nSo funny"; |
| 78 | + assertEquals(expectedOutput, output); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testDisplayCommentsWithNullList() { |
| 83 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 84 | + PrintStream printStream = new PrintStream(outputStream); |
| 85 | + PrintStream originalOut = System.out; |
| 86 | + System.setOut(printStream); |
| 87 | + |
| 88 | + tiktokVideo.displayComments(null); |
| 89 | + |
| 90 | + System.setOut(originalOut); |
| 91 | + |
| 92 | + String output = outputStream.toString().trim(); |
| 93 | + assertEquals("No comments yet. Be the first to comment!", output); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void getCreatorName() { |
| 98 | + assertEquals("enigivensunday", tiktokVideo.getCreator()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testAddComments() { |
| 103 | + tiktokVideo.addComments("This is awesome!"); |
| 104 | + |
| 105 | + assertEquals("This is awesome!", tiktokVideo.getCommentsList().get(0)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testLikeToViewRatio() { |
| 110 | + // Create the object using the constructor with views and likes |
| 111 | + double result = tiktokVideo.likeToViewRatio(); |
| 112 | + assertEquals( |
| 113 | + 0.5, result, 0.0001); // got the idea to Use delta for comparing doubles from chatGpt |
| 114 | + } |
| 115 | +} |
0 commit comments