From 8b2fe0cb40da28d7613753bc5caa99e4299f16ac Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sun, 14 Jan 2024 15:36:45 +0100 Subject: [PATCH] Extract item counting in InventoryControllerIntegrationTests --- .../InventoryControllerIntegrationTests.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java b/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java index 5badb9c..24a9564 100644 --- a/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java +++ b/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java @@ -76,6 +76,10 @@ class InventoryControllerIntegrationTests { } } + private long countItems() { + return inventory.findAll().stream().count(); + } + @BeforeEach void populateAnyInventoryItem() { anyInventoryItem = inventory.findAll().stream().findAny().get(); @@ -107,7 +111,7 @@ class InventoryControllerIntegrationTests { .andExpect(status().isOk()) .andExpect(content().string(containsString("Produkt anlegen"))); - long itemCountBefore = inventory.findAll().stream().count(); + long itemCountBefore = countItems(); mvc.perform(post("/inventory/add?type=Consumable") .queryParam("type", Consumable.class.getSimpleName()) @@ -119,9 +123,7 @@ class InventoryControllerIntegrationTests { .param("promotionPrice", "6.66")) .andExpect(redirectedUrl("/inventory")); - long itemCountAfter = inventory.findAll().stream().count(); - - assertThat(itemCountAfter).isEqualTo(itemCountBefore + 1); + assertThat(countItems()).isEqualTo(itemCountBefore + 1); // extracting is not possible here, as the category sets are not equal assertThat(inventory.findAll().stream() @@ -140,14 +142,12 @@ class InventoryControllerIntegrationTests { @Test @WithMockUser(username = "admin", roles = "ADMIN") void adminCanDelete() throws Exception { - long itemCountBefore = inventory.findAll().stream().count(); + long itemCountBefore = countItems(); mvc.perform(get("/inventory/delete/" + anyPid)) .andExpect(redirectedUrl("/inventory")); - long itemCountAfter = inventory.findAll().stream().count(); - - assertThat(itemCountAfter).isEqualTo(itemCountBefore - 1); + assertThat(countItems()).isEqualTo(itemCountBefore - 1); assertThat(inventory.findAll().stream()) .extracting("product.name", "quantity")