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.

7 changes: 7 additions & 0 deletions .idea/encodings.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.

45 changes: 44 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,64 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>

<!-- Фреймворк для модульного тестирования и Мокито для мок-объектов -->

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.12.1</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>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<!-- Плагин JaCoCo -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
10 changes: 4 additions & 6 deletions src/main/java/com/example/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import java.util.List;

public class Cat {
private Predator predator;

Predator predator;

public Cat(Feline feline) {
this.predator = feline;
public Cat(Predator predator) {
this.predator = predator;
}

public String getSound() {
Expand All @@ -17,5 +16,4 @@ public String getSound() {
public List<String> getFood() throws Exception {
return predator.eatMeat();
}

}
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/Feline.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public String getFamily() {
return "Кошачьи";
}

@Override
public int getKittens() {
return getKittens(1);
}

public int getKittens(int kittensCount) {
return kittensCount;
}

}
}
17 changes: 8 additions & 9 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
import java.util.List;

public class Lion {
private boolean hasMane;
private Predator predator;

boolean hasMane;

public Lion(String sex) throws Exception {
public Lion(String sex, Predator predator) throws Exception {
if ("Самец".equals(sex)) {
hasMane = true;
} else if ("Самка".equals(sex)) {
hasMane = false;
} else {
throw new Exception("Используйте допустимые значения пола животного - самей или самка");
throw new Exception("Используйте допустимые значения пола животного - самец или самка");
}
this.predator = predator;
}

Feline feline = new Feline();

public int getKittens() {
return feline.getKittens();
return predator.getKittens();
}

public boolean doesHaveMane() {
return hasMane;
}

public List<String> getFood() throws Exception {
return feline.getFood("Хищник");
return predator.eatMeat();
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/example/Predator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.List;

public interface Predator {

List<String> eatMeat() throws Exception;

int getKittens();
default String getFamily() {
return "Неизвестное семейство";
}
}
51 changes: 51 additions & 0 deletions src/test/java/com/example/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class CatTest {

@Mock
private Predator predator;

@Test
void getSoundShouldReturnMeow() {
Cat cat = new Cat(predator);

assertEquals("Мяу", cat.getSound());
}

@Test
void getFoodShouldCallEatMeatOnPredator() throws Exception {
Cat cat = new Cat(predator);
List<String> expectedFood = List.of("Животные", "Птицы", "Рыба");

when(predator.eatMeat()).thenReturn(expectedFood);

List<String> actualFood = cat.getFood();

assertEquals(expectedFood, actualFood);
verify(predator, times(1)).eatMeat();
}

@Test
void getFoodShouldPropagateException() throws Exception {
Cat cat = new Cat(predator);
Exception expectedException = new Exception("Ошибка получения еды");

when(predator.eatMeat()).thenThrow(expectedException);

Exception actualException = assertThrows(Exception.class, cat::getFood);

assertEquals(expectedException, actualException);
verify(predator, times(1)).eatMeat();
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/example/FelineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class FelineTest {

@Test
@DisplayName("Тест eatMeat возвращает правильный список еды для хищника")
void eatMeatShouldReturnPredatorFood() throws Exception {
Feline feline = new Feline();
List<String> expectedFood = List.of("Животные", "Птицы", "Рыба");

assertEquals(expectedFood, feline.eatMeat());
}

@Test
@DisplayName("Тест getFamily возвращает 'Кошачьи'")
void getFamilyShouldReturnFelidae() {
Feline feline = new Feline();

assertEquals("Кошачьи", feline.getFamily());
}

@Test
@DisplayName("Тест getKittens без параметра возвращает 1")
void getKittensWithoutParametersShouldReturnOne() {
Feline feline = new Feline();

assertEquals(1, feline.getKittens());
}

@Test
@DisplayName("Тест getKittens с параметром возвращает переданное значение")
void getKittensWithParameterShouldReturnGivenValue() {
Feline feline = new Feline();
int kittensCount = 5;

assertEquals(kittensCount, feline.getKittens(kittensCount));
}
}
67 changes: 67 additions & 0 deletions src/test/java/com/example/LionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.example;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class LionTest {

@Mock
private Predator predator;

@Test
void lionConstructorShouldSetManeForMale() throws Exception {
Lion lion = new Lion("Самец", predator);

assertTrue(lion.doesHaveMane());
}

@Test
void lionConstructorShouldNotSetManeForFemale() throws Exception {
Lion lion = new Lion("Самка", predator);

assertFalse(lion.doesHaveMane());
}

@Test
void lionConstructorShouldThrowExceptionForInvalidSex() {
Exception exception = assertThrows(Exception.class,
() -> new Lion("Неизвестный", predator));

assertEquals("Используйте допустимые значения пола животного - самец или самка",
exception.getMessage());
}

@Test
void getKittensShouldCallPredatorGetKittens() throws Exception {
Lion lion = new Lion("Самец", predator);
int expectedKittens = 3;

when(predator.getKittens()).thenReturn(expectedKittens);

int actualKittens = lion.getKittens();

assertEquals(expectedKittens, actualKittens);
verify(predator, times(1)).getKittens();
}

@Test
void getFoodShouldCallPredatorEatMeat() throws Exception {
Lion lion = new Lion("Самка", predator);
List<String> expectedFood = List.of("Животные", "Птицы", "Рыба");

when(predator.eatMeat()).thenReturn(expectedFood);

List<String> actualFood = lion.getFood();

assertEquals(expectedFood, actualFood);
verify(predator, times(1)).eatMeat();
}
}
Loading