Skip to content
Draft
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
37 changes: 18 additions & 19 deletions src/main/java/com/example/Animal.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.example;

import java.util.List;

public class Animal {

public List<String> getFood(String animalKind) throws Exception {
if ("Травоядное".equals(animalKind)) {
return List.of("Трава", "Различные растения");
} else if ("Хищник".equals(animalKind)) {
return List.of("Животные", "Птицы", "Рыба");
} else {
throw new Exception("Неизвестный вид животного, используйте значение Травоядное или Хищник");
}
}

public String getFamily() {
return "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи";
}
package com.example;

import java.util.List;

public class Animal {

public List<String> getFood(String animalKind) throws Exception {
if ("Травоядное".equals(animalKind)) {
return List.of("Трава", "Различные растения");
} else if ("Хищник".equals(animalKind)) {
return List.of("Животные", "Птицы", "Рыба");
} else {
throw new Exception("Неизвестный вид животного, используйте значение Травоядное или Хищник");
}
}
public String getFamily() {
return "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи";
}
}
11 changes: 7 additions & 4 deletions src/main/java/com/example/Cat.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.example;

import java.util.List;

public class Cat {

private Predator predator;
private final Feline feline;

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

public String getSound() {
return "Мяу";
}

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

public List<String> getFood() throws Exception {
return predator.eatMeat();
return feline.eatMeat();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/example/Feline.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public int getKittens() {
public int getKittens(int kittensCount) {
return kittensCount;
}

}
12 changes: 6 additions & 6 deletions src/main/java/com/example/Lion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class Lion {

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

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

public int getKittens() {
return feline.getKittens();
this.feline = feline;
}

public boolean doesHaveMane() {
return hasMane;
}

public int getKittens() throws Exception {
return feline.getKittens();
}

public List<String> getFood() throws Exception {
return feline.eatMeat();
}
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
Expand Up @@ -5,5 +5,4 @@
public interface Predator {

List<String> eatMeat() throws Exception;

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;

class AnimalTest {

private Animal animal;

@BeforeEach
void setUp() {
animal = new Animal();
}

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

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

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

@Test
void testGetFoodUnknown() {
Exception exception = assertThrows(Exception.class, () -> {
animal.getFood("Неизвестное");
});
assertTrue(exception.getMessage().contains("Неизвестный вид животного"));
}
}
37 changes: 27 additions & 10 deletions src/test/java/com/example/CatTest.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
package com.example;

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

import java.util.List;

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

public class CatTest {

private Feline mockFeline;
private Cat cat;

@BeforeEach
void setUp() {
// Создаем мок Feline
mockFeline = mock(Feline.class);
// Передаем мок в Cat
cat = new Cat(mockFeline);
}

@Test
public void getSoundAlwaysMiau() {
Feline felineMock = mock(Feline.class);
Cat cat = new Cat(felineMock);
void testGetSound() {
assertEquals("Мяу", cat.getSound());
}

@Test
public void getFoodUsesFelineMock() throws Exception {
Feline felineMock = mock(Feline.class);
when(felineMock.eatMeat()).thenReturn(List.of("Рыба", "Птицы"));
void testGetKittens() {
when(mockFeline.getKittens()).thenReturn(1); // обязательно для мока
assertEquals(1, cat.getKittens());

Choose a reason for hiding this comment

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

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

verify(mockFeline, times(1)).getKittens();
}

Cat cat = new Cat(felineMock);
List<String> food = cat.getFood();
@Test
void testGetFood() throws Exception {
List<String> expected = List.of("Животные", "Птицы", "Рыба");
when(mockFeline.eatMeat()).thenReturn(expected); // обязательно для мока

assertEquals(List.of("Рыба", "Птицы"), food);
verify(felineMock, times(1)).eatMeat();
List<String> actual = cat.getFood();
assertEquals(expected, actual);
verify(mockFeline, times(1)).eatMeat();
}
}
1 change: 1 addition & 0 deletions src/test/java/com/example/FelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void eatMeatReturnsPredatorFoodList() throws Exception {


@Test

public void getFamilyReturnsCorrectFamily() {
assertEquals("Кошачьи", feline.getFamily());
}
Expand Down
21 changes: 0 additions & 21 deletions src/test/java/com/example/LionParameterizedTest.java

This file was deleted.

65 changes: 33 additions & 32 deletions src/test/java/com/example/LionTest.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
package com.example;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.List;

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

class LionTest {

@Test
void doesHaveManeReturnsTrueForMale() throws Exception {
Feline felineMock = mock(Feline.class);
Lion lion = new Lion("Самец", felineMock);
assertTrue(lion.doesHaveMane());
@ParameterizedTest
@CsvSource({
"Самец, true",
"Самка, false"
})
void doesHaveManeReturnsCorrectly(String sex, boolean expectedHasMane) throws Exception {
Feline mockFeline = mock(Feline.class);
Lion lion = new Lion(sex, mockFeline);
assertEquals(expectedHasMane, lion.doesHaveMane());
}

@Test
void doesHaveManeReturnsFalseForFemale() throws Exception {
Feline felineMock = mock(Feline.class);
Lion lion = new Lion("Самка", felineMock);
assertFalse(lion.doesHaveMane());
void testInvalidSexThrowsException() {
Feline mockFeline = mock(Feline.class);
Exception exception = assertThrows(Exception.class, () -> {
new Lion("Неизвестно", mockFeline);
});
assertEquals("Используйте допустимые значения пола животного - Самец или Самка", exception.getMessage());
}

@Test
void getFoodCallsFelineEatMeat() throws Exception {
Feline felineMock = mock(Feline.class);
when(felineMock.eatMeat()).thenReturn(List.of("Животные", "Птицы", "Рыба"));

Lion lion = new Lion("Самец", felineMock);
List<String> food = lion.getFood();

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

@Test
void getKittensCallsFeline() throws Exception {
Feline felineMock = mock(Feline.class);
when(felineMock.getKittens()).thenReturn(3);
void testGetKittens() throws Exception {
Feline mockFeline = mock(Feline.class);
when(mockFeline.getKittens()).thenReturn(2);

Choose a reason for hiding this comment

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

⛔️Нужно исправить. Мок должен вести себя как реальный объект и возвращать 1


Lion lion = new Lion("Самец", felineMock);
int kittens = lion.getKittens();

assertEquals(3, kittens);
verify(felineMock, times(1)).getKittens();
Lion lion = new Lion("Самец", mockFeline);
assertEquals(2, lion.getKittens());
verify(mockFeline, times(1)).getKittens();
}

@Test
void constructorThrowsExceptionForInvalidSex() {
Feline felineMock = mock(Feline.class);
Exception exception = assertThrows(Exception.class, () -> new Lion("Неизвестно", felineMock));
assertEquals("Используйте допустимые значения пола животного - Самец или Самка", exception.getMessage());
void testGetFood() throws Exception {
Feline mockFeline = mock(Feline.class);
when(mockFeline.eatMeat()).thenReturn(List.of("Животные", "Птицы", "Рыба"));

Lion lion = new Lion("Самец", mockFeline);
List<String> actualFood = lion.getFood();
assertEquals(List.of("Животные", "Птицы", "Рыба"), actualFood);
verify(mockFeline, times(1)).eatMeat();
}
}
1 change: 1 addition & 0 deletions target/classes/read.me
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Choose a reason for hiding this comment

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

⛔️Нужно исправить. Нет отчета о тестировании