From 67afb532a118328a1aa79a1270c0c21030c6d457 Mon Sep 17 00:00:00 2001 From: Lukas Karsch <lk224@hdm-stuttgart.de> Date: Sun, 23 Apr 2023 21:37:51 +0200 Subject: [PATCH] upgraded to java 17 and JUnit 5! --- pom.xml | 10 ++--- .../mi/hdm/recipes/IngredientManager.java | 1 + src/test/java/mi/hdm/AppTest.java | 7 ++-- .../java/mi/hdm/recipes/CategoryTest.java | 37 ++++++++--------- .../java/mi/hdm/recipes/IngredientTest.java | 40 ++++++++++--------- .../hdm/recipes/NutritionCalculatorTest.java | 15 +++---- .../mi/hdm/recipes/NutritionTableTest.java | 16 ++++---- 7 files changed, 61 insertions(+), 65 deletions(-) diff --git a/pom.xml b/pom.xml index eacd70f..061e241 100644 --- a/pom.xml +++ b/pom.xml @@ -8,8 +8,8 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <maven.compiler.source>11</maven.compiler.source> - <maven.compiler.target>11</maven.compiler.target> + <maven.compiler.source>17</maven.compiler.source> + <maven.compiler.target>17</maven.compiler.target> <javafx.base.version>14.0.1</javafx.base.version> </properties> @@ -32,9 +32,9 @@ </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.12</version> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <version>5.9.2</version> <scope>test</scope> </dependency> diff --git a/src/main/java/mi/hdm/recipes/IngredientManager.java b/src/main/java/mi/hdm/recipes/IngredientManager.java index af33221..89260a3 100644 --- a/src/main/java/mi/hdm/recipes/IngredientManager.java +++ b/src/main/java/mi/hdm/recipes/IngredientManager.java @@ -13,6 +13,7 @@ public class IngredientManager { private static final IngredientManager instance = new IngredientManager(); private final List<Ingredient> allIngredients; + private IngredientManager() { allIngredients = new ArrayList<>(); } diff --git a/src/test/java/mi/hdm/AppTest.java b/src/test/java/mi/hdm/AppTest.java index f09b2e0..06f6dfa 100644 --- a/src/test/java/mi/hdm/AppTest.java +++ b/src/test/java/mi/hdm/AppTest.java @@ -1,7 +1,8 @@ package mi.hdm; -import org.junit.Assert; -import org.junit.Test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Unit test for simple App. @@ -12,7 +13,7 @@ public class AppTest { */ @Test public void testApp() { - Assert.assertTrue( true ); + Assertions.assertTrue(true); } } diff --git a/src/test/java/mi/hdm/recipes/CategoryTest.java b/src/test/java/mi/hdm/recipes/CategoryTest.java index 3edc2e9..59df0cf 100644 --- a/src/test/java/mi/hdm/recipes/CategoryTest.java +++ b/src/test/java/mi/hdm/recipes/CategoryTest.java @@ -1,11 +1,8 @@ package mi.hdm.recipes; import mi.hdm.exceptions.InvalidCategoryException; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class CategoryTest { CategoryManager underTest = CategoryManager.getInstance(); @@ -18,9 +15,9 @@ public class CategoryTest { Category c3 = new Category("ew", 0x13); Category c4 = new Category("ew", 0x100); - assertNotEquals(c1, c2); - assertEquals(c3, c4); - assertEquals(c2, c3); + Assertions.assertNotEquals(c1, c2); + Assertions.assertEquals(c3, c4); + Assertions.assertEquals(c2, c3); } @Test @@ -29,7 +26,7 @@ public class CategoryTest { underTest.addCategory("Mittag", 0x100000); underTest.addCategory("Mittag", 0x100040); - Assert.assertEquals(1, underTest.getAllCategories().size()); + Assertions.assertEquals(1, underTest.getAllCategories().size()); } @Test @@ -38,7 +35,7 @@ public class CategoryTest { underTest.addCategory("Morgen", 0x100040); underTest.addCategory("Mittag", 0x100040); - Assert.assertEquals(1, underTest.getAllCategories().size()); + Assertions.assertEquals(1, underTest.getAllCategories().size()); } @Test @@ -47,7 +44,7 @@ public class CategoryTest { underTest.addCategory("Morgen", 0x100000); underTest.addCategory("Abend", 0x100010); - Assert.assertEquals(2, underTest.getAllCategories().size()); + Assertions.assertEquals(2, underTest.getAllCategories().size()); } @Test @@ -60,26 +57,26 @@ public class CategoryTest { underTest.deleteCategory("Jan ist cool"); //expect - assertEquals(0, underTest.getAllCategories().size()); + Assertions.assertEquals(0, underTest.getAllCategories().size()); } - @Test(expected = InvalidCategoryException.class) + @Test public void canNotAddCategoryWithEmptyName() { - Category emptyName = new Category("", 0x1000); + Assertions.assertThrows(InvalidCategoryException.class, () -> new Category("", 0x1000)); } - @Test(expected = InvalidCategoryException.class) + @Test public void canNotAddCategoryWithNullName() { - Category nullName = new Category(null, 0x1000); + Assertions.assertThrows(InvalidCategoryException.class, () -> new Category(null, 0x1000)); } - @Test(expected = InvalidCategoryException.class) + @Test public void canNotAddCategoryWithNegativeCode() { - Category negativeCode = new Category("foo", -100); + Assertions.assertThrows(InvalidCategoryException.class, () -> new Category("Negative", -100)); } - @Test(expected = InvalidCategoryException.class) + @Test public void canNotAddCategoryWithCodeTooHigh() { - Category tooHighCode = new Category("foo", Integer.MAX_VALUE); + Assertions.assertThrows(InvalidCategoryException.class, () -> new Category("too high", Integer.MAX_VALUE)); } } diff --git a/src/test/java/mi/hdm/recipes/IngredientTest.java b/src/test/java/mi/hdm/recipes/IngredientTest.java index 5b9699c..82da1bd 100644 --- a/src/test/java/mi/hdm/recipes/IngredientTest.java +++ b/src/test/java/mi/hdm/recipes/IngredientTest.java @@ -1,29 +1,33 @@ package mi.hdm.recipes; import mi.hdm.exceptions.InvalidIngredientException; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class IngredientTest { IngredientManager underTest = IngredientManager.getInstance(); NutritionTable tt = ValidObjectsPool.getValidNutritionTableOne(); - @Test(expected = InvalidIngredientException.class) + @BeforeEach + public void setup() { + underTest.clearIngredients(); + } + + @Test public void shouldNotCreateIngredientWithNameNull() { - new Ingredient(Measurement.GRAM, null, tt); + Assertions.assertThrows(InvalidIngredientException.class, () -> new Ingredient(Measurement.GRAM, null, tt)); } - @Test(expected = InvalidIngredientException.class) + @Test public void shouldNotCreateIngredientWithNameEmpty() { - new Ingredient(Measurement.GRAM, "", tt); + Assertions.assertThrows(InvalidIngredientException.class, () -> new Ingredient(Measurement.GRAM, "", tt)); } - @Test(expected = InvalidIngredientException.class) + @Test public void shouldNotCreateIngredientWithNutritionTableNull() { - new Ingredient(Measurement.GRAM, "Name is not null", null); + Assertions.assertThrows(InvalidIngredientException.class, () -> new Ingredient(Measurement.GRAM, "foo", null)); } @Test @@ -32,9 +36,9 @@ public class IngredientTest { Ingredient i2 = new Ingredient(Measurement.PIECE, "Banana", tt); Ingredient i3 = new Ingredient(Measurement.GRAM, "Apple", tt); - assertEquals(i1, i2); - assertNotEquals(i1, i3); - assertNotEquals(i2, i3); + Assertions.assertEquals(i1, i2); + Assertions.assertNotEquals(i1, i3); + Assertions.assertNotEquals(i2, i3); } @Test @@ -42,13 +46,12 @@ public class IngredientTest { underTest.addIngredient(Measurement.GRAM, "Banana", tt); underTest.addIngredient(Measurement.PIECE, "Banana", tt); - Assert.assertEquals(1, underTest.getAllIngredients().size()); + Assertions.assertEquals(1, underTest.getAllIngredients().size()); } @Test public void shouldRemoveExistingIngredient() { //given - underTest.clearIngredients(); Ingredient i1 = ValidObjectsPool.getValidIngredientOne(); underTest.addIngredient(i1); @@ -56,13 +59,12 @@ public class IngredientTest { boolean shouldBeTrue = underTest.deleteIngredient(i1.getName()); //expect - assertTrue(shouldBeTrue); + Assertions.assertTrue(shouldBeTrue); } - @Test(expected = InvalidIngredientException.class) + @Test public void shouldThrowWhenRemovingWrongIngredient() { - underTest.clearIngredients(); Ingredient i1 = ValidObjectsPool.getValidIngredientOne(); - underTest.deleteIngredient(i1.getName()); + Assertions.assertThrows(InvalidIngredientException.class, () -> underTest.deleteIngredient(i1.getName())); } } diff --git a/src/test/java/mi/hdm/recipes/NutritionCalculatorTest.java b/src/test/java/mi/hdm/recipes/NutritionCalculatorTest.java index d54f4f9..cb5a28a 100644 --- a/src/test/java/mi/hdm/recipes/NutritionCalculatorTest.java +++ b/src/test/java/mi/hdm/recipes/NutritionCalculatorTest.java @@ -1,21 +1,18 @@ package mi.hdm.recipes; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class NutritionCalculatorTest { List<Ingredient> ingredients = ValidObjectsPool.getValidIngredientList(); Map<RecipeComponent, Integer> recipeMap; - - @Before + @BeforeEach public void setUp() { recipeMap = new HashMap<>(); ingredients.forEach(ingredient -> recipeMap.put(ingredient, 100)); @@ -30,12 +27,12 @@ public class NutritionCalculatorTest { NutritionTable actual = NutritionCalculator.calculateNutritionTable(recipeMap); //expect - assertEquals(expected, actual); + Assertions.assertEquals(expected, actual); } @Test public void testCalculateNutritionTableFromRecipes() { //TODO - assertTrue(true); + Assertions.assertTrue(true); } } diff --git a/src/test/java/mi/hdm/recipes/NutritionTableTest.java b/src/test/java/mi/hdm/recipes/NutritionTableTest.java index 12c7374..38176e2 100644 --- a/src/test/java/mi/hdm/recipes/NutritionTableTest.java +++ b/src/test/java/mi/hdm/recipes/NutritionTableTest.java @@ -1,19 +1,17 @@ package mi.hdm.recipes; import mi.hdm.exceptions.InvalidNutritionTableException; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - public class NutritionTableTest { - @Test(expected = InvalidNutritionTableException.class) + @Test public void shouldNotCreateInvalidNutritionTable() { - new NutritionTable(-1.0, 1.0, 1.0, 1.0, 1.0, 1.2); + Assertions.assertThrows(InvalidNutritionTableException.class, () -> new NutritionTable(-1.0, 1.0, 1.0, 1.0, 1.0, 1.2)); } @Test @@ -38,7 +36,7 @@ public class NutritionTableTest { expected.put(Nutrition.FIBERS, fibers); expected.put(Nutrition.SALT, salt); - assertEquals(expected, underTest.getTable()); + Assertions.assertEquals(expected, underTest.getTable()); } @Test @@ -47,8 +45,8 @@ public class NutritionTableTest { NutritionTable underTestTwo = ValidObjectsPool.getValidNutritionTableTwo(); NutritionTable underTestEqualToOne = new NutritionTable(100.0, 17.2, 9.4, 4.3, 2.4, 0.4); - assertNotEquals(underTestOne, underTestTwo); - assertEquals(underTestEqualToOne, underTestEqualToOne); + Assertions.assertNotEquals(underTestOne, underTestTwo); + Assertions.assertEquals(underTestEqualToOne, underTestEqualToOne); } } \ No newline at end of file -- GitLab