|
1 | 1 | import static org.assertj.core.api.Assertions.assertThat; |
2 | 2 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
3 | 3 |
|
4 | | -import java.util.ArrayList; |
5 | | -import java.util.List; |
6 | 4 | import org.junit.jupiter.api.Test; |
7 | 5 |
|
8 | 6 | public class DogTest { |
9 | 7 |
|
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 { |
21 | | - WHITE, |
22 | | - BROWN, |
23 | | - BLONDE |
24 | | - } |
25 | | - |
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 | | - } |
47 | | - |
48 | | - // Getters |
49 | | - public int getAge() { |
50 | | - return age; |
51 | | - } |
52 | | - |
53 | | - public String getBreed() { |
54 | | - return breed; |
55 | | - } |
56 | | - |
57 | | - public String getGender() { |
58 | | - return gender; |
59 | | - } |
60 | | - |
61 | | - public Colors getColor() { |
62 | | - return color; |
63 | | - } |
64 | | - |
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 | | - } |
78 | | - } |
79 | | - |
80 | 8 | @Test |
81 | | - public void testGetAge() throws DogTest.Dog.DogAgeException { |
| 9 | + public void testGetAge() throws Dog.DogAgeException { |
82 | 10 | Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
83 | 11 | assertThat(dog.getAge()).isEqualTo(2); |
84 | 12 | } |
85 | 13 |
|
86 | 14 | @Test |
87 | | - public void testGetBreed() throws DogTest.Dog.DogAgeException { |
| 15 | + public void testGetBreed() throws Dog.DogAgeException { |
88 | 16 | Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
89 | 17 | assertThat(dog.getBreed()).isEqualTo("Mutt"); |
90 | 18 | } |
91 | 19 |
|
92 | 20 | @Test |
93 | | - public void testGetGender() throws DogTest.Dog.DogAgeException { |
| 21 | + public void testGetGender() throws Dog.DogAgeException { |
94 | 22 | Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
95 | 23 | assertThat(dog.getGender()).isEqualTo("male"); |
96 | 24 | } |
97 | 25 |
|
98 | 26 | @Test |
99 | | - void DogTesttestGetColor() throws DogTest.Dog.DogAgeException { |
| 27 | + public void testGetColor() throws Dog.DogAgeException { |
100 | 28 | Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
101 | 29 | assertThat(dog.getColor()).isEqualTo(Dog.Colors.BROWN); |
102 | 30 | } |
103 | 31 |
|
104 | 32 | @Test |
105 | | - public void testIsFed() throws DogTest.Dog.DogAgeException { |
| 33 | + public void testIsFed() throws Dog.DogAgeException { |
106 | 34 | Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true); |
107 | 35 | assertThat(dog.isFed()).isTrue(); |
108 | 36 | } |
|
0 commit comments