Skip to content
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
43 changes: 43 additions & 0 deletions java-core/src/main/java/ai/certifai/unittest/ex1/MathUtils.java
Original file line number Diff line number Diff line change
@@ -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;

}

}



118 changes: 118 additions & 0 deletions java-core/src/main/java/ai/certifai/unittest/ex2/Coffee.java
Original file line number Diff line number Diff line change
@@ -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));
* }
*/
135 changes: 135 additions & 0 deletions java-core/src/test/java/ai/certifai/unittest/ex1/MathUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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
*/

}
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@
</developer>
</developers>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
</dependencies>


</project>