Skip to content
Open

Develop #1163

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
8 changes: 8 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Задание 1: юнит-тесты
Представь: нужно протестировать программу, которая помогает заказать бургер в Stellar Burgers. Тебе предстоит покрыть её юнит-тестами.
Здесь пригодятся моки, стабы и параметризация: где именно их использовать, реши самостоятельно.
### Что нужно сделать
Склонируй репозиторий с заготовкой кода.
Подключи библиотеки: Jacoco, Mockito, JUnit 4.
Покрой тестами классы Bun, Burger, Ingredient, IngredientType. Используй моки, стабы и параметризацию там, где нужно.
Процент покрытия должен быть не ниже 70%.
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,53 @@
<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.12.19</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>
27 changes: 27 additions & 0 deletions src/test/java/praktikum/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package praktikum;

import org.junit.Before;
import org.junit.Test;


import static org.junit.Assert.assertEquals;

public class BunTest {

private Bun bun;

@Before
public void setUp() {
bun = new Bun("Кунжут+Орегано", 0.03F);
}

@Test
public void testTestGetName() {
assertEquals("Кунжут+Орегано", bun.getName());
}

@Test
public void testGetPrice() {
assertEquals(0.03F, bun.getPrice(), 0.01);
}
}
98 changes: 98 additions & 0 deletions src/test/java/praktikum/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package praktikum;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static praktikum.IngredientType.FILLING;
import static praktikum.IngredientType.SAUCE;



@RunWith(MockitoJUnitRunner.class)
public class BurgerTest {

@Spy
Bun bun = new Bun("Кунжут+Орегано", 0.03F);

@Spy
Ingredient ingredient1 = new Ingredient(SAUCE, "mazik", 0.25F);

@Spy
Ingredient ingredient2 = new Ingredient(FILLING, "pepper", 0.4F);

@Spy
Burger burger;

@Test
public void setBuns() {
burger.setBuns(bun);
assertEquals(bun, burger.bun);
}

@Test
public void addOneIngredient() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
assertEquals(1, burger.ingredients.size());
assertEquals(ingredient1, burger.ingredients.get(0));

}
//Перестраховочка
@Test
public void addTwoIngredient() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);
assertEquals(2, burger.ingredients.size());
assertEquals(ingredient1, burger.ingredients.get(0));
assertEquals(ingredient2, burger.ingredients.get(1));

}

@Test
public void removeIngredient() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
assertEquals(1, burger.ingredients.size());
burger.removeIngredient(0);
assertEquals(0, burger.ingredients.size());
}

//Проверяем move и убеждаемся что 2 ингредиент не пропал
@Test
public void moveIngredient() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);
assertEquals(ingredient1, burger.ingredients.get(0));
assertEquals(ingredient2, burger.ingredients.get(1));
burger.moveIngredient(0,1);
assertEquals(ingredient2, burger.ingredients.get(0));
assertEquals(ingredient1, burger.ingredients.get(1));
}

@Test
public void getPrice() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);
// Гений маркетинга тот кто придумал булочку *2 считать.
float expected = bun.getPrice() * 2 + ingredient1.getPrice() + ingredient2.getPrice();
assertEquals(expected, burger.getPrice(), 0.01);
}

@Test
public void getReceipt() {
burger.setBuns(bun);
burger.addIngredient(ingredient2);
String expected = String.format("(==== %s ====)%n= %s %s =%n(==== %s ====)%n%nPrice: %f%n",
bun.getName(), ingredient2.getType().toString().toLowerCase(),ingredient2.getName(), bun.getName(), burger.getPrice());
// System.out.println(expected);
// System.out.println(burger.getReceipt());
assertEquals(expected, burger.getReceipt());
}
}
46 changes: 46 additions & 0 deletions src/test/java/praktikum/IngredientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package praktikum;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static praktikum.IngredientType.FILLING;
import static praktikum.IngredientType.SAUCE;


@RunWith(MockitoJUnitRunner.class)
public class IngredientTest {

private IngredientType ingredientType;

@Mock
private Ingredient ingredient1;
@Mock
private Ingredient ingredient2;



@Before
public void setUp() {
ingredient1 = new Ingredient(SAUCE, "mazik", 0.25F);
ingredient2 = new Ingredient(FILLING, "mazik", 0.25F);
}

@Test
public void testGetPrice() {
assertEquals(0.25F, ingredient1.getPrice(), 0.01);
}

@Test
public void testTestGetName() {
assertEquals("mazik", ingredient1.getName());
}

@Test
public void testGetType() {
assertEquals(FILLING, ingredient2.getType());
}
}
13 changes: 13 additions & 0 deletions src/test/java/praktikum/IngredientTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package praktikum;

import junit.framework.TestCase;

import static org.junit.Assert.assertArrayEquals;

public class IngredientTypeTest extends TestCase {

public void testValues() {
IngredientType[] expected = {IngredientType.SAUCE, IngredientType.FILLING};
assertArrayEquals(expected, IngredientType.values());
}
}
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 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