-
Notifications
You must be signed in to change notification settings - Fork 2k
Подключила библиотеки: JaCoCo, Mockito, JUnit 4.Покрыла тестами класс… #1159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8e5ddc2
a9cb796
f383593
a84fcf2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package praktikum; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.containsString; | ||
| import static org.junit.Assert.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| public class BurgerParamTest { | ||
|
|
||
| @Parameterized.Parameters(name = "bun={0}, ingredients={1}, expectedPrice={2}") | ||
| public static Iterable<Object[]> data() { | ||
| return BurgerTestData.data(); | ||
| } | ||
|
|
||
| private final float bunPrice; | ||
| private final List<Float> ingredientPrices; | ||
| private final float expectedPrice; | ||
|
|
||
| public BurgerParamTest(float bunPrice, List<Float> ingredientPrices, float expectedPrice) { | ||
| this.bunPrice = bunPrice; | ||
| this.ingredientPrices = ingredientPrices; | ||
| this.expectedPrice = expectedPrice; | ||
| } | ||
|
|
||
| private Burger burger; | ||
| private Bun bunMock; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| burger = new Burger(); | ||
|
|
||
| bunMock = mock(Bun.class); | ||
| when(bunMock.getName()).thenReturn("Test Bun"); | ||
| when(bunMock.getPrice()).thenReturn(bunPrice); | ||
|
|
||
| burger.setBuns(bunMock); | ||
|
|
||
| for (int i = 0; i < ingredientPrices.size(); i++) { | ||
| Ingredient ing = mock(Ingredient.class, "ing-" + i); | ||
| IngredientType type = (i % 2 == 0) ? IngredientType.SAUCE : IngredientType.FILLING; | ||
| when(ing.getType()).thenReturn(type); | ||
| when(ing.getName()).thenReturn((type == IngredientType.SAUCE ? "bbq" : "cutlet") + "-" + i); | ||
| when(ing.getPrice()).thenReturn(ingredientPrices.get(i)); | ||
| burger.addIngredient(ing); | ||
| } | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| burger = null; | ||
| bunMock = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void getPrice() { | ||
| float price = burger.getPrice(); | ||
| assertEquals(expectedPrice, price, 0.0001f); | ||
| } | ||
|
|
||
| @Test | ||
| public void getReceiptInFooter() { | ||
| String receipt = burger.getReceipt(); | ||
|
|
||
| String footer = String.format("(==== %s ====)%n", "Test Bun"); | ||
| assertThat("В чеке должен быть нижний разделитель", receipt, containsString(footer)); | ||
| } | ||
|
|
||
| @Test | ||
| public void getReceiptInHeader() { | ||
| String receipt = burger.getReceipt(); | ||
|
|
||
| String header = String.format("(==== %s ====)%n", "Test Bun"); | ||
| assertTrue("В чеке должен быть заголовок с названием булочки", receipt.startsWith(header)); | ||
| } | ||
|
|
||
| @Test | ||
| public void getReceiptContainAllIngredientLines() { | ||
| String receipt = burger.getReceipt(); | ||
|
|
||
| for (int i = 0; i < burger.ingredients.size(); i++) { | ||
| Ingredient ing = burger.ingredients.get(i); | ||
| String expectedLine = String.format("= %s %s =%n", | ||
| ing.getType().toString().toLowerCase(), ing.getName()); | ||
| assertThat("Должна присутствовать строка ингредиента " + i, receipt, containsString(expectedLine)); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package praktikum; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.junit.Assert.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class BurgerTest { | ||
|
|
||
| private Burger burger; | ||
| private Bun bunMock; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| burger = new Burger(); | ||
| bunMock = mock(Bun.class); | ||
| when(bunMock.getName()).thenReturn("Test Bun"); | ||
| when(bunMock.getPrice()).thenReturn(100f); | ||
| burger.setBuns(bunMock); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| burger = null; | ||
| bunMock = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void setBuns() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛔️Нужно исправить. Если в тестах не нужна параметризация, их нужно вынести в отдельный класс |
||
| assertSame("Поле bun должно ссылаться на установленную булочку", bunMock, burger.bun); | ||
| } | ||
|
|
||
| @Test | ||
| public void addIngredient() { | ||
| int before = burger.ingredients.size(); | ||
|
|
||
| Ingredient extra = mock(Ingredient.class); | ||
| when(extra.getType()).thenReturn(IngredientType.SAUCE); | ||
| when(extra.getName()).thenReturn("extra-sauce"); | ||
| when(extra.getPrice()).thenReturn(10f); | ||
|
|
||
| burger.addIngredient(extra); | ||
|
|
||
| assertEquals(before + 1, burger.ingredients.size()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛔️Нужно исправить. Для юнит-тестов придерживаемся подхода: один тест, значит одна проверка. Если очень хочется несколько проверок -- тогда используем softAssertions. Поправь, пожалуйста, во всем коде |
||
| } | ||
|
|
||
| @Test | ||
| public void removeIngredient() { | ||
| Ingredient toRemove = mock(Ingredient.class); | ||
| when(toRemove.getType()).thenReturn(IngredientType.FILLING); | ||
| when(toRemove.getName()).thenReturn("delete-me"); | ||
| when(toRemove.getPrice()).thenReturn(123f); | ||
| burger.addIngredient(toRemove); | ||
|
|
||
| int initial = burger.ingredients.size(); | ||
| int index = burger.ingredients.indexOf(toRemove); | ||
|
|
||
| burger.removeIngredient(index); | ||
|
|
||
| assertEquals("Размер должен уменьшиться на 1", initial - 1, burger.ingredients.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void removeIngredientFromList() { | ||
| Ingredient toRemove = mock(Ingredient.class); | ||
| when(toRemove.getType()).thenReturn(IngredientType.FILLING); | ||
| when(toRemove.getName()).thenReturn("delete-me"); | ||
| when(toRemove.getPrice()).thenReturn(123f); | ||
| burger.addIngredient(toRemove); | ||
|
|
||
| int index = burger.ingredients.indexOf(toRemove); | ||
|
|
||
| burger.removeIngredient(index); | ||
|
|
||
| assertFalse("Элемент должен быть удалён", burger.ingredients.contains(toRemove)); | ||
| } | ||
|
|
||
| @Test | ||
| public void moveIngredient() { | ||
| Ingredient i0 = mock(Ingredient.class, "i0"); | ||
| when(i0.getType()).thenReturn(IngredientType.SAUCE); | ||
| when(i0.getName()).thenReturn("i0"); | ||
| when(i0.getPrice()).thenReturn(1f); | ||
|
|
||
| Ingredient i1 = mock(Ingredient.class, "i1"); | ||
| when(i1.getType()).thenReturn(IngredientType.FILLING); | ||
| when(i1.getName()).thenReturn("i1"); | ||
| when(i1.getPrice()).thenReturn(2f); | ||
|
|
||
| Ingredient i2 = mock(Ingredient.class, "i2"); | ||
| when(i2.getType()).thenReturn(IngredientType.SAUCE); | ||
| when(i2.getName()).thenReturn("i2"); | ||
| when(i2.getPrice()).thenReturn(3f); | ||
|
|
||
| burger.ingredients.clear(); | ||
| burger.addIngredient(i0); | ||
| burger.addIngredient(i1); | ||
| burger.addIngredient(i2); | ||
|
|
||
| burger.moveIngredient(0, 2); | ||
| assertThat(burger.ingredients.get(2), is(i0)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package praktikum; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
|
|
||
| public class BurgerTestData { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static Iterable<Object[]> data() { | ||
| return Arrays.asList(new Object[][]{ | ||
| {100f, Collections.<Float>emptyList(), 200f}, | ||
| {100f, Arrays.asList(50f), 250f}, | ||
| {100f, Arrays.asList(50f, 25f), 275f}, | ||
| {199.99f, Arrays.asList(0.01f, 1000f), 199.99f * 2 + 0.01f + 1000f}, | ||
| }); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛔️Нужно исправить. В папке target нужно оставить только отчет |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| praktikum\Bun.class | ||
| praktikum\Praktikum.class | ||
| praktikum\IngredientType.class | ||
| praktikum\Burger.class | ||
| praktikum\Database.class | ||
| praktikum\Ingredient.class |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| H:\IdeaProjects\QA-java-diplom\src\main\java\praktikum\IngredientType.java | ||
| H:\IdeaProjects\QA-java-diplom\src\main\java\praktikum\Burger.java | ||
| H:\IdeaProjects\QA-java-diplom\src\main\java\praktikum\Database.java | ||
| H:\IdeaProjects\QA-java-diplom\src\main\java\praktikum\Praktikum.java | ||
| H:\IdeaProjects\QA-java-diplom\src\main\java\praktikum\Ingredient.java | ||
| H:\IdeaProjects\QA-java-diplom\src\main\java\praktikum\Bun.java |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| praktikum\BurgerParamTest.class | ||
| praktikum\BurgerTestData.class | ||
| praktikum\BurgerTest.class |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| H:\IdeaProjects\QA-java-diplom\src\test\java\praktikum\BurgerTestData.java | ||
| H:\IdeaProjects\QA-java-diplom\src\test\java\praktikum\BurgerTest.java | ||
| H:\IdeaProjects\QA-java-diplom\src\test\java\praktikum\BurgerParamTest.java |
| 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">201 of 358</td><td class="ctr2">43 %</td><td class="bar">0 of 4</td><td class="ctr2">100 %</td><td class="ctr1">12</td><td class="ctr2">22</td><td class="ctr1">43</td><td class="ctr2">69</td><td class="ctr1">12</td><td class="ctr2">20</td><td class="ctr1">4</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="67" height="10" title="201" alt="201"/><img src="jacoco-resources/greenbar.gif" width="52" height="10" title="157" alt="157"/></td><td class="ctr2" id="c0">43 %</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">12</td><td class="ctr2" id="g0">22</td><td class="ctr1" id="h0">43</td><td class="ctr2" id="i0">69</td><td class="ctr1" id="j0">12</td><td class="ctr2" id="k0">20</td><td class="ctr1" id="l0">4</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> |
| 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; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⛔️Нужно исправить. Должно быть два класса