Skip to content
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

Develop1 #869

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
.idea
.build
.target
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Задание 1: юнит-тесты

Представь: нужно протестировать программу, которая помогает заказать бургер в Stellar Burgers. Тебе предстоит покрыть её юнит-тестами.
Здесь пригодятся моки, стабы и параметризация: где именно их использовать, реши самостоятельно.
Что нужно сделать
Склонируй репозиторий с заготовкой кода.
Подключи библиотеки: Jacoco, Mockito, JUnit 4.
Покрой тестами классы Bun, Burger, Ingredient, IngredientType. Используй моки, стабы и параметризацию там, где нужно.
Процент покрытия должен быть не ниже 70%.
57 changes: 54 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,64 @@
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.praktikum.yandex</groupId>
<artifactId>Diplom_1</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</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>

</project>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
59 changes: 59 additions & 0 deletions src/test/java/BunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import praktikum.Bun;
import praktikum.Database;

import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class BunTest {

private final String expectedName;
private final float expectedPrice;

private Bun bun;

private static Database database;

public BunTest(String expectedName, float expectedPrice) {
this.expectedName = expectedName;
this.expectedPrice = expectedPrice;
}

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"black bun", 100.0F},
{"white bun", 200.0F},
{"red bun", 300.0F}
});
}

@Before
public void setUp() {
database = new Database();
}

@Test
public void testBunInitialization() {
bun = new Bun(expectedName, expectedPrice);
}

@Test
public void testBunGetName() {
bun = new Bun(expectedName, expectedPrice);
Assert.assertEquals(expectedName, bun.getName());
}

@Test
public void testBunGetPrice() {
bun = new Bun(expectedName, expectedPrice);
Assert.assertEquals(expectedPrice, bun.getPrice(), 0.001);
}

}

108 changes: 108 additions & 0 deletions src/test/java/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import praktikum.Bun;
import praktikum.Burger;
import praktikum.Ingredient;
import praktikum.IngredientType;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class BurgerTest {
private Burger burger;
@Mock
private Bun bun;
@Mock
private Ingredient ingredient1, ingredient2, ingredient3;

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

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

@Test
public void testAddIngredient() {
burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);
burger.addIngredient(ingredient3);

Assert.assertEquals(3, burger.ingredients.size());
}

@Test
public void testRemoveIngredient() {
testAddIngredient();
burger.removeIngredient(2);
Assert.assertEquals(2, burger.ingredients.size());
}

@Test
public void testMoveIngredient() {
testAddIngredient();
burger.moveIngredient(2, 1);
Assert.assertEquals(ingredient3, burger.ingredients.get(1));
Assert.assertEquals(ingredient2, burger.ingredients.get(2));
}

@Test
public void testGetPrice() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);
burger.addIngredient(ingredient3);

when(bun.getPrice()).thenReturn(60F);
when(ingredient1.getPrice()).thenReturn(15F);
when(ingredient2.getPrice()).thenReturn(20F);
when(ingredient3.getPrice()).thenReturn(25F);

float expected = (60F * 2) + 15F + 20F + 25F;
System.out.println(expected);
System.out.println(burger.getPrice());
Assert.assertEquals(expected, burger.getPrice(), 0);
}

@Test
public void testGetReceipt() {
burger.setBuns(bun);
burger.addIngredient(ingredient1);
burger.addIngredient(ingredient2);
burger.addIngredient(ingredient3);

when(bun.getName()).thenReturn("Double Cheese");
when(ingredient1.getName()).thenReturn("Lettuce");
when(ingredient2.getName()).thenReturn("Mayo");
when(ingredient3.getName()).thenReturn("Tomato");

when(bun.getPrice()).thenReturn(60F);
when(ingredient1.getPrice()).thenReturn(15F);
when(ingredient2.getPrice()).thenReturn(20F);
when(ingredient3.getPrice()).thenReturn(25F);

when(ingredient1.getType()).thenReturn(IngredientType.FILLING);
when(ingredient2.getType()).thenReturn(IngredientType.SAUCE);
when(ingredient3.getType()).thenReturn(IngredientType.FILLING);

String expected = String.format("(==== Double Cheese ====)%n" +
"= filling Lettuce =%n" +
"= sauce Mayo =%n" +
"= filling Tomato =%n" +
"(==== Double Cheese ====)%n" +
"%nPrice: 180,000000%n");

System.out.println(expected);
System.out.println(burger.getReceipt());
Assert.assertEquals(expected, burger.getReceipt());
}

}
49 changes: 49 additions & 0 deletions src/test/java/IngredientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import praktikum.IngredientType;
import praktikum.Ingredient;

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

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

@Parameterized.Parameters
public static Object[][] getParameters() {
return new Object[][]{
{IngredientType.SAUCE, "Tomato Sauce", 2.5f},
{IngredientType.FILLING, "Beef Patty", 3.0f}
};
}

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

@Test
public void testGetPrice() {
Assert.assertEquals(price, ingredient.getPrice(), 0.0001);
}

@Test
public void testGetName() {
Assert.assertEquals(name, ingredient.getName());
}

@Test
public void testGetType() {
Assert.assertEquals(ingredientType, ingredient.getType());
}
}
42 changes: 42 additions & 0 deletions src/test/java/IngredientTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import praktikum.IngredientType;

import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class IngredientTypeTest {

private IngredientType ingredientType;
private String expectedString;

public IngredientTypeTest(IngredientType ingredientType, String expectedString) {
this.ingredientType = ingredientType;
this.expectedString = expectedString;
}

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ IngredientType.SAUCE, "SAUCE" },
{ IngredientType.FILLING, "FILLING" }
});
}

@Test
public void testIngredientTypeToString() {
assertEquals(expectedString, ingredientType.name());
}

@Test
public void testIngredientTypeValues() {
IngredientType[] types = IngredientType.values();
assertEquals(2, types.length);
assertEquals(IngredientType.SAUCE, types[0]);
assertEquals(IngredientType.FILLING, types[1]);
}
}
Binary file added target/classes/praktikum/Bun.class
Binary file not shown.
Binary file added target/classes/praktikum/Burger.class
Binary file not shown.
Binary file added target/classes/praktikum/Database.class
Binary file not shown.
Binary file added target/classes/praktikum/Ingredient.class
Binary file not shown.
Binary file added target/classes/praktikum/IngredientType.class
Binary file not shown.
Binary file added target/classes/praktikum/Praktikum.class
Binary file not shown.
Binary file added target/jacoco.exec
Binary file not shown.
3 changes: 3 additions & 0 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
artifactId=praktikum
groupId=org.example
version=1.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
praktikum/Bun.class
praktikum/IngredientType.class
praktikum/Database.class
praktikum/Praktikum.class
praktikum/Burger.class
praktikum/Ingredient.class
Loading