forked from yandex-praktikum/qa-java-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Часть 1, unit-тесты и отчет о 100% покрытии JaCoCo #1
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
Open
fe-y
wants to merge
5
commits into
main
Choose a base branch
from
develop1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79ebe15
Часть 1, unit-тесты и отчет о 100% покрытии JaCoCo
3d2cf0c
Доработка 1
7cae8f0
Исправлены тесты BurgerTest, обновлён pom.xml, добавлен JaCoCo отчёт
7fd9580
Удалена папка .idea из GitHub, добавлено в .gitignore
d343960
Доработка 2
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,95 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.example</groupId> | ||
| <artifactId>praktikum</artifactId> | ||
| <artifactId>QA-java-diplom-1</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <junit.version>4.13.2</junit.version> | ||
| <mockito.version>4.6.1</mockito.version> | ||
| <jacoco.version>0.8.10</jacoco.version> | ||
| </properties> | ||
|
|
||
| </project> | ||
| <build> | ||
| <plugins> | ||
| <!-- Surefire Plugin для запуска тестов с JUnit 4 --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.0.0-M9</version> | ||
| <configuration> | ||
| <argLine>-javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${project.build.directory}/jacoco.exec</argLine> | ||
| <includes> | ||
| <include>**/*Test.java</include> | ||
| </includes> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <!-- Jacoco 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> | ||
| <configuration> | ||
| <outputDirectory>${project.build.directory}/site/jacoco</outputDirectory> | ||
| <dataFile>${project.build.directory}/jacoco.exec</dataFile> | ||
| <formats> | ||
| <format>HTML</format> | ||
| </formats> | ||
|
|
||
| <includes> | ||
| <include>praktikum/Bun.class</include> | ||
| <include>praktikum/Burger.class</include> | ||
| <include>praktikum/Ingredient.class</include> | ||
| <include>praktikum/IngredientType.class</include> | ||
| </includes> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <dependencies> | ||
| <!-- JUnit 4 --> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- Mockito --> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <version>5.4.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- JUnitParams --> | ||
| <dependency> | ||
| <groupId>pl.pragmatists</groupId> | ||
| <artifactId>JUnitParams</artifactId> | ||
| <version>1.1.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| package praktikum; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import junitparams.JUnitParamsRunner; | ||
| import junitparams.Parameters; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| import static org.junit.Assert.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @RunWith(JUnitParamsRunner.class) | ||
| public class BurgerTest { | ||
|
|
||
| // ====== Константы ====== | ||
| private static final float BUN_PRICE_SMALL = 100f; | ||
| private static final float BUN_PRICE_MEDIUM = 150f; | ||
| private static final float BUN_PRICE_LARGE = 200f; | ||
|
|
||
| private static final float SAUCE_PRICE = 50f; | ||
| private static final float FILLING_PRICE = 75f; | ||
|
|
||
| // ====== Моки ====== | ||
| private Burger burger; | ||
| private Bun bunMock; | ||
| private Ingredient sauceIngredient; | ||
| private Ingredient fillingIngredient; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| burger = new Burger(); | ||
| bunMock = mock(Bun.class); | ||
| sauceIngredient = mock(Ingredient.class); | ||
| fillingIngredient = mock(Ingredient.class); | ||
|
|
||
| when(bunMock.getName()).thenReturn("Test Bun"); | ||
| when(bunMock.getPrice()).thenReturn(BUN_PRICE_SMALL); | ||
|
|
||
| when(sauceIngredient.getName()).thenReturn("Tomato Sauce"); | ||
| when(sauceIngredient.getPrice()).thenReturn(SAUCE_PRICE); | ||
| when(sauceIngredient.getType()).thenReturn(IngredientType.SAUCE); | ||
|
|
||
| when(fillingIngredient.getName()).thenReturn("Beef Patty"); | ||
| when(fillingIngredient.getPrice()).thenReturn(FILLING_PRICE); | ||
| when(fillingIngredient.getType()).thenReturn(IngredientType.FILLING); | ||
| } | ||
|
|
||
| // ====== Тесты булки ====== | ||
| @Test | ||
| public void testSetBun() { | ||
| burger.setBuns(bunMock); | ||
| assertEquals(bunMock, burger.bun); | ||
| } | ||
|
|
||
| // ====== Тесты добавления ингредиентов ====== | ||
| @Test | ||
| public void testAddSauceIngredient() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(sauceIngredient); | ||
| assertEquals(sauceIngredient, burger.ingredients.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddFillingIngredient() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(fillingIngredient); | ||
| assertEquals(fillingIngredient, burger.ingredients.get(0)); | ||
| } | ||
|
|
||
| // ====== Тесты удаления ингредиентов ====== | ||
| @Test | ||
| public void testRemoveIngredientKeepsCorrectIngredient() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(sauceIngredient); | ||
| burger.addIngredient(fillingIngredient); | ||
|
|
||
| burger.removeIngredient(0); | ||
| assertEquals(fillingIngredient, burger.ingredients.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveIngredientListSize() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(sauceIngredient); | ||
| burger.addIngredient(fillingIngredient); | ||
|
|
||
| burger.removeIngredient(0); | ||
| assertEquals(1, burger.ingredients.size()); | ||
| } | ||
|
|
||
| // ====== Тесты перемещения ингредиентов ====== | ||
| @Test | ||
| public void testMoveIngredientFirstPosition() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(sauceIngredient); | ||
| burger.addIngredient(fillingIngredient); | ||
|
|
||
| burger.moveIngredient(0, 1); | ||
| assertEquals(fillingIngredient, burger.ingredients.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMoveIngredientSecondPosition() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(sauceIngredient); | ||
| burger.addIngredient(fillingIngredient); | ||
|
|
||
| burger.moveIngredient(0, 1); | ||
| assertEquals(sauceIngredient, burger.ingredients.get(1)); | ||
| } | ||
|
|
||
| // ====== Параметризированные тесты цены ====== | ||
| private Object[] parametersForBurgerPriceTest() { | ||
| Ingredient sauce = mock(Ingredient.class); | ||
| when(sauce.getPrice()).thenReturn(SAUCE_PRICE); | ||
|
|
||
| Ingredient filling = mock(Ingredient.class); | ||
| when(filling.getPrice()).thenReturn(FILLING_PRICE); | ||
|
|
||
| return new Object[]{ | ||
| new Object[]{BUN_PRICE_SMALL, new Ingredient[]{sauce, filling}, 325f}, | ||
| new Object[]{BUN_PRICE_LARGE, new Ingredient[]{sauce}, 450f}, | ||
| new Object[]{BUN_PRICE_MEDIUM, new Ingredient[]{}, 300f} | ||
| }; | ||
| } | ||
|
|
||
| @Test | ||
| @Parameters(method = "parametersForBurgerPriceTest") | ||
| public void testGetPriceParameterized(float bunPrice, Ingredient[] ingredients, float expectedPrice) { | ||
| Bun bun = mock(Bun.class); | ||
| when(bun.getPrice()).thenReturn(bunPrice); | ||
| when(bun.getName()).thenReturn("Test Bun"); | ||
|
|
||
| Burger burger = new Burger(); | ||
| burger.setBuns(bun); | ||
|
|
||
| for (Ingredient ingredient : ingredients) { | ||
| burger.addIngredient(ingredient); | ||
| } | ||
|
|
||
| assertEquals(expectedPrice, burger.getPrice(), 0.01); | ||
| } | ||
|
|
||
| // ====== Параметризированные тесты добавления ингредиентов ====== | ||
| private Object[] parametersForAddIngredientTest() { | ||
| return new Object[]{ | ||
| new Object[]{sauceIngredient}, | ||
| new Object[]{fillingIngredient} | ||
| }; | ||
| } | ||
|
|
||
| @Test | ||
| @Parameters(method = "parametersForAddIngredientTest") | ||
| public void testAddIngredientParameterized(Ingredient ingredient) { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(ingredient); | ||
| assertTrue(burger.ingredients.contains(ingredient)); | ||
| } | ||
|
|
||
| // ====== Тесты рецепта ====== | ||
| @Test | ||
| public void testGetReceiptFull() { | ||
| burger.setBuns(bunMock); | ||
| burger.addIngredient(sauceIngredient); | ||
| burger.addIngredient(fillingIngredient); | ||
|
|
||
| String expectedReceipt = String.format(Locale.US, | ||
| "(==== %s ====)%n" + | ||
| "= %s %s =%n" + | ||
| "= %s %s =%n" + | ||
| "(==== %s ====)%n" + | ||
| "%nPrice: %.2f%n", | ||
| bunMock.getName(), | ||
| sauceIngredient.getType().toString().toLowerCase(), sauceIngredient.getName(), | ||
| fillingIngredient.getType().toString().toLowerCase(), fillingIngredient.getName(), | ||
| bunMock.getName(), | ||
| burger.getPrice() | ||
| ); | ||
|
|
||
| assertEquals(expectedReceipt, burger.getReceipt()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetReceiptEmptyIngredients() { | ||
| burger.setBuns(bunMock); | ||
| String expectedReceipt = String.format(Locale.US, | ||
| "(==== %s ====)%n" + | ||
| "(==== %s ====)%n%n" + | ||
| "Price: %.2f%n", | ||
| bunMock.getName(), | ||
| bunMock.getName(), | ||
| burger.getPrice()); | ||
| assertEquals(expectedReceipt, burger.getReceipt()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetReceiptWithoutBun() { | ||
| burger.addIngredient(sauceIngredient); | ||
| burger.addIngredient(fillingIngredient); | ||
|
|
||
| try { | ||
| burger.getReceipt(); | ||
| fail("Expected NullPointerException when bun is null"); | ||
| } catch (NullPointerException ignored) {} | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetPriceWithoutBun() { | ||
| assertEquals(0f, burger.getPrice(), 0.01); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
⛔️Нужно исправить. Для юнит-тестов придерживаемся подхода: один тест, значит одна проверка. Если очень хочется несколько проверок -- тогда используем softAssertions. Поправь, пожалуйста, во всем коде