Skip to content
Closed
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
52 changes: 52 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,56 @@
<maven.compiler.target>11</maven.compiler.target>
</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>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.11.16</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<!--id выбираешь самостоятельно-->
<id>prepare-agent</id>
<!--в какой фазе maven будет выполняться цель-->
<phase>initialize</phase>
<!--цель jacoco, которую нужно выполнить-->
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

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

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class BunTest {
private final String name;
private final float price;
private Bun bun;


public BunTest(String name, float price) {
this.name = name;
this.price = price;
}


@Parameterized.Parameters(name = "Тестовые данные: name={0}, price={1}")
public static Object[][] getTestData() {
return new Object[][]{
{"Обычная булочка", 100.0f},
{"Специальная булочка", 150.5f},
{"", 0.0f}, // граничный случай: пустое название
{"Булочка с очень длинным названием которое может быть использовано в системе", 999.99f},
{"Булочка", -50.0f}, // граничный случай: отрицательная цена
{null, 75.0f} // граничный случай: null название
};
}

@Before
public void setUp() {
bun = new Bun(name, price);
}

@Test
public void TestGetName() {
assertEquals("Название булочки должно совпадать с переданным в конструктор", name, bun.getName());

}

@Test
public void TestGetPrice() {
assertEquals("Цена булочки должна совпадать с переданной в конструктор", price, bun.getPrice(), 0.001f);
}
}
180 changes: 180 additions & 0 deletions src/test/java/praktikum/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
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.*;

@RunWith(Parameterized.class)
public class BurgerTest {

@Mock
private Bun mockBun;
@Mock
private Ingredient mockIngredient1;
@Mock
private Ingredient mockIngredient2;

private Burger burger;

private final float bunPrice;
private final float ingredientPrice;
private final float expectedTotal;

public BurgerTest(float bunPrice, float ingredientPrice, float expectedTotal) {
this.bunPrice = bunPrice;
this.ingredientPrice = ingredientPrice;
this.expectedTotal = expectedTotal;
}


@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{0.0f, 0.0f, 0.0f},
{100.0f, 50.0f, 250.0f},
{200.0f, 150.0f, 550.0f}
});
}

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

@Test
public void TestSetBuns() {
Mockito.when(mockBun.getPrice()).thenReturn(bunPrice);
Mockito.when(mockBun.getName()).thenReturn("mock_bun");
burger.setBuns(mockBun);
assertEquals(mockBun, burger.bun);
}
@Test
public void TestSetNullBuns() {
burger.setBuns(null);
assertNull(burger.bun);
}

@Test
public void TestAddIngredient() {
Mockito.when(mockIngredient1.getPrice()).thenReturn(ingredientPrice);
burger.addIngredient(mockIngredient1);
assertEquals(1, burger.ingredients.size());
assertEquals(mockIngredient1, burger.ingredients.get(0));
}


@Test
public void TestRemoveIngredient() {
burger.addIngredient(mockIngredient1);
burger.addIngredient(mockIngredient2);

burger.removeIngredient(0);
assertEquals(1, burger.ingredients.size());
assertEquals(mockIngredient2, burger.ingredients.get(0));
}
@Test
public void TestNegativeRemoveNullIngredient() {
burger.addIngredient(mockIngredient1);

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

//Negative tests for remove ingredient
@Test(expected = IndexOutOfBoundsException.class)
public void TestRemoveFromEmptyListThrowsException() {
burger.removeIngredient(0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void TestRemoveWithNegativeIndexThrowsException() {
burger.addIngredient(mockIngredient1);
burger.removeIngredient(-1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void TestRemoveWithIndexOutOfBoundsThrowsException() {
burger.addIngredient(mockIngredient1);
burger.removeIngredient(1);
}


@Test
public void TestMoveIngredient() {
burger.addIngredient(mockIngredient1);
burger.addIngredient(mockIngredient2);

assertEquals(mockIngredient1, burger.ingredients.get(0));
assertEquals(mockIngredient2, burger.ingredients.get(1));
burger.moveIngredient(0,1);
assertEquals(mockIngredient1, burger.ingredients.get(1));
assertEquals(mockIngredient2, burger.ingredients.get(0));
}

//Negative tests for move ingredient
@Test(expected = IndexOutOfBoundsException.class)
public void TestMoveIngredientWithInvalidIndices() {
burger.addIngredient(mockIngredient1);
burger.addIngredient(mockIngredient2);

burger.moveIngredient(-1, 0); // отрицательный индекс
}
@Test(expected = IndexOutOfBoundsException.class)
public void TestMoveIngredientToOutOfBoundsIndex() {
burger.addIngredient(mockIngredient1);
burger.addIngredient(mockIngredient2);

burger.moveIngredient(0, 10); // целевой индекс вне границ
}

@Test
public void TesGetPrice() {
Mockito.when(mockIngredient1.getPrice()).thenReturn(ingredientPrice);
Mockito.when(mockBun.getPrice()).thenReturn(bunPrice);

burger.setBuns(mockBun);
burger.addIngredient(mockIngredient1);

assertEquals(expectedTotal, burger.getPrice(), 0.001f);
}

@Test
public void TestGetReceipt() {
Mockito.when(mockBun.getName()).thenReturn("mock_bun");
Mockito.when(mockBun.getPrice()).thenReturn(100.0f);
Mockito.when(mockIngredient1.getType()).thenReturn(IngredientType.SAUCE);
Mockito.when(mockIngredient1.getName()).thenReturn("mock_SAUCE");
Mockito.when(mockIngredient1.getPrice()).thenReturn(300.0f);

burger.setBuns(mockBun);
burger.addIngredient(mockIngredient1);

String receipt = burger.getReceipt();

assertTrue(receipt.contains("(==== mock_bun ====)"));
assertTrue(receipt.contains("= sauce mock_SAUCE ="));
assertTrue(receipt.contains("Price: 500.0"));
}
@Test
public void TestGetReceiptWithNoIngredients() {
Mockito.when(mockBun.getName()).thenReturn("mock_bun");
Mockito.when(mockBun.getPrice()).thenReturn(100.0f);

burger.setBuns(mockBun);

String receipt = burger.getReceipt();

assertTrue(receipt.contains("(==== mock_bun ====)"));
assertTrue(receipt.contains("Price: 200.0")); // true
assertFalse(receipt.contains("sauce")); // false
}

}
57 changes: 57 additions & 0 deletions src/test/java/praktikum/IngredientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package praktikum;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class IngredientTest {
private final IngredientType type;
private final String name;
private final float price;
private Ingredient ingredient;

public IngredientTest(IngredientType type, String name, float price) {
this.type = type;
this.name = name;
this.price = price;
}

@Parameterized.Parameters(name = "Тестовые данные: type={0}, name={1}, price={2}")
public static Object[][] getTestData() {
return new Object[][]{
{IngredientType.SAUCE, "Соус традиционный галактический", 15.5f},
{IngredientType.SAUCE, "Соус фирменный космический", 20.0f},
{IngredientType.FILLING, "Говяжий метеорит", 30.0f},
{IngredientType.FILLING, "Сыр с астероидной плесенью", 25.5f},
{IngredientType.FILLING, "Хрустящие минеральные кольца", 18.75f},
{IngredientType.SAUCE, "", 0.0f},
{IngredientType.FILLING, "минимальная цена", 0.01f},
{IngredientType.SAUCE, "максимальная цена", Float.MAX_VALUE}
};
}


@Before
public void setUp(){
ingredient = new Ingredient(type, name, price);
}

@Test
public void TestGetPrice() {
assertEquals("Цена ингредиента должна совпадать", price, ingredient.getPrice(), 0.001f);
}

@Test
public void TestGetName() {
assertEquals("Имя ингредиента должна совпадать", name, ingredient.getName());
}

@Test
public void TestGetType() {
assertEquals("Тип ингредиента должна совпадать", type, ingredient.getType());
}
}
21 changes: 21 additions & 0 deletions src/test/java/praktikum/IngredientTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package praktikum;

import org.junit.Test;

import static org.junit.Assert.*;

public class IngredientTypeTest {

@Test
public void TestValues() {
IngredientType[] values = IngredientType.values();
assertNotNull(values);
}

@Test
public void TestValueOf() {
assertEquals(IngredientType.SAUCE, IngredientType.valueOf("SAUCE"));
assertEquals(IngredientType.FILLING, IngredientType.valueOf("FILLING"));

}
}
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="en"><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 349</td><td class="ctr2">52%</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="56" height="10" title="165" alt="165"/><img src="jacoco-resources/greenbar.gif" width="63" height="10" title="184" alt="184"/></td><td class="ctr2" id="c0">52%</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.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