diff --git a/src/main/java/com/example/Animal.java b/src/main/java/com/example/Animal.java index 6792db6..d1e9c70 100644 --- a/src/main/java/com/example/Animal.java +++ b/src/main/java/com/example/Animal.java @@ -1,20 +1,19 @@ -package com.example; - -import java.util.List; - -public class Animal { - - public List getFood(String animalKind) throws Exception { - if ("Травоядное".equals(animalKind)) { - return List.of("Трава", "Различные растения"); - } else if ("Хищник".equals(animalKind)) { - return List.of("Животные", "Птицы", "Рыба"); - } else { - throw new Exception("Неизвестный вид животного, используйте значение Травоядное или Хищник"); - } - } - - public String getFamily() { - return "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи"; - } +package com.example; + +import java.util.List; + +public class Animal { + + public List getFood(String animalKind) throws Exception { + if ("Травоядное".equals(animalKind)) { + return List.of("Трава", "Различные растения"); + } else if ("Хищник".equals(animalKind)) { + return List.of("Животные", "Птицы", "Рыба"); + } else { + throw new Exception("Неизвестный вид животного, используйте значение Травоядное или Хищник"); + } + } + public String getFamily() { + return "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи"; + } } \ No newline at end of file diff --git a/src/main/java/com/example/Cat.java b/src/main/java/com/example/Cat.java index cc0dd35..122741c 100644 --- a/src/main/java/com/example/Cat.java +++ b/src/main/java/com/example/Cat.java @@ -1,20 +1,23 @@ package com.example; - import java.util.List; public class Cat { - private Predator predator; + private final Feline feline; public Cat(Feline feline) { - this.predator = feline; + this.feline = feline; } public String getSound() { return "Мяу"; } + public int getKittens() { + return feline.getKittens(); + } + public List getFood() throws Exception { - return predator.eatMeat(); + return feline.eatMeat(); } } \ No newline at end of file diff --git a/src/main/java/com/example/Feline.java b/src/main/java/com/example/Feline.java index 3630cfd..dd73479 100644 --- a/src/main/java/com/example/Feline.java +++ b/src/main/java/com/example/Feline.java @@ -21,4 +21,5 @@ public int getKittens() { public int getKittens(int kittensCount) { return kittensCount; } + } \ No newline at end of file diff --git a/src/main/java/com/example/Lion.java b/src/main/java/com/example/Lion.java index 1ad6354..1ee9e5a 100644 --- a/src/main/java/com/example/Lion.java +++ b/src/main/java/com/example/Lion.java @@ -4,8 +4,8 @@ public class Lion { - private boolean hasMane; private Feline feline; + private final boolean hasMane; public Lion(String sex, Feline feline) throws Exception { if ("Самец".equals(sex)) { @@ -15,17 +15,17 @@ public Lion(String sex, Feline feline) throws Exception { } else { throw new Exception("Используйте допустимые значения пола животного - Самец или Самка"); } - this.feline = feline; // dependency injection - } - - public int getKittens() { - return feline.getKittens(); + this.feline = feline; } public boolean doesHaveMane() { return hasMane; } + public int getKittens() throws Exception { + return feline.getKittens(); + } + public List getFood() throws Exception { return feline.eatMeat(); } diff --git a/src/main/java/com/example/Predator.java b/src/main/java/com/example/Predator.java index 02e6cfd..65fbdc6 100644 --- a/src/main/java/com/example/Predator.java +++ b/src/main/java/com/example/Predator.java @@ -5,5 +5,4 @@ public interface Predator { List eatMeat() throws Exception; - } diff --git a/src/test/java/com/example/AnimalTest.java b/src/test/java/com/example/AnimalTest.java new file mode 100644 index 0000000..979af1f --- /dev/null +++ b/src/test/java/com/example/AnimalTest.java @@ -0,0 +1,45 @@ +package com.example; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.List; + +class AnimalTest { + + private Animal animal; + + @BeforeEach + void setUp() { + animal = new Animal(); + } + + @Test + void testGetFamily() { + String expected = "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи"; + String actual = animal.getFamily(); + assertEquals(expected, actual); + } + + @Test + void testGetFoodPredator() throws Exception { + List expected = List.of("Животные", "Птицы", "Рыба"); + List actual = animal.getFood("Хищник"); + assertEquals(expected, actual); + } + + @Test + void testGetFoodHerbivore() throws Exception { + List expected = List.of("Трава", "Различные растения"); + List actual = animal.getFood("Травоядное"); + assertEquals(expected, actual); + } + + @Test + void testGetFoodUnknown() { + Exception exception = assertThrows(Exception.class, () -> { + animal.getFood("Неизвестное"); + }); + assertTrue(exception.getMessage().contains("Неизвестный вид животного")); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/CatTest.java b/src/test/java/com/example/CatTest.java index 304d6c7..0a00b38 100644 --- a/src/test/java/com/example/CatTest.java +++ b/src/test/java/com/example/CatTest.java @@ -1,28 +1,45 @@ package com.example; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; public class CatTest { + private Feline mockFeline; + private Cat cat; + + @BeforeEach + void setUp() { + // Создаем мок Feline + mockFeline = mock(Feline.class); + // Передаем мок в Cat + cat = new Cat(mockFeline); + } + @Test - public void getSoundAlwaysMiau() { - Feline felineMock = mock(Feline.class); - Cat cat = new Cat(felineMock); + void testGetSound() { assertEquals("Мяу", cat.getSound()); } @Test - public void getFoodUsesFelineMock() throws Exception { - Feline felineMock = mock(Feline.class); - when(felineMock.eatMeat()).thenReturn(List.of("Рыба", "Птицы")); + void testGetKittens() { + when(mockFeline.getKittens()).thenReturn(1); // обязательно для мока + assertEquals(1, cat.getKittens()); + verify(mockFeline, times(1)).getKittens(); + } - Cat cat = new Cat(felineMock); - List food = cat.getFood(); + @Test + void testGetFood() throws Exception { + List expected = List.of("Животные", "Птицы", "Рыба"); + when(mockFeline.eatMeat()).thenReturn(expected); // обязательно для мока - assertEquals(List.of("Рыба", "Птицы"), food); - verify(felineMock, times(1)).eatMeat(); + List actual = cat.getFood(); + assertEquals(expected, actual); + verify(mockFeline, times(1)).eatMeat(); } } \ No newline at end of file diff --git a/src/test/java/com/example/FelineTest.java b/src/test/java/com/example/FelineTest.java index 254e48c..5caad49 100644 --- a/src/test/java/com/example/FelineTest.java +++ b/src/test/java/com/example/FelineTest.java @@ -56,6 +56,7 @@ public void eatMeatReturnsPredatorFoodList() throws Exception { @Test + public void getFamilyReturnsCorrectFamily() { assertEquals("Кошачьи", feline.getFamily()); } diff --git a/src/test/java/com/example/LionParameterizedTest.java b/src/test/java/com/example/LionParameterizedTest.java deleted file mode 100644 index 82cc77a..0000000 --- a/src/test/java/com/example/LionParameterizedTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -class LionParameterizedTest { - - @ParameterizedTest - @CsvSource({ - "Самец, true", - "Самка, false" - }) - void doesHaveManeReturnsCorrectly(String sex, boolean expectedHasMane) throws Exception { - Feline felineMock = mock(Feline.class); - Lion lion = new Lion(sex, felineMock); - assertEquals(expectedHasMane, lion.doesHaveMane()); - } -} \ No newline at end of file diff --git a/src/test/java/com/example/LionTest.java b/src/test/java/com/example/LionTest.java index f8fc6ed..49733b6 100644 --- a/src/test/java/com/example/LionTest.java +++ b/src/test/java/com/example/LionTest.java @@ -1,54 +1,55 @@ package com.example; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + import java.util.List; + import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class LionTest { - @Test - void doesHaveManeReturnsTrueForMale() throws Exception { - Feline felineMock = mock(Feline.class); - Lion lion = new Lion("Самец", felineMock); - assertTrue(lion.doesHaveMane()); + @ParameterizedTest + @CsvSource({ + "Самец, true", + "Самка, false" + }) + void doesHaveManeReturnsCorrectly(String sex, boolean expectedHasMane) throws Exception { + Feline mockFeline = mock(Feline.class); + Lion lion = new Lion(sex, mockFeline); + assertEquals(expectedHasMane, lion.doesHaveMane()); } @Test - void doesHaveManeReturnsFalseForFemale() throws Exception { - Feline felineMock = mock(Feline.class); - Lion lion = new Lion("Самка", felineMock); - assertFalse(lion.doesHaveMane()); + void testInvalidSexThrowsException() { + Feline mockFeline = mock(Feline.class); + Exception exception = assertThrows(Exception.class, () -> { + new Lion("Неизвестно", mockFeline); + }); + assertEquals("Используйте допустимые значения пола животного - Самец или Самка", exception.getMessage()); } - @Test - void getFoodCallsFelineEatMeat() throws Exception { - Feline felineMock = mock(Feline.class); - when(felineMock.eatMeat()).thenReturn(List.of("Животные", "Птицы", "Рыба")); - - Lion lion = new Lion("Самец", felineMock); - List food = lion.getFood(); - - assertEquals(List.of("Животные", "Птицы", "Рыба"), food); - verify(felineMock, times(1)).eatMeat(); - } @Test - void getKittensCallsFeline() throws Exception { - Feline felineMock = mock(Feline.class); - when(felineMock.getKittens()).thenReturn(3); + void testGetKittens() throws Exception { + Feline mockFeline = mock(Feline.class); + when(mockFeline.getKittens()).thenReturn(2); - Lion lion = new Lion("Самец", felineMock); - int kittens = lion.getKittens(); - - assertEquals(3, kittens); - verify(felineMock, times(1)).getKittens(); + Lion lion = new Lion("Самец", mockFeline); + assertEquals(2, lion.getKittens()); + verify(mockFeline, times(1)).getKittens(); } @Test - void constructorThrowsExceptionForInvalidSex() { - Feline felineMock = mock(Feline.class); - Exception exception = assertThrows(Exception.class, () -> new Lion("Неизвестно", felineMock)); - assertEquals("Используйте допустимые значения пола животного - Самец или Самка", exception.getMessage()); + void testGetFood() throws Exception { + Feline mockFeline = mock(Feline.class); + when(mockFeline.eatMeat()).thenReturn(List.of("Животные", "Птицы", "Рыба")); + + Lion lion = new Lion("Самец", mockFeline); + List actualFood = lion.getFood(); + assertEquals(List.of("Животные", "Птицы", "Рыба"), actualFood); + verify(mockFeline, times(1)).eatMeat(); } } \ No newline at end of file diff --git a/target/classes/read.me b/target/classes/read.me new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/target/classes/read.me @@ -0,0 +1 @@ +