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
67 changes: 66 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,73 @@
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<junit.version>5.10.1</junit.version>
<mockito.version>5.8.0</mockito.version>
<jacoco.version>0.8.11</jacoco.version>
</properties>

</project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>

<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
38 changes: 38 additions & 0 deletions src/test/java/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package praktikum;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

class BunTest {

private Bun bun;

@BeforeEach
void setUp() {
bun = new Bun("black bun", 100);
}

@Test
@DisplayName("Метод getName возвращает имя булочки")
void getNameShouldReturnBunName() {
assertEquals("black bun", bun.getName());
}

@Test
@DisplayName("Метод getPrice возвращает цену булочки")
void getPriceShouldReturnBunPrice() {
assertEquals(100, (int) bun.getPrice());
}

@Test
@DisplayName("Конструктор корректно сохраняет параметры")
void constructorShouldSaveFields() {
Bun testBun = new Bun("white bun", 200);

assertEquals("white bun", testBun.getName());
assertEquals(200, (int) testBun.getPrice());
}
}
190 changes: 190 additions & 0 deletions src/test/java/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package praktikum;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

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

@ExtendWith(MockitoExtension.class)
class BurgerTest {

private Burger burger;

@Mock
private Bun bun;

@Mock
private Ingredient ingredient;

@Mock
private Ingredient secondIngredient;

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

@Test
@DisplayName("setBuns устанавливает булочку")
void setBunsShouldSetBun() {
burger.setBuns(bun);

assertEquals(bun, burger.bun);
}

@Test
@DisplayName("addIngredient добавляет ингредиент")
void addIngredientShouldAddIngredientToList() {
burger.addIngredient(ingredient);

assertEquals(1, burger.ingredients.size());
}

@Test
@DisplayName("removeIngredient удаляет ингредиент по индексу")
void removeIngredientShouldRemoveIngredient() {
burger.addIngredient(ingredient);

burger.removeIngredient(0);

assertTrue(burger.ingredients.isEmpty());
}

@Test
@DisplayName("moveIngredient меняет местами ингредиенты")
void moveIngredientShouldSwapIngredients() {
burger.addIngredient(ingredient);
burger.addIngredient(secondIngredient);

burger.moveIngredient(0, 1);

assertEquals(secondIngredient, burger.ingredients.get(0));
assertEquals(ingredient, burger.ingredients.get(1));
}

@Test
@DisplayName("ингредиенты сохраняют порядок добавления")
void ingredientsShouldKeepOrder() {
burger.addIngredient(ingredient);
burger.addIngredient(secondIngredient);

assertEquals(ingredient, burger.ingredients.get(0));
assertEquals(secondIngredient, burger.ingredients.get(1));
}

@Test
@DisplayName("getPrice возвращает цену бургера с одним ингредиентом")
void getPriceShouldCalculateCorrectPrice() {
int bunPrice = 100;
int ingredientPrice = 50;

when(bun.getPrice()).thenReturn((float) bunPrice);
when(ingredient.getPrice()).thenReturn((float) ingredientPrice);

burger.setBuns(bun);
burger.addIngredient(ingredient);

int expectedPrice = bunPrice * 2 + ingredientPrice;

assertEquals(expectedPrice, (int) burger.getPrice());
}

@Test
@DisplayName("getPrice считает цену всех ингредиентов")
void getPriceShouldSumAllIngredients() {
int bunPrice = 100;
int firstIngredientPrice = 30;
int secondIngredientPrice = 40;

when(bun.getPrice()).thenReturn((float) bunPrice);
when(ingredient.getPrice()).thenReturn((float) firstIngredientPrice);
when(secondIngredient.getPrice()).thenReturn((float) secondIngredientPrice);

burger.setBuns(bun);
burger.addIngredient(ingredient);
burger.addIngredient(secondIngredient);

int expectedPrice = bunPrice * 2 + firstIngredientPrice + secondIngredientPrice;

assertEquals(expectedPrice, (int) burger.getPrice());
}

@Test
@DisplayName("getReceipt содержит название булки и ингредиента")
void getReceiptShouldContainNames() {
when(bun.getName()).thenReturn("black bun");
when(bun.getPrice()).thenReturn((float) 100);

when(ingredient.getName()).thenReturn("cutlet");
when(ingredient.getType()).thenReturn(IngredientType.FILLING);
when(ingredient.getPrice()).thenReturn((float) 50);

burger.setBuns(bun);
burger.addIngredient(ingredient);

String receipt = burger.getReceipt();

assertTrue(receipt.contains("black bun"));
assertTrue(receipt.contains("cutlet"));
}

@Test
@DisplayName("getReceipt содержит все ингредиенты")
void getReceiptShouldContainAllIngredients() {
when(bun.getName()).thenReturn("black bun");
when(bun.getPrice()).thenReturn((float) 100);

when(ingredient.getName()).thenReturn("hot sauce");
when(ingredient.getType()).thenReturn(IngredientType.SAUCE);
when(ingredient.getPrice()).thenReturn((float) 20);

when(secondIngredient.getName()).thenReturn("cutlet");
when(secondIngredient.getType()).thenReturn(IngredientType.FILLING);
when(secondIngredient.getPrice()).thenReturn((float) 50);

burger.setBuns(bun);
burger.addIngredient(ingredient);
burger.addIngredient(secondIngredient);

String receipt = burger.getReceipt();

assertTrue(receipt.contains("= sauce hot sauce ="));
assertTrue(receipt.contains("= filling cutlet ="));
}

@Test
@DisplayName("getReceipt содержит строку с ценой")
void getReceiptShouldContainPriceLine() {
when(bun.getName()).thenReturn("black bun");
when(bun.getPrice()).thenReturn((float) 100);

when(ingredient.getName()).thenReturn("cutlet");
when(ingredient.getType()).thenReturn(IngredientType.FILLING);
when(ingredient.getPrice()).thenReturn((float) 50);

burger.setBuns(bun);
burger.addIngredient(ingredient);

String receipt = burger.getReceipt();

assertTrue(receipt.contains("Price:"));
}

@Test
@DisplayName("getReceipt использует название булочки два раза")
void getReceiptShouldUseBunNameTwice() {
when(bun.getName()).thenReturn("black bun");
when(bun.getPrice()).thenReturn((float) 100);

burger.setBuns(bun);

burger.getReceipt();

verify(bun, times(2)).getName();
}
}
24 changes: 24 additions & 0 deletions src/test/java/IngredientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package praktikum;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

class IngredientTest {

@ParameterizedTest
@CsvSource({
"SAUCE, hot sauce, 100",
"FILLING, cutlet, 200",
"SAUCE, cheese sauce, 50"
})
void ingredientShouldStoreCorrectData(IngredientType type, String name, float price) {

Ingredient ingredient = new Ingredient(type, name, price);

assertEquals(type, ingredient.getType());
assertEquals(name, ingredient.getName());
assertEquals((int) price, (int) ingredient.getPrice());
}
}
23 changes: 23 additions & 0 deletions src/test/java/IngredientTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package praktikum;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class IngredientTypeTest {

@Test
void enumShouldContainTwoValues() {
assertEquals(2, IngredientType.values().length);
}

@Test
void valueOfShouldReturnSauce() {
assertEquals(IngredientType.SAUCE, IngredientType.valueOf("SAUCE"));
}

@Test
void valueOfShouldReturnFilling() {
assertEquals(IngredientType.FILLING, IngredientType.valueOf("FILLING"));
}
}
Binary file added target/classes/praktikum/Bun.class
Binary file not shown.
Binary file added target/classes/praktikum/Burger.class
Binary file not shown.
Binary file added target/classes/praktikum/Database.class
Binary file not shown.
Binary file added target/classes/praktikum/Ingredient.class
Binary file not shown.
Binary file added target/classes/praktikum/IngredientType.class
Binary file not shown.
Binary file added target/classes/praktikum/Praktikum.class
Binary file not shown.
Binary file added target/jacoco.exec
Binary file not shown.
3 changes: 3 additions & 0 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
artifactId=praktikum
groupId=org.example
version=1.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
praktikum\Bun.class
praktikum\Praktikum.class
praktikum\IngredientType.class
praktikum\Burger.class
praktikum\Database.class
praktikum\Ingredient.class
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
C:\cygwin64\home\vikaa\Project\Task_1\src\main\java\praktikum\Bun.java
C:\cygwin64\home\vikaa\Project\Task_1\src\main\java\praktikum\Burger.java
C:\cygwin64\home\vikaa\Project\Task_1\src\main\java\praktikum\Database.java
C:\cygwin64\home\vikaa\Project\Task_1\src\main\java\praktikum\Ingredient.java
C:\cygwin64\home\vikaa\Project\Task_1\src\main\java\praktikum\IngredientType.java
C:\cygwin64\home\vikaa\Project\Task_1\src\main\java\praktikum\Praktikum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
praktikum\IngredientTypeTest.class
praktikum\IngredientTest.class
praktikum\BunTest.class
praktikum\BurgerTest.class
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
C:\cygwin64\home\vikaa\Project\Task_1\src\test\java\BunTest.java
C:\cygwin64\home\vikaa\Project\Task_1\src\test\java\BurgerTest.java
C:\cygwin64\home\vikaa\Project\Task_1\src\test\java\IngredientTest.java
C:\cygwin64\home\vikaa\Project\Task_1\src\test\java\IngredientTypeTest.java
Binary file added target/praktikum-1.0-SNAPSHOT.jar
Binary file not shown.
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>praktikum</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">praktikum</span></div><h1>praktikum</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">165 of 358</td><td class="ctr2">53 %</td><td class="bar">0 of 4</td><td class="ctr2">100 %</td><td class="ctr1">5</td><td class="ctr2">22</td><td class="ctr1">29</td><td class="ctr2">69</td><td class="ctr1">5</td><td class="ctr2">20</td><td class="ctr1">2</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="55" height="10" title="165" alt="165"/><img src="jacoco-resources/greenbar.gif" width="64" height="10" title="193" alt="193"/></td><td class="ctr2" id="c0">53 %</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">5</td><td class="ctr2" id="g0">22</td><td class="ctr1" id="h0">29</td><td class="ctr2" id="i0">69</td><td class="ctr1" id="j0">5</td><td class="ctr2" id="k0">20</td><td class="ctr1" id="l0">2</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.11.202310140853</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.
Loading