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
57 changes: 56 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,59 @@
<maven.compiler.target>11</maven.compiler.target>
</properties>

</project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.17.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>
</project>
27 changes: 27 additions & 0 deletions src/test/java/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import praktikum.*;

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

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class BunTest {
static Bun bun;

@BeforeAll
static void setUpAll() {
bun = new Bun("Bulochka", 22);
}

@Test
void getNameReturnsName() {
String actual = bun.getName();
assertEquals("Bulochka", actual);
}

@Test
void getPriceReturnPrice() {
float actual = bun.getPrice();
assertEquals(22, actual);
}
}
117 changes: 117 additions & 0 deletions src/test/java/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import praktikum.*;

import static org.junit.jupiter.api.Assertions.*;
import static praktikum.IngredientType.FILLING;
import static praktikum.IngredientType.SAUCE;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;


class BurgerTest {
Burger burger;
Bun bun;
Ingredient sauce;
Ingredient filling;

@BeforeEach
public void setUpAll() {
burger = new Burger();
bun = new Bun("Bulochka", 100.0f);
sauce = new Ingredient(SAUCE, "ketchup", 20.5f);
filling = new Ingredient(FILLING, "chicken", 50.0f);
}

@Test
//проверяем, что в бургере есть булка
void setBunsShouldSetBun() {
burger.setBuns(bun);
assertNotNull(burger.bun);
assertEquals("Bulochka", burger.bun.getName());
assertEquals(100.0f, burger.bun.getPrice());
}
@Test
//проверяем, что через метод addIngredient в список ingredients добавляется sauce и filling
void addIngredientShouldAddToList(){
burger.addIngredient(sauce);
burger.addIngredient(filling);
assertEquals(2, burger.ingredients.size());
assertSame(sauce, burger.ingredients.get(0));
assertSame(filling, burger.ingredients.get(1));
}
@ParameterizedTest
@ValueSource(ints = {0, 1, 2, 3, 4, 5})
void removeIngredientShouldWorkWithAnyIndex(int index) {
// заполняем 6 ингредиентов
for (int i = 0; i < 6; i++) {
burger.addIngredient(new Ingredient(SAUCE, "test" + i, 10f));
}

burger.removeIngredient(index);

assertEquals(5, burger.ingredients.size()); // всегда 5 остается
}
@Test
void removeFirstShouldLeaveFilling() {
burger.addIngredient(sauce);
burger.addIngredient(filling);
burger.removeIngredient(0); // удаляем sauce

assertSame(filling, burger.ingredients.get(0)); // ✓
}

@Test
void removeSecondShouldLeaveSauce() {
burger.addIngredient(sauce);
burger.addIngredient(filling);
burger.removeIngredient(1); // удаляем filling

assertSame(sauce, burger.ingredients.get(0)); // ✓
}

@Test
void moveIngredientShouldChangeOrder() {
Ingredient anotherSauce = new Ingredient(SAUCE, "ketchup", 15.0f);

burger.addIngredient(sauce); // index 0
burger.addIngredient(filling); // index 1
burger.addIngredient(anotherSauce); // index 2

// перенос элемент с index 0 в позицию 2
burger.moveIngredient(0, 2);

assertEquals(3, burger.ingredients.size());
assertSame(filling, burger.ingredients.get(0));
assertSame(anotherSauce, burger.ingredients.get(1));
assertSame(sauce, burger.ingredients.get(2));
}
@Test
void getPriceShouldReturnSumOfBunAndIngredients() {
burger.setBuns(bun); // 100 * 2 = 200
burger.addIngredient(sauce); // + 20.5
burger.addIngredient(filling); // + 50

float expected = 100.0f * 2 + 20.5f + 50.0f;

assertEquals(expected, burger.getPrice());
}
@Test
void getReceiptShouldReturnCorrectFormattedString() {
burger.setBuns(bun);
burger.addIngredient(sauce);
burger.addIngredient(filling);

String receipt = burger.getReceipt();

assertTrue(receipt.contains("(==== Bulochka ====)"));
assertTrue(receipt.contains("= sauce ketchup ="));
assertTrue(receipt.contains("= filling chicken ="));

float expectedPrice = 100.0f * 2 + 20.5f + 50.0f;
String expectedPricePart = String.format("Price: %f", expectedPrice);

assertTrue(receipt.contains(expectedPricePart));
}
}
34 changes: 34 additions & 0 deletions src/test/java/DatabaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import praktikum.*;
import java.util.List;

class DatabaseTest {
private Database db;
@BeforeEach
void setUp() {
db = new Database();
}
@Test
void availableBunsShouldReturnThreeBuns() {
List<Bun> buns = db.availableBuns();
assertEquals(3, buns.size());
// проверяем первый black bun
assertEquals("black bun", buns.get(0).getName());
assertEquals(100f, buns.get(0).getPrice());
}
@Test
void availableIngredientsShouldReturnSixIngredients() {
List<Ingredient> ingredients = db.availableIngredients();
assertEquals(6, ingredients.size());
// 3 соуса + 3 начинки
long sauces = ingredients.stream()
.map(Ingredient::getType)
.filter(type -> type == IngredientType.SAUCE)
.count();
assertEquals(3, sauces);
}

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static praktikum.IngredientType.SAUCE;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class IngredientTest {
static Ingredient ingredient;
@BeforeAll
static void setUpAll() {
ingredient = new Ingredient(SAUCE, "NachinkaSauce", 10);
}
@Test
void getPriceReturnPrice(){
float actual = ingredient.getPrice();
assertEquals(10, actual);
}
@Test
void getNameReturnName(){
String actual = ingredient.getName();
assertEquals("NachinkaSauce", actual);
}
@Test
void getTypeReturnsType(){
IngredientType actual = ingredient.getType();
assertEquals(SAUCE, actual);
}
}