Skip to content

Conversation

@SofiBesch
Copy link

Create test for the burger class - diplom 1

when(mockbun.getPrice()).thenReturn(100.0f);
when(mockIngredient.getType()).thenReturn(ingredientType);
when(mockIngredient.getName()).thenReturn("Test Ingredient");
when(mockIngredient.getPrice()).thenReturn(50.0f);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️Можно улучшить. Цену можно также задавать через параметризацию. Дополнительная проверка не будет лишней

private Bun mockBun;

@Mock
private Ingredient mockIngredient1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. При нейминге не рекомендуется использовать числа (Field2), их еще называют magicNumbers. Очень тяжело поддерживать код с magicNumbers.

public void testAddIngredient() {
Burger burger = new Burger();
burger.addIngredient(mockIngredient1);
assertEquals("Ingredients list should have 1 element", 1, burger.ingredients.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. Для юнит-тестов придерживаемся подхода: один тест, значит одна проверка. Если очень хочется несколько проверок -- тогда используем softAssertions. Поправь, пожалуйста, во всем коде

burger.addIngredient(mockIngredient2);

// burger.moveIngredient(0, 1);
//

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. Неиспользуемый код нужно удалить

float expectedPrice = 100.0f * 2;
float actualPrice = burger.getPrice();

assertEquals("Price should be only buns price when no ingredients", expectedPrice, actualPrice, 0.001f);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. Для юнит-тестов применим подход: один тест, значит одна проверка. В этом тесте две проверки (Mockito.verify, assertEquals), а должна быть одна. Исправь, пожалуйста, этот момент во всем коде.


@Test
public void testGetPriceWithoutIngredients() {
Burger burger = new Burger();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️Можно улучшить. Инициализацию объекта можно вынести в метод с аннотацией before

String receipt = burger.getReceipt();

assertNotNull("Receipt should not be null", receipt);
assertTrue("Receipt should contain bun name", receipt.contains("Test Bun"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛔️Нужно исправить. Строку рецепта нужно проверить целиком в одной проверке, чтобы не пропустить проблем с форматированием текста

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants