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

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

45 changes: 45 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.11.16</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

</dependencies>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
Expand All @@ -25,6 +49,27 @@
<target>11</target>
</configuration>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/Animal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.example;

import java.util.List;

public class Animal {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/Cat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.example;

import java.util.List;

public class Cat {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/Feline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.example;

import java.util.List;

public class Feline extends Animal implements Predator {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.example;

import java.util.List;

public class Lion {

boolean hasMane;
private final Feline feline;
private final boolean hasMane;

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

Feline feline = new Feline();

public int getKittens() {
return feline.getKittens();
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/Predator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.example;

import java.util.List;

public interface Predator {
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/AnimalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import com.example.Animal;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class AnimalTest {

@Test
public void getFoodHerbivoreTest() throws Exception {
Animal animal = new Animal();
List<String> expected = List.of("Трава", "Различные растения");
Assert.assertEquals(expected, animal.getFood("Травоядное"));
}

@Test
public void getFoodPredatorTest() throws Exception {
Animal animal = new Animal();
List<String> expected = List.of("Животные", "Птицы", "Рыба");
Assert.assertEquals(expected, animal.getFood("Хищник"));
}

@Test
public void getFoodThrowsExceptionTest() {
Animal animal = new Animal();
try {
animal.getFood("Кот");
Assert.fail("Ожидали Exception");
} catch (Exception e) {
Assert.assertEquals("Неизвестный вид животного, используйте значение Травоядное или Хищник", e.getMessage());
}
}

@Test
public void getFamilyTest() {
Animal animal = new Animal();
Assert.assertTrue(animal.getFamily().contains("кошачьи"));
}
}
31 changes: 31 additions & 0 deletions src/test/java/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import com.example.Cat;
import com.example.Feline;
import org.junit.Assert;
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;

@RunWith(MockitoJUnitRunner.class)
public class CatTest {

@Mock
private Feline feline;

@Test
public void getSoundTest() {
Cat cat = new Cat(feline);
Assert.assertEquals("Мяу", cat.getSound());
}

@Test
public void getFoodTest() throws Exception {
List<String> food = List.of("Мясо");
Mockito.when(feline.eatMeat()).thenReturn(food);
Cat cat = new Cat(feline);
Assert.assertEquals(food, cat.getFood());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Это нужно доработать:
для юнит-тестов применяется подход: один тест - одна проверка. В этом тесте две проверки, нужно оставить одну проверку.
Эта же проблема есть в тестах в классе LionTest.

Так как больше критических замечаний нет , то не буду просить доработать проект.

Но на дипломе тоже нужно будет реализовывать юнит-тесты. Учти этот момент на дипломе.

Mockito.verify(feline).eatMeat();
}
}
Loading