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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IDE files
.idea/

# Maven build output
target/
43 changes: 41 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

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

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -25,7 +40,31 @@
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</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>

</project>
</project>
23 changes: 23 additions & 0 deletions src/main/java/com/example/Alex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;
}
}
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 @@ -4,7 +4,7 @@

public class Cat {

Predator predator;
private final Predator predator;

public Cat(Feline feline) {
this.predator = feline;
Expand Down
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 @@ -4,9 +4,11 @@

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)) {
Expand All @@ -16,8 +18,6 @@ public Lion(String sex) throws Exception {
}
}

Feline feline = new Feline();

public int getKittens() {
return feline.getKittens();
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/example/AlexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class AlexTest {

@Test
public void getFriendsReturnsAlexFriends() throws Exception {
Alex alex = new Alex(mock(Feline.class));

assertEquals(List.of("Марти", "Глория", "Мелман"), alex.getFriends());
}

@Test
public void getPlaceOfLivingReturnsNewYorkZoo() throws Exception {
Alex alex = new Alex(mock(Feline.class));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️Можно улучшить. Инициализацию объекта можно вынести в метод с аннотацией before


assertEquals("Нью-Йоркский зоопарк", alex.getPlaceOfLiving());
}

@Test
public void getKittensReturnsZero() throws Exception {
Alex alex = new Alex(mock(Feline.class));

assertEquals(0, alex.getKittens());
}

@Test
public void doesHaveManeReturnsTrue() throws Exception {
Alex alex = new Alex(mock(Feline.class));

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class AnimalParameterizedTest {

private final String animalKind;
private final List<String> expectedFood;

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

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> getFoodData() {
return Arrays.asList(new Object[][]{
{"Травоядное", List.of("Трава", "Различные растения")},
{"Хищник", List.of("Животные", "Птицы", "Рыба")}
});
}

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

assertEquals(expectedFood, animal.getFood(animalKind));
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/example/AnimalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class AnimalTest {

@Test
public void getFamilyReturnsAnimalFamilies() {
Animal animal = new Animal();

assertEquals("Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи", animal.getFamily());
}

@Test
public void getFoodThrowsExceptionForUnknownAnimalKind() {
Animal animal = new Animal();

assertThrows(Exception.class, () -> animal.getFood("Неизвестный вид"));
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/example/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CatTest {

@Test
public void getSoundReturnsMeow() {
Cat cat = new Cat(mock(Feline.class));

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

@Test
public void getFoodReturnsPredatorFood() throws Exception {
Feline feline = mock(Feline.class);
List<String> expectedFood = List.of("Животные", "Птицы", "Рыба");
when(feline.eatMeat()).thenReturn(expectedFood);
Cat cat = new Cat(feline);

assertEquals(expectedFood, 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.

⛔️Нужно исправить. Для юнит-тестов применим подход: один тест, значит одна проверка. В этом тесте две проверки (Mockito.verify, assertEquals), а должна быть одна. Исправь, пожалуйста, этот момент во всем коде.

verify(feline).eatMeat();
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/example/FelineParameterizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class FelineParameterizedTest {

private final int kittensCount;

public FelineParameterizedTest(int kittensCount) {
this.kittensCount = kittensCount;
}

@Parameterized.Parameters(name = "kittensCount={0}")
public static Collection<Object[]> getKittensData() {
return Arrays.asList(new Object[][]{
{0},
{1},
{5}
});
}

@Test
public void getKittensReturnsKittensCount() {
Feline feline = new Feline();

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

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;

public class FelineTest {

@Test
public void eatMeatReturnsPredatorFood() throws Exception {
Feline feline = new Feline();

assertEquals(List.of("Животные", "Птицы", "Рыба"), feline.eatMeat());
}

@Test
public void getFamilyReturnsCatFamily() {
Feline feline = new Feline();

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

@Test
public void getKittensWithoutArgumentsReturnsOne() {
Feline feline = new Feline();

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

@RunWith(Parameterized.class)
public class LionParameterizedTest {

private final String sex;
private final boolean hasMane;

public LionParameterizedTest(String sex, boolean hasMane) {
this.sex = sex;
this.hasMane = hasMane;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> getManeData() {
return Arrays.asList(new Object[][]{
{"Самец", true},
{"Самка", false}
});
}

@Test
public void doesHaveManeReturnsExpectedValueBySex() throws Exception {
Lion lion = new Lion(sex, mock(Feline.class));

assertEquals(hasMane, lion.doesHaveMane());
}
}
Loading