Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ai.certifai.junit5.ex1;

/**
* Demo of Junit5 Test (Class)
* Test file path : test\java\ai\certifai\junit5\ex1
* @author Boon Khai Yeoh
*/

public class BasicMethodAssertion {

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;

}

}



Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ai.certifai.junit5.ex2;

/**
* Demo of Junit5 Test (Class)
* solution file path : test\java\ai\certifai\junit5\ex2
* @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 : GetSetMethodAssertion coffee = new GetSetMethodAssertion("Espresso",9);
* and using getCoffeeType() to get the coffee type
*
*
* Task 3 : Create a simple test use assertEquals to evaluate the price of coffee
* by set the value Example : GetSetMethodAssertion coffee = new GetSetMethodAssertion("Espresso",9);
* and using getPrice() to get the coffee price
*
*
* Task 4 : Create a exception test to evaluate negative price value
* Type of Exception : ExceptionIllegalArgumentException
*/

public class GetSetMethodAssertion {

private String coffeeType;
private double price;



public GetSetMethodAssertion(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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package ai.certifai.junit5.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.*;
class BasicMethodAssertionTest {

/**
* Test Method 1 : Using assertEquals to compare two value.
*
* Usage :
* If both value are equal , the test will pass
*
*/

@Test
@DisplayName("Add Function Test")
void add() {
BasicMethodAssertion mathUtils = new BasicMethodAssertion();
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
*
* Comment the @Disabled to review the difference
*/

@Test
@Disabled
@DisplayName("Multiplication Function Test")
void mul(){
BasicMethodAssertion mathUtils = new BasicMethodAssertion();
assertEquals(4,mathUtils.mul(2,2));
assertEquals(2,mathUtils.mul(1,1));
assertEquals(3,mathUtils.mul(3,3));
}


@Test
@Disabled
@DisplayName("Assert ALL Multiplication Function Test")
void mul1(){
BasicMethodAssertion mathUtils = new BasicMethodAssertion();
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("Divide Function Test")
void divide(){
BasicMethodAssertion mathUtils = new BasicMethodAssertion();
assertThrows(ArithmeticException.class,() -> mathUtils.divide(1,0));

}

/**
* Test Method 4 : ParameterizedTest
*
* Usage :
* Evaluate each element in the ValueSource
*
*/

@ParameterizedTest
@ValueSource(ints ={3,4,5,6})
void test(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("Add Function Test")
void repeatAdd() {
BasicMethodAssertion mathUtils = new BasicMethodAssertion();
int expected = 2;
int actual = mathUtils.add(1, 1);
assertEquals(expected, actual, "The sum expected to be 2");
}


/**
* Task 1 : Evaluate difference of two value by create minus (subtraction) test case using assertEquals
*
*
* Task 2 : Compare more subtraction equation by using assertAll
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ai.certifai.junit5.ex2;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class GetSetMethodAssertionTestSolution {
@Test
void getCoffeeType() {
GetSetMethodAssertion coffee = new GetSetMethodAssertion("Espresso",9);
assertEquals("Espresso",coffee.getCoffeeType());
}

@Test
void getPrice() {
GetSetMethodAssertion coffee = new GetSetMethodAssertion("Latte",11);
assertEquals(11,coffee.getPrice());

}

@Test
void allCoffeeType() {
GetSetMethodAssertion espresso = new GetSetMethodAssertion("Espresso",9);
GetSetMethodAssertion latte = new GetSetMethodAssertion("Latte",11);
GetSetMethodAssertion mocha = new GetSetMethodAssertion("Mocha",12);

assertAll(
()->assertEquals("Espresso",espresso.getCoffeeType()),
()->assertEquals("Latte",latte.getCoffeeType()),
()->assertEquals("Mocha",mocha.getCoffeeType())
);
}



@Test
void exceptionTest() {
GetSetMethodAssertion coffee = new GetSetMethodAssertion("Latte",11);
assertThrows(IllegalArgumentException.class,
()->coffee.setPrice(-1));
}

}
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>