diff --git a/java-core/src/main/java/ai/certifai/unittest/ex1/MathUtils.java b/java-core/src/main/java/ai/certifai/unittest/ex1/MathUtils.java new file mode 100644 index 0000000..2f440a6 --- /dev/null +++ b/java-core/src/main/java/ai/certifai/unittest/ex1/MathUtils.java @@ -0,0 +1,43 @@ +package ai.certifai.unittest.ex1; + +/** + * Demo of Unit Test (Class) + * + * @author Boon Khai Yeoh + */ + +/** + * This file contain some mathematical operation method + * which will use for the demonstration of Unit Test Function + * in the test folder + * Test file path : "test/java/ai/certifai/unitTest/ex1/MathUtilsTest.java" + * + */ + + +public class MathUtils { + + public int add(int a, int b){ + return a+b; + + } + + public int minus(int a, int b){ + return a-b; + + } + + public int mul(int a, int b){ + return a*b; + + } + + public int divide(int a,int b){ + return a/b; + + } + +} + + + diff --git a/java-core/src/main/java/ai/certifai/unittest/ex2/Coffee.java b/java-core/src/main/java/ai/certifai/unittest/ex2/Coffee.java new file mode 100644 index 0000000..ad65564 --- /dev/null +++ b/java-core/src/main/java/ai/certifai/unittest/ex2/Coffee.java @@ -0,0 +1,118 @@ +package ai.certifai.unittest.ex2; + +/** + * Demo of Unit Test (Class) + * Solution : At Bottom of this file + * Test Naming Method : methodName_expectedBehavior_stateUnderTest + * @author Boon Khai Yeoh + */ + +/** + * Task 1 : Create a test program file + * + * + * Task 2 : Create a simple test use assertEquals to evaluate the type of coffee + * by set the value Example : Coffee coffee = new Coffee("Espresso",9); + * and using coffeeTypeTest() to get the coffee type + * + * + * Task 3 : Create a simple test use assertEquals to evaluate the price of coffee + * by set the value Example : Coffee coffee = new Coffee("Latte",11); + * and using priceTest() to get the coffee price + * + * + * Task 4 : Create a simple test use assertAll to evaluate the types of coffee + * Your type of coffee should be include : Espresso, Latte ,Mocha + * and using allCoffeeTypeTest() to get the type of coffee + * + * + * Task 5 : Create a exception test to evaluate negative price value + * Type of Exception : ExceptionIllegalArgumentException + * + */ + +public class Coffee { + + private String coffeeType; + private double price; + + + + public Coffee(String coffeeType, double price){ + this.coffeeType = coffeeType; + this.price=price; + + } + + public String getCoffeeType() { + return coffeeType; + } + + public void setCoffeeType(String coffeeType) { + this.coffeeType = coffeeType; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + if (price <= -1 ){ + throw new IllegalArgumentException("Price must be >=0"); + } + this.price = price; + } + +} + +/** ============================================ + * Solution for test file + * ============================================ + * Task 1 : Create a test program file + * Put the cursor to the class name "Coffee" + * Right click -> Generate -> test + * + * + * Task 2 : Create a simple test use assertEquals to evaluate the type of coffee + * @Test + * @DisplayName("coffeeTypeTest_True_ifCoffeeTypeSameWithUserDefine") + * void coffeeTypeTest() { + * Coffee coffee = new Coffee("Espresso",9); + * assertEquals("Espresso",coffee.getCoffeeType()); + * } + * + * + * Task 3 : Create a simple test use assertEquals to evaluate the price of coffee + * @Test + * @DisplayName("priceTest_True_ifCoffeePriceSameWithUserDefine") + * void priceTest() { + * Coffee coffee = new Coffee("Latte",11); + * assertEquals(11,coffee.getPrice()); + * } + * + * + * Task 4 : Create a simple test use assertAll to evaluate the types of coffee + * @Test + * @DisplayName("allCoffeeTypeTest_True_ifAllCoffeeTypeSameWithUserDefine") + * void allCoffeeTypeTest() { + * Coffee espresso = new Coffee("Espresso",9); + * Coffee latte = new Coffee("Latte",11); + * Coffee mocha = new Coffee("Mocha",12); + * + * assertAll( + * ()->assertEquals("Espresso",espresso.getCoffeeType()), + * ()->assertEquals("Latte",latte.getCoffeeType()), + * ()->assertEquals("Mocha",mocha.getCoffeeType()) + * ); + * } + * + * + * Task 5 : Create a exception test to evaluate negative price value + * @Test + * @DisplayName("priceExceptionTest_ExceptionThrown_ifCoffeePriceNegative") + * void priceExceptionTest() { + * Coffee coffee = new Coffee("Latte",11); + * assertThrows(IllegalArgumentException.class, + * ()->coffee.setPrice(-1)); + * } + */ \ No newline at end of file diff --git a/java-core/src/test/java/ai/certifai/unittest/ex1/MathUtilsTest.java b/java-core/src/test/java/ai/certifai/unittest/ex1/MathUtilsTest.java new file mode 100644 index 0000000..69e5db0 --- /dev/null +++ b/java-core/src/test/java/ai/certifai/unittest/ex1/MathUtilsTest.java @@ -0,0 +1,135 @@ +package ai.certifai.unittest.ex1; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Demo of Unit Test (Test File) + * Test Naming Method : methodName_expectedBehavior_stateUnderTest + * @author Boon Khai Yeoh + */ + +class MathUtilsTest { + + /** + * Test Method 1 : Using assertEquals to compare two value. + * + * Usage : + * If both value are equal , the test will pass + * + */ + + @Test + @DisplayName("additionTest_True_ifCorrectSumOfTwoVariable") + void additionTest() { + MathUtils mathUtils = new MathUtils(); + int expected = 2; + int actual = mathUtils.add(1,1); + assertEquals(expected,actual,"The sum expected to be 2"); + } + + /** + * Test Method 2 : Using assertAll to compare more value + * + * Usage : + * assertAll is use to compare all the value, + * evaluate all condition compare to assertEquals + * observe the difference between assertEquals and assertAll method + * Comment the @Disabled to review the difference + */ + + @Test + @Disabled + @DisplayName("equalsMultiplicationTest_True_ifCorrectProductOfTwoVariable") + void equalsMultiplicationTest(){ + MathUtils mathUtils = new MathUtils(); + assertEquals(4,mathUtils.mul(2,2)); + assertEquals(2,mathUtils.mul(1,1)); + assertEquals(3,mathUtils.mul(3,3)); + } + + + @Test + @Disabled + @DisplayName("allMultiplicationTest_True_ifCorrectProductOfTwoVariable") + void allMultiplicationTest(){ + MathUtils mathUtils = new MathUtils(); + assertAll( + ()->assertEquals(4,mathUtils.mul(2,2)), + ()->assertEquals(2,mathUtils.mul(1,1)), + ()->assertEquals(3,mathUtils.mul(3,3)) + ); + } + + /** + * Test Method 3 : assertThrows + * + * Usage : + * Use to evaluate the Exception + * assertThrows(Type of Exception,condition) + * when condition is meet the type of Exception, + * the test is pass + * + */ + + @Test + @DisplayName("divideTest_ExceptionThrown_ifInvalidAnswer") + void divideTest(){ + MathUtils mathUtils = new MathUtils(); + assertThrows(ArithmeticException.class,() -> mathUtils.divide(1,0)); + + + } + + /** + * Test Method 4 : ParameterizedTest + * + * Usage : + * Evaluate each element in the ValueSource + * + */ + + @ParameterizedTest + @DisplayName("allElementTest_True_ifZeroRemainder") + @ValueSource(ints ={3,4,5,6}) + void allElementTest(int number){ + assertEquals(0,number%3); + + } + + + /** + * Test Method 5 : RepeatedTest + * + * Usage : + * Repeat the test base on the number input + * RepeatedTest(3) << Repeat the test 3 times + * + */ + + @RepeatedTest(3) + @DisplayName("repeatAddTest_True_ifCorrectSumOfTwoVariable") + void repeatAddTest() { + MathUtils mathUtils = new MathUtils(); + int expected = 2; + int actual = mathUtils.add(1, 1); + assertEquals(expected, actual, "The sum expected to be 2"); + } + + + /** + * Create the test function + * Task 1 : Evaluate difference of two value by create minus (subtraction) test case using assertEquals + * + * + * Task 2 : Compare more subtraction equation by using assertAll + */ + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index c20262f..fc451ca 100644 --- a/pom.xml +++ b/pom.xml @@ -49,4 +49,33 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + org.projectlombok + lombok + 1.18.16 + provided + + + + \ No newline at end of file