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.

69 changes: 60 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<artifactId>qa_java</artifactId>
<version>1.0-SNAPSHOT</version>

<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>
<java.version>11</java.version>
<junit.version>4.13.2</junit.version>
<mockito.version>4.11.0</mockito.version>
<jacoco.version>0.8.8</jacoco.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -23,9 +38,45 @@
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/site/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
2 changes: 1 addition & 1 deletion src/main/java/com/example/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ public List<String> getFood() throws Exception {
return predator.eatMeat();
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/Feline.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public int getKittens(int kittensCount) {
return kittensCount;
}

}
}
7 changes: 4 additions & 3 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +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() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это нужно доработать:

нужно вернуть исходную реализацию

return feline.getKittens();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/Predator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public interface Predator {

List<String> eatMeat() throws Exception;

}
}
56 changes: 56 additions & 0 deletions src/test/java/com/example/AnimalParameterizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example;

import com.example.Animal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)

public class AnimalParameterizedTest {
private final String animalKind;
private final List<String> expectedFood;
private final boolean shouldThrowException;

public AnimalParameterizedTest(String animalKind, List<String> expectedFood, boolean shouldThrowException) {
this.animalKind = animalKind;
this.expectedFood = expectedFood;
this.shouldThrowException = shouldThrowException;
}

@Parameterized.Parameters(name = "Вид: {0}")
public static Object[][] getAnimalKindData() {
return new Object[][] {
{"Травоядное", Arrays.asList("Трава", "Различные растения"), false},
{"Хищник", Arrays.asList("Животные", "Птицы", "Рыба"), false},
{"Всеядное", null, true},
{"Неизвестный", null, true},
{"", null, true},
{null, null, true}
};
}

@Test
public void testGetFood() throws Exception {
Animal animal = new Animal();

if (shouldThrowException) {
try {
animal.getFood(animalKind);
fail("Должно быть выброшено исключение для вида: " + animalKind);
} catch (Exception e) {
assertTrue("Сообщение об ошибке должно содержать 'Неизвестный вид животного'",
e.getMessage().contains("Неизвестный вид животного"));
}
} else {
List<String> actualFood = animal.getFood(animalKind);
assertEquals("Еда для вида " + animalKind + " должна соответствовать ожидаемой",
expectedFood, actualFood);
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/example/AnimalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example;

import org.junit.Test;
import static org.junit.Assert.*;

public class AnimalTest {
@Test
public void testGetFamilyContainsSeveralFamilies() {
com.example.Animal animal = new com.example.Animal(); // Полное имя
String family = animal.getFamily();
assertTrue("Описание семейства должно содержать 'несколько семейств'",
family.contains("несколько семейств"));
}

@Test
public void testGetFamilyContainsFeline() {
com.example.Animal animal = new com.example.Animal();
String family = animal.getFamily();
assertTrue("Описание должно содержать 'кошачьи'",
family.contains("кошачьи"));
}

@Test
public void testGetFamilyContainsCanine() {
com.example.Animal animal = new com.example.Animal();
String family = animal.getFamily();
assertTrue("Описание должно содержать 'псовые'",
family.contains("псовые"));
}
}
77 changes: 77 additions & 0 deletions src/test/java/com/example/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.example;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class CatTest {
// Мок зависимости: Cat внутри хранит Predator, а Feline реализует Predator
@Mock
private Feline feline;

private Cat cat;

@Before
public void setUp() {
// Given: кот, которому передали зависимость
cat = new Cat(feline);
}

@Test
public void shouldReturnMeowWhenGetSoundCalled() {

// When: вызываем метод getSound()
String sound = cat.getSound();

// Then: кот должен сказать "Мяу"
assertEquals("Мяу", sound);
}

@Test
public void shouldReturnFoodWhenGetFoodCalled() throws Exception {

// Given: зависимость возвращает список еды
List<String> expectedFood = List.of("Животные", "Птицы", "Рыба");
Mockito.when(feline.eatMeat()).thenReturn(expectedFood);

// When: вызываем getFood()
List<String> actualFood = cat.getFood();

// Then: возвращается ожидаемый результат
assertEquals(expectedFood, actualFood);
}

@Test
public void shouldCallEatMeatWhenGetFoodCalled() throws Exception {

// Given: зависимость настроена
Mockito.when(feline.eatMeat()).thenReturn(List.of("Животные"));

// When: вызываем getFood()
cat.getFood();

// Then: кот обращается к зависимости
Mockito.verify(feline).eatMeat();
}

@Test(expected = Exception.class)
public void shouldThrowExceptionWhenPredatorThrowsException() throws Exception {

// Given: зависимость выбрасывает Exception при попытке получить еду
Mockito.when(feline.eatMeat()).thenThrow(
new Exception("Не удалось получить еду")
);

// When: вызываем getFood()
cat.getFood();
// Then @Test(expected = Exception.class)
}
}
Loading