diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..d9e3477 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1465bc1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8177035..a715994 100644 --- a/pom.xml +++ b/pom.xml @@ -13,19 +13,59 @@ 11 + + + org.junit.jupiter + junit-jupiter + 5.11.4 + test + + + org.junit.jupiter + junit-jupiter-params + 5.11.4 + test + + + org.mockito + mockito-junit-jupiter + 5.17.0 + test + + + src/main/java org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.13.0 - 11 - 11 + 11 + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + prepare-agent + prepare-agent + + + report + test + report + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.4 + - - \ No newline at end of file + diff --git a/src/main/java/com/example/Alex.java b/src/main/java/com/example/Alex.java new file mode 100644 index 0000000..e1a7c2a --- /dev/null +++ b/src/main/java/com/example/Alex.java @@ -0,0 +1,20 @@ +package com.example; + +import java.util.List; + +public class Alex extends Lion { + public Alex(Feline feline) throws Exception { + super("Самец",feline); + } + + public List getFriends() { + return List.of("Глория", "Мелман", "Марти"); + } + public String getPlaceOfLiving() { + return "Нью-Йоркский зоопарк"; + } + @Override + public int getKittens() { + return 0; + } +} diff --git a/src/main/java/com/example/Lion.java b/src/main/java/com/example/Lion.java index 8bd39f8..62673aa 100644 --- a/src/main/java/com/example/Lion.java +++ b/src/main/java/com/example/Lion.java @@ -5,19 +5,19 @@ public class Lion { boolean hasMane; + Feline feline; - public Lion(String sex) throws Exception { + public Lion(String sex, Feline feline) throws Exception { if ("Самец".equals(sex)) { hasMane = true; } else if ("Самка".equals(sex)) { hasMane = false; } else { - throw new Exception("Используйте допустимые значения пола животного - самей или самка"); + throw new Exception("Используйте допустимые значения пола животного - самец или самка"); } + this.feline = feline; } - Feline feline = new Feline(); - public int getKittens() { return feline.getKittens(); } diff --git a/src/test/java/AlexTest.java b/src/test/java/AlexTest.java new file mode 100644 index 0000000..1ca3d4f --- /dev/null +++ b/src/test/java/AlexTest.java @@ -0,0 +1,29 @@ +import com.example.Alex; +import com.example.Feline; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AlexTest { + + @Mock + Feline feline; + + @Test + void getKittenTest() throws Exception { + Alex alex = new Alex(feline); + assertEquals(0, alex.getKittens()); + } + @Test + void getPlaceOfLivingTest() throws Exception { + Alex alex = new Alex(feline); + assertEquals("Нью-Йоркский зоопарк", alex.getPlaceOfLiving()); + } + @Test + void getFriendsTest() throws Exception { + Alex alex = new Alex(feline); + assertEquals(List.of("Глория", "Мелман", "Марти"), alex.getFriends()); + } +} diff --git a/src/test/java/AnimalTests.java b/src/test/java/AnimalTests.java new file mode 100644 index 0000000..8d5f20f --- /dev/null +++ b/src/test/java/AnimalTests.java @@ -0,0 +1,17 @@ +import com.example.Animal; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AnimalTests { + + @Test + void getFamilyTest() { + Animal animal = new Animal(); + String actual = animal.getFamily(); + String expected = "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи"; + assertEquals(expected, actual); + } +} + + + diff --git a/src/test/java/CatTests.java b/src/test/java/CatTests.java new file mode 100644 index 0000000..4e355ca --- /dev/null +++ b/src/test/java/CatTests.java @@ -0,0 +1,35 @@ +import com.example.Cat; +import com.example.Feline; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +@ExtendWith(MockitoExtension.class) +public class CatTests { + + @Mock + Feline feline; + + @Test + void getSoundTest() { + Cat cat = new Cat(feline); + String actual = cat.getSound(); + String expected = "Мяу"; + assertEquals(expected, actual); + } + + @Test + void getFoodTest() throws Exception { + Cat cat = new Cat(feline); + Mockito.when(feline.eatMeat()).thenReturn(List.of("Животные", "Птицы", "Рыба")); + List expectedFood = List.of("Животные", "Птицы", "Рыба"); + assertEquals(expectedFood, cat.getFood()); + } + + + } diff --git a/src/test/java/FelineTests.java b/src/test/java/FelineTests.java new file mode 100644 index 0000000..933a7db --- /dev/null +++ b/src/test/java/FelineTests.java @@ -0,0 +1,33 @@ +import com.example.Feline; +import org.junit.jupiter.api.Test; +import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FelineTests { +@Test +void getFamilyTest() { + Feline feline = new Feline(); + String actual = feline.getFamily(); + String expected = "Кошачьи"; + assertEquals(expected, actual); +} +@Test + void getKittensNoParamTest() { + Feline feline = new Feline(); + int actual = feline.getKittens(); + int expected = 1; + assertEquals(expected, actual); +} +@Test + void getKittenWithParamTest() { + Feline feline = new Feline(); + int actual = feline.getKittens(3); + int expected = 3; + assertEquals(expected, actual); +} +@Test + void eatMeatTest() throws Exception { + Feline feline = new Feline(); + assertEquals(List.of("Животные", "Птицы", "Рыба"), feline.eatMeat()); +} +} diff --git a/src/test/java/LionTests.java b/src/test/java/LionTests.java new file mode 100644 index 0000000..9465895 --- /dev/null +++ b/src/test/java/LionTests.java @@ -0,0 +1,34 @@ +import com.example.Feline; +import com.example.Lion; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ExtendWith(MockitoExtension.class) +public class LionTests { + @Mock + Feline feline; + + @Test + void getKittenTest() throws Exception { + Mockito.when(feline.getKittens()).thenReturn(1); + Lion lion = new Lion("Самец", feline); + int actual = lion.getKittens(); + assertEquals(1, actual); + } + @Test + void getFoodTest()throws Exception{ + List expectedFood = List.of("Животные", "Птицы", "Рыба"); + Mockito.when(feline.getFood("Хищник")).thenReturn(List.of("Животные", "Птицы", "Рыба")); + Lion lion = new Lion("Самец", feline); + assertEquals(expectedFood, lion.getFood()); + + } +} + diff --git a/src/test/java/ParametrizedTestsAnimal.java b/src/test/java/ParametrizedTestsAnimal.java new file mode 100644 index 0000000..bef57f8 --- /dev/null +++ b/src/test/java/ParametrizedTestsAnimal.java @@ -0,0 +1,42 @@ +import com.example.Animal; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import java.util.List; +import java.util.stream.Stream; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ParametrizedTestsAnimal { + + + private static Stream provideEatPositive() { + return Stream.of( + Arguments.of("Травоядное", List.of("Трава", "Различные растения")), + Arguments.of("Хищник", List.of("Животные", "Птицы", "Рыба")) + ); + } + @ParameterizedTest + @MethodSource("provideEatPositive") + void getFoodPositiveTest(String animalKind, List expected) throws Exception { + Animal animal = new Animal(); + List actual = animal.getFood(animalKind); + assertEquals(expected, actual); + } + private static Stream provideEatNegative() { + return Stream.of( + Arguments.of("Пу-пу-пу"), + Arguments.of("") + ); + } + @ParameterizedTest + @MethodSource("provideEatNegative") + void getFoodNegativeTest(String animalKind) { + Animal animal = new Animal(); + + Exception exception = assertThrows(Exception.class, + () -> animal.getFood(animalKind)); + assertEquals("Неизвестный вид животного, используйте значение Травоядное или Хищник", + exception.getMessage()); + } +} diff --git a/src/test/java/ParametrizedTestsLion.java b/src/test/java/ParametrizedTestsLion.java new file mode 100644 index 0000000..e37bc48 --- /dev/null +++ b/src/test/java/ParametrizedTestsLion.java @@ -0,0 +1,42 @@ +import com.example.Feline; +import com.example.Lion; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.stream.Stream; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@ExtendWith(MockitoExtension.class) +public class ParametrizedTestsLion { + @Mock + Feline feline; + + private static Stream provideEatPositiveLion() { + return Stream.of( + Arguments.of("Самец", true), + Arguments.of("Самка", false) + ); + } + @ParameterizedTest + @MethodSource("provideEatPositiveLion") + void doesHaveManeTest(String sex, boolean expected) throws Exception { + Lion lion = new Lion(sex, feline); + boolean actual = lion.doesHaveMane(); + assertEquals(expected, actual); + } + @ParameterizedTest + @MethodSource("provideEatPositiveLion") + void getFoodTest(String sex) throws Exception { + Mockito.when(feline.getFood("Хищник")).thenReturn(List.of("Животные", "Птицы", "Рыба")); + Lion lion = new Lion(sex, feline); + assertEquals(List.of("Животные", "Птицы", "Рыба"), lion.getFood()); + } + +} \ 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 @@ +