|
1 |
| -package com.codedifferently.lesson16; |
2 | 1 | import static org.assertj.core.api.Assertions.assertThat;
|
| 2 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
3 | 3 |
|
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
4 | 6 | import org.junit.jupiter.api.Test;
|
5 | 7 |
|
6 |
| - |
7 | 8 | public class DogTest {
|
8 |
| - public abstract class EnumToArray { |
9 |
| - enum Colors { |
| 9 | + |
| 10 | + // Test the Dog class |
| 11 | + public class Dog { |
| 12 | + // Custom exception |
| 13 | + public static class DogAgeException extends Exception { |
| 14 | + public DogAgeException(String message) { |
| 15 | + super(message); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + // Enum for dog colors |
| 20 | + public enum Colors { |
10 | 21 | WHITE,
|
11 | 22 | BROWN,
|
12 |
| - BLONDE; |
| 23 | + BLONDE |
13 | 24 | }
|
14 |
| - } |
15 |
| - |
16 |
| - |
17 | 25 |
|
18 |
| - public int getAge(int age) { |
19 |
| - return age; |
20 |
| - } |
| 26 | + // Member variables |
| 27 | + private int age; |
| 28 | + private String breed; |
| 29 | + private String gender; |
| 30 | + private Colors color; |
| 31 | + private boolean isFed; |
| 32 | + private List<String> favoriteToys; |
| 33 | + |
| 34 | + // Constructor |
| 35 | + public Dog(int age, String breed, String gender, Colors color, boolean isFed) |
| 36 | + throws DogAgeException { |
| 37 | + if (age < 0) { |
| 38 | + throw new DogAgeException("Age cannot be negative."); |
| 39 | + } |
| 40 | + this.age = age; |
| 41 | + this.breed = breed; |
| 42 | + this.gender = gender; |
| 43 | + this.color = color; |
| 44 | + this.isFed = isFed; |
| 45 | + this.favoriteToys = new ArrayList<>(); |
| 46 | + } |
21 | 47 |
|
22 |
| - @Test |
23 |
| - public void testGetAge() { |
24 |
| - int getAge = getAge(2); |
25 |
| - int myDogsAge = getAge(2); |
26 |
| - assertThat(getAge(2)).isNotNull(); |
27 |
| - } |
| 48 | + // Getters |
| 49 | + public int getAge() { |
| 50 | + return age; |
| 51 | + } |
28 | 52 |
|
29 |
| - public String getbreed(String breed) { |
30 |
| - return breed; |
31 |
| - } |
| 53 | + public String getBreed() { |
| 54 | + return breed; |
| 55 | + } |
32 | 56 |
|
33 |
| - @Test |
34 |
| - public void testGetBreed() { |
35 |
| - String getBreed = getbreed(getbreed("Mutt")); |
36 |
| - assertThat(getbreed("Mutt")); |
37 |
| - } |
| 57 | + public String getGender() { |
| 58 | + return gender; |
| 59 | + } |
| 60 | + |
| 61 | + public Colors getColor() { |
| 62 | + return color; |
| 63 | + } |
38 | 64 |
|
39 |
| - public String getGender(String gender) { |
40 |
| - return gender; |
| 65 | + public boolean isFed() { |
| 66 | + return isFed; |
| 67 | + } |
| 68 | + |
| 69 | + public String getFavoriteToys() { |
| 70 | + String favoriteToy = "Squeaky Fish"; |
| 71 | + return favoriteToy; |
| 72 | + } |
| 73 | + |
| 74 | + public String addFavoriteToy(String favoriteToy2) { |
| 75 | + String favoriteToy = "Squeaky Fish"; |
| 76 | + return favoriteToy; |
| 77 | + } |
41 | 78 | }
|
42 | 79 |
|
43 | 80 | @Test
|
44 |
| - public String testGetGender() { |
45 |
| - String getGender = getGender(getGender("male")); |
46 |
| - |
47 |
| - assertThat(getGender("male")); |
48 |
| - return getbreed(getGender); |
| 81 | + public void testGetAge() throws DogTest.Dog.DogAgeException { |
| 82 | + Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
| 83 | + assertThat(dog.getAge()).isEqualTo(2); |
49 | 84 | }
|
50 | 85 |
|
51 |
| - public Enum getColors(Enum Colors) { |
52 |
| - return Colors; |
| 86 | + @Test |
| 87 | + public void testGetBreed() throws DogTest.Dog.DogAgeException { |
| 88 | + Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
| 89 | + assertThat(dog.getBreed()).isEqualTo("Mutt"); |
53 | 90 | }
|
54 | 91 |
|
55 | 92 | @Test
|
56 |
| - public Enum testGetColors(Enum Colors) { |
57 |
| - Enum testColor = Colors; |
58 |
| - assertThat(getColors(Colors)); |
59 |
| - return getColors(Colors); |
| 93 | + public void testGetGender() throws DogTest.Dog.DogAgeException { |
| 94 | + Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
| 95 | + assertThat(dog.getGender()).isEqualTo("male"); |
60 | 96 | }
|
61 | 97 |
|
62 | 98 | @Test
|
63 |
| - public void testgetColor() { |
64 |
| - assertThat(getColors(null)); |
| 99 | + void DogTesttestGetColor() throws DogTest.Dog.DogAgeException { |
| 100 | + Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
| 101 | + assertThat(dog.getColor()).isEqualTo(Dog.Colors.BROWN); |
65 | 102 | }
|
66 | 103 |
|
67 | 104 | @Test
|
68 |
| - Boolean isFed(Boolean Fed) { |
69 |
| - var isNotFed = false; |
70 |
| - var isFed = true; |
71 |
| - var getFedStatus = Fed || isNotFed; |
72 |
| - assertThat(getFedStatus).isTrue(); |
73 |
| - assertThat(Fed).isFalse(); |
74 |
| - assertThat(getFedStatus).isEqualTo(isFed); |
75 |
| - return getFedStatus == Fed; |
| 105 | + public void testIsFed() throws DogTest.Dog.DogAgeException { |
| 106 | + Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
| 107 | + assertThat(dog.isFed()).isTrue(); |
76 | 108 | }
|
77 | 109 |
|
78 | 110 | @Test
|
79 | 111 | public void testDogConstructorThrowsExceptionForNegativeAge() {
|
80 |
| - public class newDog extends Dog { |
81 |
| - |
82 |
| - |
83 |
| - // Arrange |
84 |
| - Enum colorList = getColors(null); |
85 |
| - int negativeAge = -1; |
86 |
| - String breed = "Mutt"; |
87 |
| - String gender = "male"; |
88 |
| - boolean isFed = true; |
89 |
| - Enum Color1 = null; |
90 |
| - |
91 |
| - Dog dog = new Dog(2, "Mutt", "male", Color1, true); |
92 |
| - |
93 |
| - //None of this is going to work without the correct import configuration |
94 |
| - |
95 |
| - |
96 | 112 | // Act & Assert
|
97 |
| - assertThatThrownBy(() -> new Dog(negativeAge, breed, gender, Color1, isFed)) |
98 |
| - .isInstanceOf(DogAgeException.class) |
| 113 | + assertThatThrownBy(() -> new Dog(-1, "Mutt", "male", Dog.Colors.BROWN, true)) |
| 114 | + .isInstanceOf(Dog.DogAgeException.class) |
99 | 115 | .hasMessage("Age cannot be negative.");
|
100 |
| - return |
101 | 116 | }
|
102 | 117 |
|
103 | 118 | @Test
|
104 |
| - public String testaddFavoriteToy(String toy) { |
105 |
| - |
106 |
| - Enum Color1 = null; |
107 |
| - |
| 119 | + public void testAddFavoriteToy() throws Dog.DogAgeException { |
| 120 | + Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
108 | 121 | String favoriteToy = "Squeaky Fish";
|
109 |
| - |
110 |
| - assertThat(favoriteToy).isEqualTo("Squeaky Fish"); |
111 |
| - |
112 |
| - return toy; |
| 122 | + dog.addFavoriteToy(favoriteToy); |
| 123 | + assertThat(dog.getFavoriteToys()).contains(favoriteToy); |
113 | 124 | }
|
114 | 125 | }
|
0 commit comments