-
Notifications
You must be signed in to change notification settings - Fork 0
Создание тестов и изменения #5
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,4 +18,4 @@ public List<String> getFood() throws Exception { | |
| return predator.eatMeat(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,4 +22,4 @@ public int getKittens(int kittensCount) { | |
| return kittensCount; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,4 @@ public interface Predator { | |
|
|
||
| List<String> eatMeat() throws Exception; | ||
|
|
||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| } |
| 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("псовые")); | ||
| } | ||
| } |
| 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) | ||
| } | ||
| } |
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.
Это нужно доработать:
нужно вернуть исходную реализацию