Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 45 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,59 @@
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.17.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.13.0</version>
<configuration>
<source>11</source>
<target>11</target>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>

</project>
</project>
20 changes: 20 additions & 0 deletions src/main/java/com/example/Alex.java
Original file line number Diff line number Diff line change
@@ -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<String> getFriends() {
return List.of("Глория", "Мелман", "Марти");
}
public String getPlaceOfLiving() {
return "Нью-Йоркский зоопарк";
}
@Override
public int getKittens() {
return 0;
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/AlexTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
17 changes: 17 additions & 0 deletions src/test/java/AnimalTests.java
Original file line number Diff line number Diff line change
@@ -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);
}
}



35 changes: 35 additions & 0 deletions src/test/java/CatTests.java
Original file line number Diff line number Diff line change
@@ -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<String> expectedFood = List.of("Животные", "Птицы", "Рыба");
assertEquals(expectedFood, cat.getFood());
}


}
33 changes: 33 additions & 0 deletions src/test/java/FelineTests.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
34 changes: 34 additions & 0 deletions src/test/java/LionTests.java
Original file line number Diff line number Diff line change
@@ -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<String> expectedFood = List.of("Животные", "Птицы", "Рыба");
Mockito.when(feline.getFood("Хищник")).thenReturn(List.of("Животные", "Птицы", "Рыба"));
Lion lion = new Lion("Самец", feline);
assertEquals(expectedFood, lion.getFood());

}
}

42 changes: 42 additions & 0 deletions src/test/java/ParametrizedTestsAnimal.java
Original file line number Diff line number Diff line change
@@ -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<Arguments> provideEatPositive() {
return Stream.of(
Arguments.of("Травоядное", List.of("Трава", "Различные растения")),
Arguments.of("Хищник", List.of("Животные", "Птицы", "Рыба"))
);
}
@ParameterizedTest
@MethodSource("provideEatPositive")
void getFoodPositiveTest(String animalKind, List<String> expected) throws Exception {
Animal animal = new Animal();
List<String> actual = animal.getFood(animalKind);
assertEquals(expected, actual);
}
private static Stream<Arguments> 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());
}
}
Loading