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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Задание 1: юнит-тесты
75 changes: 71 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,81 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>praktikum</artifactId>
<groupId>ru.yandex.praktikum</groupId>
<artifactId>qa-java-diplom-1</artifactId>
<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>
<minimal.maven.version>3.3.9</minimal.maven.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>3.0.0-M3</maven-surefire-plugin.version>
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
<junit-4.version>4.13.2</junit-4.version>
<mockito.version>3.12.4</mockito.version>
<net.bytebuddy.version>1.11.16</net.bytebuddy.version>
</properties>

</project>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-4.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>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${net.bytebuddy.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</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>
</project>
30 changes: 30 additions & 0 deletions src/test/java/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import org.junit.Test;
import org.junit.Before;
import praktikum.Bun;

import static org.junit.Assert.assertEquals;

public class BunTest {
private Bun bun;

@Before
public void createNewInstance() {
bun = new Bun("original", 200.0f);
}

@Test
public void getName() {
String expected = "original";
String actual = bun.getName();

assertEquals("Неверное значение названия булочки", expected, actual);
}

@Test
public void getPrice() {
float expected = 200.0f;
float actual = bun.getPrice();

assertEquals("Неверные значения цены булочки", expected, actual, 0);
}
}
51 changes: 51 additions & 0 deletions src/test/java/BurgerParametrizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import praktikum.Bun;
import praktikum.Burger;
import praktikum.Ingredient;
import praktikum.IngredientType;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class BurgerParametrizedTest {
private Burger burger;
private Ingredient sauce;
private Ingredient filling;
private final String name;
private final float price;

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

@Before
public void createNewInstance() {
sauce = new Ingredient(IngredientType.SAUCE, "chili", 20.0f);
filling = new Ingredient(IngredientType.FILLING, "cheese", 15.5f);
burger = new Burger();
}

@Parameterized.Parameters(name = "{index} : price = {1}")
public static Object[][] getBunData() {
return new Object[][] {
{"original-free", 0.0f},
{"original", 200.0f}
};
}

@Test
public void getPrice() {
Bun bun = new Bun(name, price);
burger.setBuns(bun);
burger.addIngredient(sauce);
burger.addIngredient(filling);
float expected = bun.price * 2 + sauce.price + filling.price;
float actual = burger.getPrice();

assertEquals("Неверные значения цены бургера", expected, actual, 0);
}
}
96 changes: 96 additions & 0 deletions src/test/java/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

import java.util.List;
import org.junit.Test;
import org.junit.Before;
import org.mockito.Mock;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import praktikum.Bun;
import praktikum.Burger;
import praktikum.Ingredient;
import praktikum.IngredientType;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class BurgerTest {
@Mock
private Bun bun;
@Mock
private Ingredient ingredient;
@Mock
private Ingredient sauce;
@Mock
private Ingredient filling;
private Burger burger;

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

@Test
public void setBuns() {
burger.setBuns(bun);
Bun actual = burger.bun;

assertEquals("Неправильное использование зависимости от булочки в бургере", bun, actual);
}

@Test
public void addIngredient() {
burger.addIngredient(ingredient);
List<Ingredient> expected = List.of(ingredient);
List<Ingredient> actual = burger.ingredients;

assertEquals("Неправильное добавление ингредиента в список", expected, actual);
}

@Test
public void removeIngredient() {
burger.addIngredient(ingredient);
burger.removeIngredient(0);
List<Ingredient> actual = burger.ingredients;

assertEquals("Неправильное удаление ингредиента из списка", List.of(), actual);
}

@Test
public void moveIngredient() {
burger.addIngredient(filling);
burger.addIngredient(sauce);
burger.moveIngredient(0, 1);
Ingredient actual = burger.ingredients.get(1);

assertEquals("Неправильное перемещение ингредиента в списке", filling, actual);
}

@Test
public void getReceipto() {
Mockito.when(bun.getName()).thenReturn("original");
Mockito.when(bun.getPrice()).thenReturn(200.0f);
burger.setBuns(bun);

Mockito.when(ingredient.getType()).thenReturn(IngredientType.SAUCE);
Mockito.when(ingredient.getName()).thenReturn("chili");
Mockito.when(ingredient.getPrice()).thenReturn(20.0f);
burger.addIngredient(ingredient);

StringBuilder receipt = new StringBuilder(String.format("(==== %s ====)%n", bun.getName()));
List<Ingredient> ingredients = burger.ingredients;

for (Ingredient ingredient : ingredients) {
receipt.append(String.format("= %s %s =%n", ingredient.getType().name().toLowerCase(),
ingredient.getName()));
}

receipt.append(String.format("(==== %s ====)%n", bun.getName()));
receipt.append(String.format("%nPrice: %f%n", burger.getPrice()));

String expected = receipt.toString();
String actual = burger.getReceipt();

assertEquals("Некорректный рецепт бургера", expected, actual);
}
}
37 changes: 37 additions & 0 deletions src/test/java/IngredientParametrizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import praktikum.Ingredient;
import praktikum.IngredientType;

import static org.junit.Assert.assertEquals;

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

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

@Parameterized.Parameters(name = "{index} : type = {0}")
public static Object[][] getIngredientData() {
return new Object[][] {
{IngredientType.SAUCE, "chili", 20.0f},
{IngredientType.FILLING, "cheese", 15.5f},
{null, null, 0.0f}
};
}

@Test
public void getType() {
Ingredient ingredient = new Ingredient(type, name, price);
IngredientType actual = ingredient.getType();

assertEquals("Неверные значения типа ингредиента", type, actual);
}
}
31 changes: 31 additions & 0 deletions src/test/java/IngredientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.Test;
import org.junit.Before;
import praktikum.Ingredient;
import praktikum.IngredientType;

import static org.junit.Assert.assertEquals;

public class IngredientTest {
private Ingredient ingredient;

@Before
public void createNewInstance() {
ingredient = new Ingredient(IngredientType.SAUCE, "chili", 20.0f);
}

@Test
public void getName() {
String expected = "chili";
String actual = ingredient.getName();

assertEquals("Неверные значения названия ингредиента", expected, actual);
}

@Test
public void getPrice() {
float expected = 20.0f;
float actual = ingredient.getPrice();

assertEquals("Неверные значения цены ингредиента", expected, actual, 0);
}
}
30 changes: 30 additions & 0 deletions src/test/java/IngredientTypeParametrizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import praktikum.IngredientType;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class IngredientTypeParametrizedTest {
private final IngredientType type;
private final String name;

public IngredientTypeParametrizedTest(IngredientType type, String name) {
this.type = type;
this.name = name;
}

@Parameterized.Parameters(name = "{index} : name = {0}")
public static Object[][] getIngredientTypeData() {
return new Object[][] {
{IngredientType.SAUCE, "SAUCE"},
{IngredientType.FILLING, "FILLING"}
};
}

@Test
public void checkIngredientTypeName() {
assertEquals("Incorrect name ingredient type", name, type.name());
}
}
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>qa-java-diplom-1</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">qa-java-diplom-1</span></div><h1>qa-java-diplom-1</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.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