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
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,52 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<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>3.12.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</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>
<excludes>
<exclude>**/*Database.class</exclude>
<exclude>**/*Praktikum.class</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
32 changes: 32 additions & 0 deletions src/Test/java/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.junit.Test;
import praktikum.Bun;

import static org.junit.Assert.*;

public class BunTest {

private static final String BUN_NAME = "black bun";
private static final float BUN_PRICE = 100;

@Test
public void constructorShouldSetFieldsCorrectly() {
Bun bun = new Bun(BUN_NAME, BUN_PRICE);

assertEquals("Name should be set correctly", BUN_NAME, bun.name);
assertEquals("Price should be set correctly", BUN_PRICE, bun.price, 0.01);
}

@Test
public void getNameShouldReturnCorrectName() {
Bun bun = new Bun(BUN_NAME, BUN_PRICE);

assertEquals("getName() should return correct name", BUN_NAME, bun.getName());
}

@Test
public void getPriceShouldReturnCorrectPrice() {
Bun bun = new Bun(BUN_NAME, BUN_PRICE);

assertEquals("getPrice() should return correct price", BUN_PRICE, bun.getPrice(), 0.01);
}
}
107 changes: 107 additions & 0 deletions src/Test/java/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import praktikum.Bun;
import org.junit.After;
import praktikum.Burger;
import praktikum.Ingredient;
import praktikum.IngredientType;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class BurgerTest {

private Burger burger;

@Mock
private Bun bun;

@Mock
private Ingredient ingredient;

// Добавьте это поле
private AutoCloseable closeable;

@Before
public void setUp() {
// Замените initMocks на openMocks
closeable = MockitoAnnotations.openMocks(this);
burger = new Burger();
}

// Добавьте метод tearDown для закрытия ресурсов
@After
public void tearDown() throws Exception {
closeable.close();
}

// Остальные тесты остаются без изменений
@Test
public void constructorShouldCreateEmptyIngredientsList() {
assertNotNull("Ingredients list should not be null", burger.ingredients);
assertTrue("Ingredients list should be empty", burger.ingredients.isEmpty());
}

@Test
public void setBunsShouldSetBunCorrectly() {
burger.setBuns(bun);
assertEquals("Bun should be set correctly", bun, burger.bun);
}

@Test
public void addIngredientShouldAddIngredientToList() {
burger.addIngredient(ingredient);
assertTrue("Ingredient should be added to list", burger.ingredients.contains(ingredient));
assertEquals("Ingredients list size should be 1", 1, burger.ingredients.size());
}

@Test
public void removeIngredientShouldRemoveIngredientFromList() {
burger.addIngredient(ingredient);
burger.removeIngredient(0);
assertTrue("Ingredient should be removed from list", burger.ingredients.isEmpty());
}

@Test
public void moveIngredientShouldMoveIngredientToNewIndex() {
Ingredient ingredient1 = mock(Ingredient.class);
Ingredient ingredient2 = mock(Ingredient.class);

burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);

burger.moveIngredient(0, 1);

assertEquals("First ingredient should be moved to index 1", ingredient1, burger.ingredients.get(1));
assertEquals("Second ingredient should be moved to index 0", ingredient2, burger.ingredients.get(0));
}

@Test
public void getPriceShouldReturnCorrectPrice() {
when(bun.getPrice()).thenReturn(100f);
when(ingredient.getPrice()).thenReturn(50f);

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

float expectedPrice = 100f * 2 + 50f;
assertEquals("Price should be calculated correctly", expectedPrice, burger.getPrice(), 0.01);
}

@Test
public void getReceiptShouldReturnCorrectReceipt() {
when(bun.getName()).thenReturn("black bun");
when(ingredient.getName()).thenReturn("cutlet");
when(ingredient.getType()).thenReturn(IngredientType.FILLING);

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

String receipt = burger.getReceipt();
assertTrue("Receipt should contain bun name", receipt.contains("(==== black bun ====)"));
assertTrue("Receipt should contain ingredient", receipt.contains("= filling cutlet ="));
assertTrue("Receipt should contain price", receipt.contains("Price: "));
}
}
62 changes: 62 additions & 0 deletions src/Test/java/IngredientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import praktikum.Ingredient;
import praktikum.IngredientType;

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

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class IngredientTest {

@Parameters(name = "Test {index}: type={0}, name={1}, price={2}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ IngredientType.SAUCE, "Hot sauce", 100.0f },
{ IngredientType.FILLING, "Cheese", 50.0f },
{ IngredientType.SAUCE, "Sour cream", 75.0f },
{ IngredientType.FILLING, "Lettuce", 25.0f }
});
}

@Parameter()
public IngredientType type;

@Parameter(1)
public String name;

@Parameter(2)
public float price;

@Test
public void testConstructor() {
Ingredient ingredient = new Ingredient(type, name, price);

assertEquals(type, ingredient.type);
assertEquals(name, ingredient.name);
assertEquals(price, ingredient.price, 0.001);
}

@Test
public void testGetPrice() {
Ingredient ingredient = new Ingredient(type, name, price);
assertEquals(price, ingredient.getPrice(), 0.001);
}

@Test
public void testGetName() {
Ingredient ingredient = new Ingredient(type, name, price);
assertEquals(name, ingredient.getName());
}

@Test
public void testGetType() {
Ingredient ingredient = new Ingredient(type, name, price);
assertEquals(type, ingredient.getType());
}
}
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">0 of 193</td><td class="ctr2">100 %</td><td class="bar">0 of 4</td><td class="ctr2">100 %</td><td class="ctr1">0</td><td class="ctr2">17</td><td class="ctr1">0</td><td class="ctr2">40</td><td class="ctr1">0</td><td class="ctr2">15</td><td class="ctr1">0</td><td class="ctr2">4</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/greenbar.gif" width="120" height="10" title="193" alt="193"/></td><td class="ctr2" id="c0">100 %</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">0</td><td class="ctr2" id="g0">17</td><td class="ctr1" id="h0">0</td><td class="ctr2" id="i0">40</td><td class="ctr1" id="j0">0</td><td class="ctr2" id="k0">15</td><td class="ctr1" id="l0">0</td><td class="ctr2" id="m0">4</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</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