-
Notifications
You must be signed in to change notification settings - Fork 35
Bk junit5 test #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
evanyeohboonkhai
wants to merge
14
commits into
main
Choose a base branch
from
BK-Junit5-Test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
53c5977
add on junit5 demo and modify in pom dependency plugin
evanyeohboonkhai 246247d
add on junit5 example
evanyeohboonkhai b3b41d7
add on training file
evanyeohboonkhai cdc2fe0
rearrange file path
evanyeohboonkhai f1ab87b
rename package name to unitTest , add in introduction
evanyeohboonkhai 3e4c745
rename package and test file name
evanyeohboonkhai 6191fbd
remove test solution file , the solution change to bottom of this fil…
evanyeohboonkhai 32056da
rename package name , add test method name
evanyeohboonkhai 1a8ce29
rename package name , add test method name
evanyeohboonkhai 2128de4
rename package name , add test method name
evanyeohboonkhai 759c5d8
modify test name
evanyeohboonkhai 25a7087
modify test name
evanyeohboonkhai 60261eb
modify test name to more meaningfull name
evanyeohboonkhai b037c6a
modify test name to more meaningfull name
evanyeohboonkhai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
java-core/src/main/java/ai/certifai/junit5/ex1/BasicMethodAssertion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package ai.certifai.junit5.ex1; | ||
|
|
||
| /** | ||
| * Demo of Junit5 Test (Class) | ||
williamardianto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Test file path : test\java\ai\certifai\junit5\ex1 | ||
| * @author Boon Khai Yeoh | ||
| */ | ||
|
|
||
| public class BasicMethodAssertion { | ||
williamardianto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
59 changes: 59 additions & 0 deletions
59
java-core/src/main/java/ai/certifai/junit5/ex2/GetSetMethodAssertion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
124 changes: 124 additions & 0 deletions
124
java-core/src/test/java/ai/certifai/junit5/ex1/BasicMethodAssertionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
williamardianto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * 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 | ||
| */ | ||
|
|
||
| } | ||
43 changes: 43 additions & 0 deletions
43
java-core/src/test/java/ai/certifai/junit5/ex2/GetSetMethodAssertionTestSolution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
williamardianto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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)); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.