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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Курс Яндекс Практикум "Инженер по тестированию: от новичка до автоматизатора"
## Диплом. Задание 1. Юнит-тесты

### Технологии проекта:
Java 11, Maven 3.9.0, Jacoco 0.8.13, JUnit 4.13.2, Mockito 5.19.0, Assertj 3.27.6

### Как запускать:
`mvn clean test`

### Описание проекта:
Протестирована программа Stellar Burgers, которая помогает собрать свой бургер и сделать заказ.

- собран Maven-проект в IntelliJ IDEA с использованием Java 11 и подключением библиотек JUnit 4, Jacoco, Mockito;
- покрыт юнит-тестами класс Burger;
- применены параметризация и моки;
- сгенерирован отчет с помощью Jacoco для оценки покрытия кода тестами.
66 changes: 64 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,74 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>praktikum</artifactId>
<artifactId>untitled</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>
</properties>

</project>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.13</version>
<executions>
<execution>
<id>prepare-agent</id>
<phase>initialize</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<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>5.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.6</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
75 changes: 75 additions & 0 deletions src/test/java/praktikum/BurgerGetPriceParameterizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package praktikum;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

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

import static org.junit.Assert.assertEquals;


@RunWith(Parameterized.class)
public class BurgerGetPriceParameterizedTest {
private Burger burger;
private float bunPrice;
private float expectedPrice;
private int ingredientCount;

@Mock
private Bun mockBun;

@Mock
private Ingredient mockIngredientSauce;

@Mock
private Ingredient mockIngredientBun;

@Mock
private Ingredient mockIngredientFilling;

public BurgerGetPriceParameterizedTest(float bunPrice, float expectedPrice, int ingredientCount) {
this.bunPrice = bunPrice;
this.expectedPrice = expectedPrice;
this.ingredientCount = ingredientCount;
}

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
burger = new Burger();

Mockito.when(mockBun.getPrice()).thenReturn(bunPrice);

Mockito.when(mockIngredientSauce.getPrice()).thenReturn(50f);
Mockito.when(mockIngredientBun.getPrice()).thenReturn(50f);
Mockito.when(mockIngredientFilling.getPrice()).thenReturn(50f);
}

@Parameterized.Parameters(name = "Цена булочки {0}, Ожидаемая итоговая цена {1}, Количество ингредиентов {2}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{100f, 200f, 0},
{100f, 250f, 1},
{100f, 300f, 2},
{150f, 350f, 1},
{200f, 550f, 3}
});
}

@Test
public void getPriceTest() {
burger.setBuns(mockBun);

if (ingredientCount >= 1) burger.addIngredient(mockIngredientSauce);
if (ingredientCount >= 2) burger.addIngredient(mockIngredientBun);
if (ingredientCount >= 3) burger.addIngredient(mockIngredientFilling);

assertEquals("Неверный расчет цены для булочки" + bunPrice + " и" + ingredientCount + " ингредиентов", expectedPrice, burger.getPrice(), 0.01f);
}
}
102 changes: 102 additions & 0 deletions src/test/java/praktikum/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package praktikum;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.assertj.core.api.SoftAssertions;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;

import static org.junit.Assert.assertEquals;


@RunWith(MockitoJUnitRunner.class)
public class BurgerTest {

private Burger burger;

@Mock
private Bun mockBun;

@Mock
private Ingredient mockIngredientSauce;

@Mock
private Ingredient mockIngredientBun;

@Mock
private Ingredient mockIngredientFilling;

@Before
public void setUp() {
burger = new Burger();
}

@Test
public void setBunsTest() {
burger.setBuns(mockBun);
assertEquals("Установлена неверная булочка", mockBun, burger.bun);
}

@Test
public void addIngredientTest() {
burger.addIngredient(mockIngredientSauce);
assertEquals("Некорректное состояние списка ингредиентов после вызова метода addIngredient()", List.of(mockIngredientSauce), burger.ingredients);
}

@Test
public void removeIngredientTest() {
SoftAssertions softAssertions = new SoftAssertions();
burger.addIngredient(mockIngredientSauce);
burger.addIngredient(mockIngredientBun);
burger.addIngredient(mockIngredientFilling);
softAssertions.assertThat(burger.ingredients.size())
.as("После добавления трёх ингредиентов размер списка должен быть равен 3")
.isEqualTo(3);

burger.removeIngredient(1);
softAssertions.assertThat(burger.ingredients.size())
.as("После удаления одного ингредиента (индекс 1) размер списка должен уменьшиться до 2")
.isEqualTo(2);
softAssertions.assertAll();
}

@Test
public void moveIngredientTest() {
burger.addIngredient(mockIngredientBun);
burger.addIngredient(mockIngredientFilling);

burger.moveIngredient(0, 1);
assertEquals("Некорректное перемещение ингредиента между позициями", mockIngredientBun, burger.ingredients.get(1));
}


@Test
public void getReceiptTest() {
burger.setBuns(mockBun);
burger.addIngredient(mockIngredientSauce);
burger.addIngredient(mockIngredientBun);

Mockito.when(mockBun.getName()).thenReturn("Space bun");
Mockito.when(mockBun.getPrice()).thenReturn(100.0f);
Mockito.when(mockIngredientSauce.getName()).thenReturn("Ingredient0");
Mockito.when(mockIngredientBun.getName()).thenReturn("Ingredient1");
Mockito.when(mockIngredientSauce.getType()).thenReturn(IngredientType.FILLING);
Mockito.when(mockIngredientBun.getType()).thenReturn(IngredientType.SAUCE);
Mockito.when(mockIngredientSauce.getPrice()).thenReturn(150.0f);
Mockito.when(mockIngredientBun.getPrice()).thenReturn(50.0f);

String expectedReceipt = "(==== Space bun ====)\r\n" +
"= filling Ingredient0 =\r\n" +
"= sauce Ingredient1 =\r\n" +
"(==== Space bun ====)\r\n" +
"\r\n" +
"Price: 400,000000\r\n";

assertEquals("Ожидался чек:\n" + expectedReceipt + "\nБыл получен чек:\n" + burger.getReceipt(), expectedReceipt, burger.getReceipt());
}

}
1 change: 1 addition & 0 deletions target/site/jacoco/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="ru"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="jacoco-resources/report.gif" type="image/gif"/><title>untitled</title><script type="text/javascript" src="jacoco-resources/sort.js"></script></head><body onload="initialSort(['breadcrumb', 'coveragetable'])"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="jacoco-sessions.html" class="el_session">Sessions</a></span><span class="el_report">untitled</span></div><h1>untitled</h1><table class="coverage" cellspacing="0" id="coveragetable"><thead><tr><td class="sortable" id="a" onclick="toggleSort(this)">Element</td><td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td><td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td><td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td><td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td><td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td><td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td><td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td><td class="sortable ctr1" id="l" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="m" onclick="toggleSort(this)">Classes</td></tr></thead><tfoot><tr><td>Total</td><td class="bar">201 of 358</td><td class="ctr2">43 %</td><td class="bar">0 of 4</td><td class="ctr2">100 %</td><td class="ctr1">12</td><td class="ctr2">22</td><td class="ctr1">43</td><td class="ctr2">69</td><td class="ctr1">12</td><td class="ctr2">20</td><td class="ctr1">4</td><td class="ctr2">6</td></tr></tfoot><tbody><tr><td id="a0"><a href="praktikum/index.html" class="el_package">praktikum</a></td><td class="bar" id="b0"><img src="jacoco-resources/redbar.gif" width="67" height="10" title="201" alt="201"/><img src="jacoco-resources/greenbar.gif" width="52" height="10" title="157" alt="157"/></td><td class="ctr2" id="c0">43 %</td><td class="bar" id="d0"><img src="jacoco-resources/greenbar.gif" width="120" height="10" title="4" alt="4"/></td><td class="ctr2" id="e0">100 %</td><td class="ctr1" id="f0">12</td><td class="ctr2" id="g0">22</td><td class="ctr1" id="h0">43</td><td class="ctr2" id="i0">69</td><td class="ctr1" id="j0">12</td><td class="ctr2" id="k0">20</td><td class="ctr1" id="l0">4</td><td class="ctr2" id="m0">6</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.13.202504020838</span></div></body></html>
Binary file added target/site/jacoco/jacoco-resources/branchfc.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/branchnc.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/branchpc.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/bundle.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/class.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/down.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/greenbar.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/group.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/method.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target/site/jacoco/jacoco-resources/package.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions target/site/jacoco/jacoco-resources/prettify.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Pretty printing styles. Used with prettify.js. */

.str { color: #2A00FF; }
.kwd { color: #7F0055; font-weight:bold; }
.com { color: #3F5FBF; }
.typ { color: #606; }
.lit { color: #066; }
.pun { color: #660; }
.pln { color: #000; }
.tag { color: #008; }
.atn { color: #606; }
.atv { color: #080; }
.dec { color: #606; }
Loading