-
Notifications
You must be signed in to change notification settings - Fork 0
Sprint 6 final push #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
17ac5c7
12495aa
d8fea5f
0ece17b
63f6112
a417aaa
31839c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 "Существует несколько семейств: заячьи, беличьи, мышиные, кошачьи, псовые, медвежьи, куньи"; | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,4 +21,5 @@ public int getKittens() { | |
| public int getKittens(int kittensCount) { | ||
| return kittensCount; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,4 @@ | |
| public interface Predator { | ||
|
|
||
| List<String> eatMeat() throws Exception; | ||
|
|
||
| } | ||
| 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("Неизвестный вид животного")); | ||
| } | ||
| } |
| 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()); | ||
| 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(); | ||
| } | ||
| } | ||
This file was deleted.
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛔️Нужно исправить. Нет отчета о тестировании |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⛔️Нужно исправить. Для юнит-тестов применим подход: один тест, значит одна проверка. В этом тесте две проверки (Mockito.verify, assertEquals), а должна быть одна. Исправь, пожалуйста, этот момент во всем коде.