Slide 1

Slide 1 text

TEST TOOL (VER. 4) A part of the Open Source Development Tools Using JUnit with JDT™ & M2Eclipse™ Eclipse™ Plug-ins 1 By Koichi NAKAGAWA Enterprise Web Application Development Course (3)

Slide 2

Slide 2 text

Update Information • Ver. 4: Use Ubuntu Desktop/Server™ instead of Windows 10™ and Rocky Linux™ as a Desktop and Server platform respectively and Payara Server 7™ certified as Jakarta EE 11 Platform Compatible Products. • Ver. 3: Use Rocky Linux™ instead of CentOS™ as a Linux platform and Payara Server 6™ certified as Jakarta EE 9.1 Platform Compatible Products. • Ver. 2 : Use JDK 11 instead of JDK 8 in all exercises and Jakarta EE 9 instead of Jakarta EE 8 in the section of “Java Web System Test Exercise” where we develop a JSF(Jakarta Faces) based web application. 2

Slide 3

Slide 3 text

Open Source Development Tools Test Tool (JUnit) 3

Slide 4

Slide 4 text

EWA development course curriculum JSF with CDI JPA + JTA with CDI JAX-RS Application Architecture Design Patterns Eclipse IDE™ Version Control Tool with Git™ Build Tool with Apache Maven™ Payara Server™ Administration Linux (Ubuntu™) Total EWA Development Exercise Jakarta Batch Java SE (OpenJDK™) Required Skills to take courses Test Tool with JUnit5 PostgreSQL™ Administration 4 Object Oriented Development Methodology

Slide 5

Slide 5 text

Open Source Development Tools • Build Tool (Apache Maven™ with Eclipse™ Plug-in) • Version Control Tool (Git™ with Eclipse™ Plug-in) • Test Tool (JUnit5 with Eclipse™ Plug-in) 5

Slide 6

Slide 6 text

Trademarks Notice • Jakarta™ EE and their logo( ) are trademarks of the Eclipse Foundation. • Payara™ Server and their logo( ) are trademarks of Payara Services® LTD. • PrimeFaces™ is trademarks of Prime Tek Informatics. • Eclipse Java development tools™, Java development tools™, Eclipse JDT™, JDT™ are trademarks of the Eclipse Foundation. • Eclipse IDE for Enterprise Java Developers™, Eclipse IDE for Enterprise Java Developers™ logo( ) are trademarks of the Eclipse Foundation. • Eclipse m2eclipse™, m2eclipse™ are trademarks of the Eclipse Foundation. • Apache Maven™, Maven™ are trademarks of the Apache Software Foundation (ASF). • Java™ is trademark of Oracle Corporation. 6

Slide 7

Slide 7 text

Assumption for this course • The trainees who take this course are required the following skills in advance • Oracle JDK (Version: 21) • Eclipse IDE for Enterprise Java Developers (Version: 2025-06 (4.36.0)) • Payara Server (Version: 7.2025.1.Alpha4 Multi-Language) – Basic Administration Operations • Build Tool Training Course 7

Slide 8

Slide 8 text

Objectives this course • Learning the JUnit5 Test Tool, you will obtain the concepts of test tool and how to operate it through JDT™ Eclipse™ plug-in for JUnit and M2Eclipse™ Eclipse™ plug-in for Apache Maven. You also learn how to perform Java Unit Test, Java Unit Test with Mock, Java CDI Integration Test and Web Application System Test. 8

Slide 9

Slide 9 text

JUnit Test Tool • Java Test Framework Basics • JUnit Concepts • JUnit + Apache Maven™ • Eclipse Java development tools™ Eclipse Plug-in 9

Slide 10

Slide 10 text

Java Test Framework Basics 10

Slide 11

Slide 11 text

Target Java Application Components CDI Component What’s Java Testing Framework? • Definition of Java Testing Framework Test Application Check Java SE (JDK) • Dependency Injection • Transaction with JTA • Directly Access from JSF, JSP • Several Scoped Contexts • Interceptor (AOP) • Support JavaSE (from CDI 2.0) CDI: A part of Jakarta EE Java Testing Framework Web Component Java Component 11

Slide 12

Slide 12 text

Testing Types • Unit Test Java Component A Java Test Application Check Java Component A Java Test Application Check Java Component B Mock for Java Component B Create Call Simple Unit Test Unit Test with Mock JUnit5 JUnit5 + Mockito-Jupiter Extension Mockito Java SE (JDK) Java SE (JDK) 12

Slide 13

Slide 13 text

Testing Types • Integration Test CDI Component A CDI Test Application Check CDI Component B Integration Test between CDI Components on Java SE JUnit5 + Weld SE + Weld JUnit 5 Extensions (for CDI Components) Java SE (JDK) 13

Slide 14

Slide 14 text

Java SE (JDK) Payara Server (Jakarta EE Framework) Testing Types • System Test CDI Component A Web Test Application Check CDI Component B System Test for Total Java Application JUnit5 + Selenium + Selenium-Jupiter Extension (for Web Interface Components) Web Component C Java SE (JDK) 14

Slide 15

Slide 15 text

JUnit Concepts 15

Slide 16

Slide 16 text

What’s JUnit? • Definition of JUnit JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. (Source: JUnit. “About JUnit4”, https://junit.org/junit4/) JUnit 5 is the next generation of JUnit. The goal is to create an up-to-date foundation for developer-side testing on the JVM. This includes focusing on Java 8 and above, as well as enabling many different styles of testing. JUnit 5 is the result of JUnit Lambda and its crowdfunding campaign on Indiegogo. (Source: JUnit. “About JUnit5”, https://junit.org/junit5/) 16

Slide 17

Slide 17 text

JUnit 5 • JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit Platform Java SE (JDK) Launching testing framework foundation (Run TestEngine from IDE like Eclipse™ and Build Tool like Maven™) TestEngine API JUnit Jupiter (TestEngine for JUnit 5) JUnit Vintage (TestEngine for JUnit 3 & 4) JUnit 5 Test Application JUnit 3 & 4 Test Application Launch TestEngine Launch TestEngine 17

Slide 18

Slide 18 text

JUnit Test Application: Test class and test • Test class and method public class XyzTest { : @Test public void funcTest1() { : assertXXXX(); } @Test public void funcTest2() { : } : } Test class : - Definition: Java Class including Test methods. - Naming Convention: xxxTest (Apache Maven™ automatically detect Test classes which have “Test” as its suffix to put it in its “test scope”.) Test method: - Definition: Java Method annotated with @Test inside Test class. - Validation: “assert” methods are used inside to validate the test results. - Naming Convention: meaningful names should be used to be understood easily. - Execution Order: Arbitrary order (Any test should not be dependent on each other.) Test Method Test Method Test class Assertions: Judge whether this test method finished successfully or not. 18

Slide 19

Slide 19 text

JUnit: Annotations for test • Useful Annotations for test (1) Annotation Meaning @Test Identifies a method as a test method. @Before (JUnit4) @BeforeEach (JUnit5) Executed before each test. It is used to prepare the test environment (e.g., read input data, initialize the class). @After (JUnit4) @AfterEach (JUnit5) Executed after each test. It is used to cleanup the test environment (e.g., delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures. @BeforeClass (JUnit4) @BeforeAll (JUnit5) Executed once, before the start of all tests. It is used to perform time intensive activities, for example, to connect to a database. Methods marked with this annotation need to be defined as static to work with JUnit. @AfterClass (JUnit4) @AfterAll (JUnit5) Executed once, after all tests have been finished. It is used to perform clean-up activities, for example, to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit. 19

Slide 20

Slide 20 text

JUnit: Annotations for test • Useful Annotations for test (2) Annotation Meaning @Ignore(["Why disabled“]) (JUnit4) @Disabled([“Why disabled”]) (JUnit5) Marks that the test should be disabled. This is useful when the underlying code has been changed and the test case has not yet been adapted. With JUnit5, assumeTrue() or assumeFalse() asserts can be also used to disable the test, when a condition is false or true respectively. @Test (expected = Exception.class) (JUnit4) Fails if the method does not throw the named exception. With JUnit5, assertThrows() assert can be used for this purpose. @Test(timeout=100) (JUnit4) Fails if the method takes longer than a specified value in milliseconds. With JUnit5, assertTimeout() assert can be used for this purpose. @DisplayName(“message”) (JUnit5) Declare a custom display name for the annotated test class or test method. 20

Slide 21

Slide 21 text

JUnit: Asserts for test • Useful Asserts for test (1) Assert Meaning fail([message]) Let the method fail. Might be used to check that a certain part of the code is not reached or to have a failing test before the test code is implemented. The message parameter is optional. assertTrue([message,] boolean condition) Checks that the boolean condition is true. assertFalse([message,] boolean condition) Checks that the boolean condition is false. assertEquals([message,] expected, actual) Tests that two values are the same. Note: for arrays the reference is checked, not the content of the arrays. assertEquals([message,] expected, actual, tolerance) Test that float or double values match. The tolerance is the number of decimals which must be the same. 21

Slide 22

Slide 22 text

JUnit: Asserts for test • Useful Asserts for test (2) Assert Meaning assertNull([message,] object) Checks that the object is null. assertNotNull([message,] object) Checks that the object is not null. assertSame([message,] expected, actual) Checks that both variables refer to the same object. assertNotSame([message,] expected, actual) Checks that both variables refer to different objects. 22

Slide 23

Slide 23 text

Java Unit Test • Check if a target class works correctly import static org.junit.jupiter.api.Assertions.assertXXX; import org.junit.jupiter.api.Test; public class XXXTest { @Test public void yyy() { : : assertXXX(); } } XXXTest.java Check if Component A worked correctly with assertions. In case of JUnit5 Test Application Test Class Test Method Java Component A Java Test Application JUnit Java SE (JDK) Test Code for Component A 23

Slide 24

Slide 24 text

Exercise for Java Unit Test • Let’s make a JUnit Test program for Java Unit Test  Check if the “evaluate” method of “Calculate” class works correctly public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java Target Test Class 24 Exercise: Let’s write a Java Test Application for the above Target Class.

Slide 25

Slide 25 text

Exercise for Java Unit Test • Sample JUnit Test program for Java Unit Test import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void evaluatesExpression() { Calculator calculator = new Calculator(); int sum = calculator.evaluate("1+2+3"); assertEquals(6, sum); } } CalculatorTest.java Check if the sum of “1+2+3” is “6”. 25

Slide 26

Slide 26 text

Java Unit Test with Mock • Check if a target class works correctly using Mock technology @ExtendWith(MockitoExtension.class) public class XXXTest { @Mock ComponentB compB; @InjectMocks ComponentA compA; @Test public void funcTest1() { when(compB.func1()).thenReturn(new ComponentB()); compA.funcA(); : } } XXXTest.java Java Component A Java Test Application Check Java Component B Mock for Java Component B Create Call JUnit + Mockito-Jupiter Extension Mockito Java SE (JDK) Mocked Component Component Injecting Mocked Component Specify when a method of Mocked Component is called and what is returned. Extend with Mockito Extension When ComponentB#func1() is called inside ComponentA#funcA(), an instance of ComponentB is returned. 26

Slide 27

Slide 27 text

Exercise for Java Unit Test with Mock • Let’s make a JUnit Test program for Java Unit Test using Mock  Check if the “add” method of “Score” class works correctly by mocking “Calculator” class public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java public class Score { private Calculator calc; public int add(String expA, String expB) { int sum = calc.evaluate(expA) + calc.evaluate(expB); return sum; } } Score.java Mocked by Mokito Target Test Class 27 Exercise: Let’s write a Java Test Application for the above Target Test Class.

Slide 28

Slide 28 text

Exercise for Java Unit Test with Mock • Sample JUnit Test program for Java Unit Test with Mock @ExtendWith(MockitoExtension.class) public class ScoreTest { @Mock Calculator calc; @InjectMocks Score score; @Test public void testAdd() { when(calc.evaluate(anyString())).thenReturn(5); int sum = score.add(“1+4”, “1+2+2”); assertEquals(10, sum); } : } ScoreTest.java 28

Slide 29

Slide 29 text

Java CDI Integration Test • Check if a target CDI class works correctly communicating with other CDI classes @EnableWeld public class XXXTest { @WeldSetup public WeldInitiator weld = WeldInitiator.from(ComponentA.class, ComponentB.class) .activate(RequestScoped.class, ApplicationScoped.class).build(); @Inject ComponentA compA ; @Test public void funcTest1() { : assertXXX(…); } } XXXTest.java Enable Weld SE with Weld JUnit 5 Extension Initialize Weld SE with CDI components Inject the target CDI component Test the target CDI component CDI Component A CDI Test Application Check CDI Component B JUnit + Weld SE + Weld JUnit 5 Extension (for CDI Components) Java SE (JDK) 29

Slide 30

Slide 30 text

Exercise for Java CDI Integration Test • Let’s make a Junit Test program for Java CDI Integration Test  Check if the “add()” method of “Score” class works correctly along with “Calculator” class @RequestScoped public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java @RequestScoped public class Score { @Inject private Calculator calc; public int add(String expA, String expB) { int sum = calc.evaluate(expA) + calc.evaluate(expB); return sum; } } Score.java Injected by CDI Target Test Class 30 Exercise: Let’s write a Java Test Application for the above Target Test Class.

Slide 31

Slide 31 text

Exercise for Java CDI Integration Test • Sample JUnit Test program for Java CDI Integration Test @EnableWeld public class ScoreTest { @WeldSetup public WeldInitiator weld = WeldInitiator.from (Score.class, Calculator.class) .activate(RequestScoped.class, RequestScoped.class).build(); @Inject Score score; @Test public void testAdd() { int sum = score.add(“1+2+3”, “3+6”); assertEquals(15, sum); } } ScoreTest.java 31

Slide 32

Slide 32 text

Java Web System Test • Check if a target Web Component works correctly communicating with other CDI classes @ExtendWith(SeleniumExtension.class) public class XXXIT { @Test public void testChrome(ChromeDriver driver) { driver.get(“"); WebElement element = driver.findElement(By.id(“")); element.sendKeys(“Input Data"); element = driver.findElement(By.id(“")); element.click(); assertThat(driver.getTitle(), startsWith(“")); } } XXXIT.java Enable Selenium-Jupiter JUnit5 Extension Inject Chrome Driver Search for a web element Click a button Java SE (JDK) Payara Server (Jakarta EE Framework) CDI Component A Web Test Application Check CDI Component B JUnit + Selenium + Selenium-Jupiter Extension (for Web Interface Components) Web Component C Java SE (JDK) Enter some data in the web element 32

Slide 33

Slide 33 text

@ReuqestScoped @Named public class Score { @Inject private Calculator calc; private String expA, expB, expSum; public void add() { int sum = calc.evaluate(expA) + calc.evaluate(expB); expSum = new Integer(sum).toString(); } } Exercise for Java Web System Test • Let’s make a JUnit Test program for Java Web System Test Check if the “Calculate” button of a JSF menu works correctly @RequestScoped public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java Score.java CDI Bean for JSF A: (Id: expA) B: (Id: expB) = (Id: expSum) + Calculate Accessors are required Web menu using JSF(Jakarta Faces) (URL: http://localhost:8080/app/) (Id: calculate) Bind Target Web Menu 33 Exercise: Let’s write a Java Test Application for the above Target Web Component.

Slide 34

Slide 34 text

Exercise for Java Web System Test • Sample JUnit Test program for Java Web System Test @ ExtendWith(SeleniumJupiter.class) public class ScoreIT { @Test public void testAdd(ChromeDriver driver) throws InterruptedException{ driver.get(“http://localhost:8080/app/"); WebElement element = driver.findElement(By.id(“expA")); element.sendKeys(“1+2+3"); element = driver.findElement(By.id(“expB")); element.sendKeys(“5+7+9"); element = driver.findElement(By.id(“calculate")); element.click(); Thread.sleep(5000); element = driver.findElement(By.id(“expSum")); assertEquals(“27”, element.getText()); } } ScoreIT.java 34 ChromeDriver is automatically downloaded over internet and stored into the Maven local repository at its “integration-test” phase.

Slide 35

Slide 35 text

JUnit + Apache Maven™ 35

Slide 36

Slide 36 text

Apache Maven™ Build Life Cycle • Test Phase of Apache Maven™ Build Life Cycle Compile Test Package Integrat ion Test Install Deploy • Java Unit Test • Java Unit Test with Mock • CDI Integration Test maven-surefire-plugin 36

Slide 37

Slide 37 text

Unit Test with JUnit + Apache Maven™ • Location of Test Programs using Apache Maven™ Package Folders xxxTest.java “maven-surefire-plugin” fetches these classes as test class. 37 Project src main test target pom.xml

Slide 38

Slide 38 text

Java Unit Test • pom.xml for JUnit Test Application (1) ... maven-surefire-plugin 3.5.3 ... From 2.22.0, maven-surefire-plugin supports JUnit Platform (JUnit5) natively. 38

Slide 39

Slide 39 text

Java Unit Test • pom.xml for JUnit Test Application (2) ... junit junit 4.13.2 test org.junit.vintage junit-vintage-engine 5.13.1 test ... ... org.junit.jupiter junit-jupiter-api 5.13.1 test org.junit.jupiter junit-jupiter-engine 5.13.1 test ... (continued…) For JUnit4 Application For JUnit5 Application 39

Slide 40

Slide 40 text

Java Unit Test • pom.xml for JUnit Test Application (3) ... org.hamcrest hamcrest 3.0 test ... For JUnit5 Application Including useful Assertions which are bundled in JUnit4 APIs 40

Slide 41

Slide 41 text

Java Unit Test with Mock • pom.xml for JUnit Test Application with Mockito ... org.mockito mockito-core 5.18.0 test org.mockito mockito-junit-jupiter 5.18.0 test ... Including Mockito Core functionalities Including Mockito Extension for JUnit5 For JUnit5 Application 41

Slide 42

Slide 42 text

Java CDI Integration Test • pom.xml for CDI Integration Test Application with Weld JUnit5 Extension ... org.jboss.weld weld-junit5 5.0.1.Final test org.jboss.weld.se weld-se-core 6.0.3.Final ... Including Weld-JUnit5 functionalities For JUnit5 Application 42

Slide 43

Slide 43 text

Apache Maven™ Build Life Cycle • Integration Test Phase of Apache Maven™ Build Life Cycle Compile Test Package Integrat ion Test Install Deploy • Web System Test with Selenium-Jupiter maven-failsafe-plugin 43

Slide 44

Slide 44 text

System Test with JUnit + Apache Maven™ • Location of Integration Test Programs using Apache Maven™ 44 Project src main test target pom.xml Package Folders xxxIT.java “maven-failsafe-plugin” fetches these classes as integration test class.

Slide 45

Slide 45 text

Web System Test • pom.xml for Web System Test Application with Selenium-Jupiter JUnit5 Extension (1) ... io.github.bonigarcia selenium-jupiter 6.1.1 test org.seleniumhq.selenium selenium-devtools-v137 4.33.0 test ... 45 ... org.seleniumhq.selenium selenium-chrome-driver 4.33.0 test org.seleniumhq.selenium selenium-firefox-driver 4.33.0 test ... Including one of the Selenium WebDriver (This dependency shows “FireFoxDriver”) Including one of the Selenium WebDriver (This dependency shows “ChromeDriver”) For the specific version of WebDriver (Option) Including Selenium-Jupiter functionalities

Slide 46

Slide 46 text

Web System Test • pom.xml for Web System Test Application with Selenium-Jupiter JUnit5 Extension (2) : org.apache.maven.plugins maven-failsafe-plugin 3.5.3 perform-it integration-test : Take care of tests in “Integration Test” phase of Maven life-cycle 46

Slide 47

Slide 47 text

Eclipse Java development tools™ Eclipse Plug-in 47

Slide 48

Slide 48 text

Eclipse Java development tools™ (JDT™) • JDT™ Eclipse Plug-in JDT™ includes JUnit Plug-in for Eclipse and is bundled into Eclipse IDE for Enterprise Java Developers™ 48

Slide 49

Slide 49 text

How to execute Tests with Eclipse™ • Execute JUnit directly from Eclipse  Right click on the project in Package Explorer and select “Run As”  “2 JUnit Test”. 49

Slide 50

Slide 50 text

How to execute Tests with Eclipse™ • Execute JUnit through M2Eclipse™ (Eclipse™ plugin for Apache Maven™)  Right click on the project in Package Explorer and select “Run As” “4 Maven build …”. Build Name Goal Name Profile Name Ex. clean package, integration-test Ex. payara5x-local, payara5x-remote 50

Slide 51

Slide 51 text

• Create a new Eclipse™ workspace and configure “Installed JREs” for the workspace 51 Eclipse™ Workspace /home//eclipse-workspace/ws3 Configure “Installed JREs” Exercise: Let’s create a new Eclipse™ workspace and configure “Installed JREs” for the workspace with the above information. Exercise to prepare for Java Test Environment

Slide 52

Slide 52 text

• Create a new Eclipse™ Workspace named “ws3”.  Click “File”  “Switch Workspace”  “Other…”  Fill in the new workspace folder path of “ws3” and click “Launch” button on “Select a directory as workspace” pane screen. /home//eclipse-workspace/ws3 52 Exercise to prepare for Java Test Environment

Slide 53

Slide 53 text

• (Option) Configure Installed JRE (1) [If JDK21 has not been selected yet] 1. Select “Window  Preferences” and navigate to “Preferences” dialog screen. 2. Select “Java  Installed JREs” and click “Add …” button. 53 Exercise to prepare for Java Test Environment java-17-openjdk-amd64 /usr/lib/jvm/java-

Slide 54

Slide 54 text

• (Option) Configure Installed JRE (2) [If JDK21 has not been selected yet] 3. Select “Standard VM” and click “Next >” button and “Add JRE” dialog menu comes up. 4. Click “Directory …” button and select an installed JDK home directory and click “Finish” button. 54 Exercise to prepare for Java Test Environment

Slide 55

Slide 55 text

• (Option) Configure Installed JRE (3) [If JDK21 has not been selected yet] 5. Select the added JDK entry from a list of “Installed JREs” and click “Apply and Close” button. 55 Exercise to prepare for Java Test Environment ava-17-openjdk-amd64 /usr/lib/jvm/java-17- op

Slide 56

Slide 56 text

Exercise for Java Unit Test • Check if the “evaluate()” method of “Calculator” class works correctly. public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java Target Class 56 Exercise: Let’s make a Java Unit Test Application to test the “evaluate()” method of the “Calculator” class with the above information.

Slide 57

Slide 57 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.x ml Make the Calculat or Java Class Make a JUnit Test Class for the Calculat or Execut e JUnit from Eclipse Execut e JUnit from Maven 57

Slide 58

Slide 58 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.x ml Make the Calculat or Java Class Make a JUnit Test Class for the Calculat or Execut e JUnit from Eclipse Execut e JUnit from Maven 58

Slide 59

Slide 59 text

Exercise for Java Unit Test • Create a new Eclipse™ Project named “test1” (1)  Click “Create a Maven project” in Project Explorer. And then “New Maven Project” dialog screen comes up. 59

Slide 60

Slide 60 text

Exercise for Java Unit Test • Create a new Eclipse™ Project named “test1” (2)  Confirm “Use default Workspace location” is selected and click “Next >” button on “Select project name and location” pane screen.  Enter “maven-archetype-simple” in “Filter” field and select an archetype of “org.apache.maven.archetypes maven-archetype-simple 1.5” and then click “Next>” button on “Select an Archetype” pane screen. 60

Slide 61

Slide 61 text

Exercise for Java Unit Test • Create a new Eclipse™ Project named “test1” (3)  On “Specify Archetype parameters” pane screen, enter “org.example” in “Group Id” field and “test1” in “Artifact Id” field and “0.0.1” in “Version” field and click “Finish” button. 61

Slide 62

Slide 62 text

Exercise for Java Unit Test • Modify the created Maven Object Model file (pom.xml)  Change the compiler version to “21”.  Change the version of “maven-surefire-plugin” to “3.5.3” and delete other plug-ins. maven-surefire-plugin 3.5.3 UTF-8 21 21 62 UTF-8 8 8

Slide 63

Slide 63 text

Exercise for Java Unit Test • Modify the created Maven Object Model file (pom.xml) Add “junit-jupiter-api”, “junit-jupiter-engine” and “hamcrest” as dependency and delete “junit” dependency. org.hamcrest hamcrest 3.0 test 63 org.junit.jupiter junit-jupiter-api 5.13.1 test org.junit.jupiter junit-jupiter-engine 5.13.1 test

Slide 64

Slide 64 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.x ml Make the Calculat or Java Class Make a JUnit Test Class for the Calculat or Execut e JUnit from Eclipse Execut e JUnit from Maven 64

Slide 65

Slide 65 text

• Created a Java source file named “Calculator.java”  Right click on the package of “org.example.test1” under “src/main/java” folder and select “New”  “Class”.  Fill “Calculator” in “Name” field and click “Finish” button on “New Java Class” dialog screen. 65 Exercise for Java Unit Test

Slide 66

Slide 66 text

• Modify the created Java source file (Calculator.java)  In the “Calculator” class, add a method of “evaluate(String expression)”. package org.example.test1; public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } 66 Exercise for Java Unit Test

Slide 67

Slide 67 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.x ml Make the Calculat or Java Class Make a JUnit Test Class for the Calculat or Execut e JUnit from Eclipse Execut e JUnit from Maven 67

Slide 68

Slide 68 text

• Create a Test Class file for the created Java source file named “CalculateTest.java”  Right click on “org.example.test1” package under “src/test/java” folder and select “New”  “Other…”.  Select “Java”  “JUnit”  “JUnit Test Case” and click “Next >” button on “Select a wizard” pane screen. 68 Exercise for Java Unit Test

Slide 69

Slide 69 text

• Create a Test Class file for the created Java source file named “CalculateTest.java”  Check if “New JUnit Jupiter test” is selected on “JUnit Test Case” pane screen.  Enter “CalculateTest” in “Name” field and click “Finish” button.  Delete “AppTest.ava” of “org.example.test1” package under “src/test/java” folder. 69 Exercise for Java Unit Test

Slide 70

Slide 70 text

• Modify the created Java Test source file (CalculatorTest.java)  In the “CalculatorTest” class, add a method of “evaluateExpression()” with “@Test” annotation. package org.example.test1; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class CalculateTest { @Test public void evaluatesExpression() { Calculator calculator = new Calculator(); int sum = calculator.evaluate("1+2+3"); assertEquals(6, sum); } } 70 Exercise for Java Unit Test

Slide 71

Slide 71 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.x ml Make the Calculat or Java Class Make a JUnit Test Class for the Calculat or Execut e JUnit from Eclipse Execut e JUnit from Maven 71

Slide 72

Slide 72 text

• Execute JUnit directly for the created Java Test source file  Right click on “test1” project and select “Run As”  “JUnit Test”.  Confirm that JUnit finished successfully showing “Green” flag in “JUnit” pane screen. 72 Exercise for Java Unit Test

Slide 73

Slide 73 text

• If we got an error like the above Error Dialog, change the configuration of JUnit Test  Right click on the project and select “Run As”  “Run Configurations…”  Select “JUnit  test1” and change “Test runner” to “JUnit 5” and click “Run” button. 73 Error Dialog Exercise for Java Unit Test

Slide 74

Slide 74 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.x ml Make the Calculat or Java Class Make a JUnit Test Class for the Calculat or Execut e JUnit from Eclipse Execut e JUnit from Maven 74

Slide 75

Slide 75 text

• Execute JUnit through M2Eclipse™ for the created Java Test source file  Right click on “test1” project and select “Run As”  “Maven build...”  Put “test1 – package” in “Name” field and “clean package” in “Goals” field and click “Run” button.  Confirm that test completes successfully in “Console” pane screen. 75 Exercise for Java Unit Test

Slide 76

Slide 76 text

Exercise for Java Unit Test with Mock • Check if the “add” method of “Score” class works correctly by mocking “Calculator” class. public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java public class Score { private Calculator calc; public int add(String expA, String expB) { int sum = calc.evaluate(expA) + calc.evaluate(expB); return sum; } } Score.java Mocked by Mokito Target Class Mocked Class 76 Exercise: Let’s make a Java Unit Test Application to test the “add()” method of the “Score” class by mocking the “Calculator” class with the above information.

Slide 77

Slide 77 text

• Procedure of the Exercise Edit po m.x ml Make the Score Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 77 Exercise for Java Unit Test with Mock

Slide 78

Slide 78 text

• Modify the Maven Object Model file (pom.xml) For “test1” project, add “mockito-core”and “mockito-junit-jupiter” as dependency. : org.mockito mockito-core 5.18.0 test org.mockito mockito-junit-jupiter 5.18.0 test : 78 Exercise for Java Unit Test with Mock

Slide 79

Slide 79 text

• Procedure of the Exercise Edit po m.x ml Make the Score Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 79 Exercise for Java Unit Test with Mock

Slide 80

Slide 80 text

• Created a Java source file named “Score.java”  Right click on “org.example.test1” package under “src/main/java” folder and select “New”  “Class”. And “New Java Class” dialog screen comes up.  Fill “Score” in “Name” field and click “Finish” button on “Java Class” pane screen. 80 Exercise for Java Unit Test with Mock

Slide 81

Slide 81 text

• Modify the created Java source file (Score.java)  In the “Score” class, add a property of “Calculator” class’s instance named “calc” and a method of “add(String expA, String expB)” which includes the following logic. package org.example.test1; public class Score { private Calculator calc; public int add(String expA, String expB) { int sum = calc.evaluate(expA) + calc.evaluate(expB); return sum; } } 81 Exercise for Java Unit Test with Mock

Slide 82

Slide 82 text

• Procedure of the Exercise Edit po m.x ml Make the Score Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 82 Exercise for Java Unit Test with Mock

Slide 83

Slide 83 text

• Create a Test Class file for the created Java source file named “ScoreTest.java”  Right click on the package of “org.example.test1” under “src/test/java” folder and select “New”  “Other…”. And “New” dialog screen comes up.  Select “Java”  “JUnit”  “JUnit Test Case” and click “Next >” button on “Select a wizard” pane screen. 83 Exercise for Java Unit Test with Mock

Slide 84

Slide 84 text

• Create a Test Class file for the created Java source file named “ScoreTest.java”  Fill “ScoreTest” in the Test Class’s “Name” field and click “Finish” button on “New JUnit Test Case” pane screen. 84 Exercise for Java Unit Test with Mock

Slide 85

Slide 85 text

• Modify the created Java Test source file (ScoreTest.java)  For the “ScoreTest” class, add “@ExtendWith(MockitoExtension.class)” annotation.  In the “ScoreTest” class, add a Mock property of “Calculator” with “@Mock” and a property injecting Mocks of “Score” with “InjectMocks” annotation and a method of “testAdd()” with “@Test” annotation. In the method, add a mocking condition using “when”. package org.example.test1; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.InjectMocks; import org.mockito.junit.jupiter.MockitoExtension; import static org.mockito.Mockito.when; import static org.mockito.ArgumentMatchers.anyString; @ExtendWith(MockitoExtension.class) class ScoreTest { @Mock Calculator calc; @InjectMocks Score score; @Test public void testAdd() { when(calc.evaluate(anyString())).thenReturn(5); int sum = score.add("1+4", "1+2+2"); assertEquals(10, sum); } } 85 Exercise for Java Unit Test with Mock

Slide 86

Slide 86 text

• Procedure of the Exercise Edit po m.x ml Make the Score Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 86 Exercise for Java Unit Test with Mock

Slide 87

Slide 87 text

• Execute JUnit directly for the created Java Test source file (1)  Right click on “test1” project and select “Run As”  “Run Configurations...”  Click “JUnit” from the right menu and click “New Configuration” icon on “Run Configuration” pane screen. 87 Exercise for Java Unit Test with Mock

Slide 88

Slide 88 text

• Execute JUnit directly for the created Java Test source file (2)  Select “Arguments” tag and add some VM Arguments like the following and click “Run” button on “Run Configuration” pane screen.  Confirm that JUnit finished successfully showing “Green” flag. 88 Exercise for Java Unit Test with Mock -ea -Xshare:off -javaagent:${env_var:HOME}/.m2/repository/org/mockito/mockito-core/5.18.0/mockito-core-5.18.0.jar

Slide 89

Slide 89 text

• Procedure of the Exercise Edit po m.x ml Make the Score Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 89 Exercise for Java Unit Test with Mock

Slide 90

Slide 90 text

• Execute JUnit through M2Eclipse™ for the created Java Test source file (1)  Revise the “pom.xml” adding a configuration for “maven-surefire-plugin” plugin like the following. 90 Exercise for Java Unit Test with Mock maven-surefire-plugin -javaagent:${settings.localRepository}/org/mockito/mockito-core/5.18.0/mockito-core-5.18.0.jar -Xshare:off

Slide 91

Slide 91 text

• Execute JUnit through M2Eclipse™ for the created Java Test source file (2)  Right click on “test1” project and select “Run As”  “test1 – package (Maven Build)”. Confirm that test completes successfully in “Console” pane screen. 91 Exercise for Java Unit Test with Mock

Slide 92

Slide 92 text

Exercise for Java CDI Integration Test • Check if the “add” method of “Score” CDI class works correctly along with “Calculator” CDI class @RequestScoped public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } Calculator.java @RequestScoped public class Score { @Inject private Calculator calc; public int add(String expA, String expB) { int sum = calc.evaluate(expA) + calc.evaluate(expB); return sum; } } Score.java Injected by CDI Target Class Injected Class 92 Exercise: Let’s make a Java CDI Integration Test Application to test the “add()” method of the “Score” CDI class injecting the “Calculator” CDI class with the above information.

Slide 93

Slide 93 text

• Procedure of the Exercise Edit po m.x ml Edit the Score and Calculato r Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 93 Exercise for Java CDI Integration Test

Slide 94

Slide 94 text

• Modify the Maven Object Model file (pom.xml) Add “weld-junit5” and “weld-se-core” as dependency in pom.xml of project “test1”. : org.jboss.weld weld-junit5 5.0.1.Final test org.jboss.weld.se weld-se-core 6.0.3.Final : 94 Exercise for Java CDI Integration Test

Slide 95

Slide 95 text

• Procedure of the Exercise Edit po m.x ml Edit the Score and Calculato r Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 95 Exercise for Java CDI Integration Test

Slide 96

Slide 96 text

• Modify the Java source file (Score.java)  In the “Score” class, add an annotation of “@RequestScoped” to its Class definition and add an annotation of “@Inject” for the “Calculator” property as a DI component. package org.example.test1; import jakarta.inject.Inject; import jakarta.enterprise.context.RequestScoped; @RequestScoped public class Score { @Inject private Calculator calc; public int add(String expA, String expB) { int sum = calc.evaluate(expA) + calc.evaluate(expB); return sum; } } 96 Exercise for Java CDI Integration Test

Slide 97

Slide 97 text

• Modify the Java source file (Calculator.java)  In the “Calculator” class, add an annotation of “@RequestScoped” to its Class definition. package org.example.test1; import jakarta.enterprise.context.RequestScoped; @RequestScoped public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } } 97 Exercise for Java CDI Integration Test

Slide 98

Slide 98 text

• Procedure of the Exercise Edit po m.x ml Edit the Score and Calculato r Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 98 Exercise for Java CDI Integration Test

Slide 99

Slide 99 text

• Create a Test Class file for the created Java source file named “ScoreCDITest.java”  Right click on the package of “org.example.test1” under “src/test/java” folder and select “New”  “Other…”.  Select “Java”  “JUnit”  “JUnit Test Case” and click “Next >” button 99 Exercise for Java CDI Integration Test

Slide 100

Slide 100 text

• Create a Test Class file for the created Java source file named “ScoreCDITest.java”  Fill “ScoreCDITest” in the Test Class “Name” field and click “Finish” button. 100 Exercise for Java CDI Integration Test

Slide 101

Slide 101 text

• Modify the Java Test source file (ScoreCDITest.java) Just before the “ScoreCDITest” class definition, add “@EnableWeld”annotation. In the “ScoreCDITest” class, setup Weld with “@WeldSetup” for “Score” and “Caluculator” CDIs and add a property injecting “Score” CDI with “@Inject” annotation and a method of “testAdd()” with “@Test” annotation. package org.example.test1; import jakarta.inject.Inject; import jakarta.enterprise.context.RequestScoped; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.jboss.weld.junit5.EnableWeld; import org.jboss.weld.junit5.WeldSetup; import org.jboss.weld.junit5.WeldInitiator; @EnableWeld class ScoreCDITest { @WeldSetup public WeldInitiator weld = WeldInitiator.from( Score.class, Calculator.class).activate( RequestScoped.class, RequestScoped.class).build(); @Inject Score score; @Test public void testAdd() { int sum = score.add("1+4", "1+2+2"); assertEquals(10, sum); } } 101 Exercise for Java CDI Integration Test

Slide 102

Slide 102 text

• Procedure of the Exercise Edit po m.x ml Edit the Score and Calculato r Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 102 Exercise for Java CDI Integration Test

Slide 103

Slide 103 text

• Execute JUnit directly for the created Java Test source file  Right click on “test1” project and select “Run As”  “2 JUnit Test”  Select “JUnit” tab and confirm that JUnit finished successfully showing “Green” flag. 103 Exercise for Java CDI Integration Test

Slide 104

Slide 104 text

• Procedure of the Exercise Edit po m.x ml Edit the Score and Calculato r Java Class Make a JUnit Test Class for the Score Execut e JUnit from Eclipse Execut e JUnit from Maven 104 Exercise for Java CDI Integration Test

Slide 105

Slide 105 text

• Execute JUnit through M2Eclipse™ for the created Java Test source file  Right click on “test1” project and select “Run As”  “Maven build”.  Confirm that test completes successfully in “Console” pane screen. 105 Exercise for Java CDI Integration Test

Slide 106

Slide 106 text

Exercise for Java Web System Test • Check if a JSF menu works correctly binding the Score CDI component. @RequestScoped public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\\ +")) sum += Integer.valueOf(summand); return sum; } } Calculator.java @ReuqestScoped @Named public class Score { @Inject private Calculator calc; private String expA, expB, expSum; public void add() { int sum = calc.evaluate(expA) + calc.evaluate(expB); expSum = new Integer(sum).toString(); } } Score.java CDI Bean for JSF A: (Id: expA) B: (Id: expB) = (Id: expSum) + Calculate Accessors are required Web menu using JSF (Id: calculate) Injected by CDI Bind Target Web Menu 106 Exercise: Let’s make a Java Web System Test Application to test a Web menu built with JSF Facelet which binds the “Score” CDI component.

Slide 107

Slide 107 text

Exercise for Java Web System Test • Procedure of the Exercise Make a Project and edit pom.xml Copy Score and Calculator Java Class and edit Score Make a web interface and JUnit Test Class for it Execut e JUnit from Maven 107

Slide 108

Slide 108 text

• Create a new Eclipse™ Project named “test2” (1)  Right click in Project Explorer and navigate to “New Project” dialog screen by selecting “New”  “Project…”.  Select “Maven”  “Maven Project” and click “Next >” button on “Select a wizard” pane screen. 108 Exercise for Java Web System Test

Slide 109

Slide 109 text

• Create a new Eclipse™ Project named “test2” (2)  Confirm “Use default Workspace location” is selected and click “Next >” button on “Select project name and location” pane screen.  From “Select an Archetype” pane screen, enter “webapp-jakarta” in Filter field and select “io.github.juneau001 webapp-jakartaee11-jdk21 1.0.0” and click “Next >” button on “Select an Archetype” pane screen. 109 Exercise for Java Web System Test

Slide 110

Slide 110 text

• Create a new Eclipse™ Project named “test2” (3)  On “Specify Achetype parameters” pane screen, enter “org.example” in “Group Id” field and “test2” in “Artifact Id” field and “0.0.1” in “Version” field and click “Finish” button. 110 Exercise for Java Web System Test

Slide 111

Slide 111 text

• Modify the Maven Object Model file (pom.xml) (1)  Replace all properties to access Payara Server™ which accommodates the target war file. . . . UTF-8 11.0.0 21 21 8080 4848 admin <> localhost domain1 <> ${payara.home}/glassfish/domains false 111 Exercise for Java Web System Test This directory must be able to be written by Eclipse IDE user.

Slide 112

Slide 112 text

• Modify the Maven Object Model file (pom.xml) (2-1)  Replace all dependencies. (Continued to next slide) org.junit.jupiter junit-jupiter-api 5.13.1 test org.junit.jupiter junit-jupiter-engine 5.13.1 test jakarta.platform jakarta.jakartaee-api ${jakartaee.version} provided io.github.bonigarcia selenium-jupiter 6.1.1 test 112 Exercise for Java Web System Test

Slide 113

Slide 113 text

• Modify the Maven Object Model file (pom.xml) (2-2)  Replace all dependencies. (Continued to next slide) org.seleniumhq.selenium selenium-devtools-v137 4.33.0 test org.seleniumhq.selenium selenium-chrome-driver 4.33.0 test org.seleniumhq.selenium selenium-firefox-driver 4.33.0 test 113 Exercise for Java Web System Test

Slide 114

Slide 114 text

package ${project.artifactId} maven-surefire-plugin 3.5.3 org.apache.maven.plugins maven-failsafe-plugin 3.5.3 114 • Modify the Maven Object Model file (pom.xml) (3-1)  Replace the build element to execute system test during “integration-test” phase. Exercise for Java Web System Test

Slide 115

Slide 115 text

• Modify the Maven Object Model file (pom.xml) (3-2)  Replace the build element to execute system test during “integration-test” phase. perform-it integration-test ${payara.hostname} ${servlet.port} ${project.artifactId} 115 Exercise for Java Web System Test

Slide 116

Slide 116 text

• Modify the Maven Object Model file (pom.xml) (4-1)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. payara7x-remote true org.codehaus.cargo cargo-maven3-plugin 1.10.14 ${skipTests} deploy pre-integration-test redeploy 116 Exercise for Java Web System Test

Slide 117

Slide 117 text

• Modify the Maven Object Model file (pom.xml) (4-2)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. undeploy post-integration-test undeploy org.codehaus.cargo cargo-maven3-plugin payara remote 117 Exercise for Java Web System Test

Slide 118

Slide 118 text

• Modify the Maven Object Model file (pom.xml) (4-3)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. runtime ${payara.username} ${payara.password} ${payara.adminPort} ${payara.hostname} ${servlet.port} 118 Exercise for Java Web System Test

Slide 119

Slide 119 text

• Modify the Maven Object Model file (pom.xml) (4-4)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. fish.payara.extras payara-embedded-web 7.2025.1.Alpha4 119 Exercise for Java Web System Test

Slide 120

Slide 120 text

• Modify the Maven Object Model file (pom.xml) (4-5)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. payara7x-local org.codehaus.cargo cargo-maven3-plugin start-deoloy pre-integration-test start redeploy undeploy-stop post-integration-test undeploy stop 120 Exercise for Java Web System Test

Slide 121

Slide 121 text

• Modify the Maven Object Model file (pom.xml) (4-6)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. org.codehaus.cargo cargo-maven3-plugin payara installed ${payara.home} existing ${payara.domainDir} ${payara.domainName} 121 Exercise for Java Web System Test

Slide 122

Slide 122 text

• Modify the Maven Object Model file (pom.xml) (4-7)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. org.glassfish.main.deployment deployment-client 5.1.0 122 Exercise for Java Web System Test

Slide 123

Slide 123 text

• Procedure of the Exercise Make a Project and edit pom.xml Copy Score and Calculator Java Class and edit Score Make a web interface and JUnit Test Class for it Execut e JUnit from Maven 123 Exercise for Java Web System Test

Slide 124

Slide 124 text

• Copy the Java source files (Score.java and Calculator.java)  Copy “Score.java” and “Calculator.java” from “test1” project and paste to “test2” project.  Delete “JakartaRestConfiguration.java”. Delete Copy & Paste 124 Exercise for Java Web System Test

Slide 125

Slide 125 text

• Modify the Java source file (Score.java) to be accessed from JSF Facelet menus  For the “Score” class, add an annotation of “@Named”.  Add menu field properties and their accessors and change add() method to use the properties. package org.example.test1; import jakarta.inject.Inject; import jakarta.inject.Named; import jakarta.enterprise.context.RequestScoped; @Named @RequestScoped public class Score { @Inject private Calculator calc; private String expA, expB, expSum; public String add() { int sum = calc.evaluate(expA) + calc.evaluate(expB); expSum = Integer.toString(sum); return "helloWorld"; } public String getExpA() { return expA;} public void setExpA(String expA) { this.expA = expA;} public String getExpB() { return expB;} public void setExpB(String expB) { this.expB = expB;} public String getExpSum() { return expSum;} public void setExpSum(String expSum) { this.expSum = expSum;} } 125 Exercise for Java Web System Test

Slide 126

Slide 126 text

• Procedure of the Exercise Make a Project and edit pom.xml Copy Score and Calculator Java Class and edit Score Make a web interface and JUnit Test Class for it Execut e JUnit from Maven 126 Exercise for Java Web System Test

Slide 127

Slide 127 text

• Create Test folder  Right click on “src” folder under “test2” project and navigate to “Folder” dialog screen by selecting “New”  “Folder”.  Fill in “test/java” for “Folder name” field and click “Finish” button.  Right click on “test2” project and click “Refresh”. 127 Exercise for Java Web System Test Refresh

Slide 128

Slide 128 text

• Create a Test Class file for the created Java source file named “ScoreIT.java” (1)  Right click on the created folder of “src/test/java” and navigate to “New” dialog screen by selecting “New”  “Other…”.  Select “Java”  “JUnit”  “JUnit Test Case” and click “Next >” button on “Select a wizard” pane screen. 128 Exercise for Java Web System Test

Slide 129

Slide 129 text

• Create a Test Class file for the created Java source file named “ScoreIT.java” (2)  Fill “org.example.test2” in “Package” field and “ScoreIT” in “Name” field and click “Finish” button on “JUnit Test Case” pane screen. 129 Exercise for Java Web System Test

Slide 130

Slide 130 text

• Modify the Java Test source file (ScoreIT.java)  For the “ScoreIT” class definition, add “@ExtendWith(SeleniumJupiter.class)”annotation.  In the “ScoreIT” class, add a method of “testAdd(ChromeDriver driver)” with “@Test” annotation. @ ExtendWith(SeleniumJupiter.class) public class ScoreIT { @Test public void testAdd(ChromeDriver driver) throws InterruptedException{ String hostname = System.getProperty("servlet.host"); String port = System.getProperty("servlet.port"); String context = System.getProperty("servlet.context"); driver.get("http://" + hostname + ":" + port + "/" + context + "/"); WebElement element = driver.findElement(By.id("expA")); element.sendKeys("1+2+3"); element = driver.findElement(By.id("expB")); element.sendKeys("5+7+9"); element = driver.findElement(By.id("calculate")); element.click(); Thread.sleep(5000); element = driver.findElement(By.id("expSum")); assertEquals("27", element.getText()); } } package org.example.test2; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import io.github.bonigarcia.seljup. SeleniumJupiter; 130 Exercise for Java Web System Test

Slide 131

Slide 131 text

• Modify the Java Test source file (ScoreIT.java) (Optional) In the “ScoreIT” class, add a method of “setup()” with “@BeforeAll” annotation to force to specify a Chrom Driver version and to use Cache in the repository. @ ExtendWith(SeleniumJupiter.class) public class ScoreIT { @BeforeAll static void setup() { // Specify a version of ChromeDriver WebDriverManager.chromedriver().driverVersion ("114.0.5735.90"); } : @Test public void testAdd(ChromeDriver driver) throws InterruptedException{ : } } package org.example.test2; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.extension.ExtendWith; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import io.github.bonigarcia.seljup. SeleniumJupiter; import io.github.bonigarcia.wdm.WebDriverManager; 131 Exercise for Java Web System Test

Slide 132

Slide 132 text

• Modify the Web XML file (web.xml) (1)  Open “web.xml” by selecting “Deployed Resources”  “webapp”  “WEB-INF”  “web.xml”  Change the “” element to use Jakarta Servlet 6.1. 132 Exercise for Java Web System Test

Slide 133

Slide 133 text

• Modify the Web XML file (web.xml) (2) Add element to set “PROJECT STAGE”. Add and elements for the Faces Servlet Configuration. Add element to specify welcome pages. 133 Project stage for the application (new in 2.0). Expects one of the following values: Development, Production, SystemTest, UnitTest jakarta.faces.PROJECT_STAGE Development Faces Servlet jakarta.faces.webapp.FacesServlet 1 Faces Servlet *.xhtml index.html Exercise for Java Web System Test

Slide 134

Slide 134 text

• Modify the JSF Facelet file (helloWorld.xhtml) (1)  Create “helloWorld.xhtml” file entering the following xhtml source code. Calculation Forms

Basic Calculation Form

134 Exercise for Java Web System Test

Slide 135

Slide 135 text

• Modify the JSF Facelet file (helloWorld.xhtml) (2) 135 Exercise for Java Web System Test

Slide 136

Slide 136 text

• Modify “index.html” file to jump to “helloWorld.xhtml” by adding a tag. 136 Start Page

Hello World!

Exercise for Java Web System Test

Slide 137

Slide 137 text

• Modify the version of Dynamic Web Module  Right click on the project of “test2” and select “Properties”.  Select “Project Facets” and then “Dynamic Web Module” and change the “version” to “6.1” and click “Apply and Close” button. 137 Exercise for Java Web System Test

Slide 138

Slide 138 text

• Procedure of the Exercise Make a Project and edit pom.xml Copy Score and Calculator Java Class and edit Score Make a web interface and JUnit Test Class for it Execut e JUnit from Maven 138 Exercise for Java Web System Test

Slide 139

Slide 139 text

• Execute JUnit through M2Eclipse™ for the created Java Test source file (Remote Profile)  Start-up Payara Server.  Right click on “test2” project and navigate to “Edit Configuration” dialog screen by selecting “Run As”  “Maven build…”.  Put “test2 – post-integration-test (Remote)” in “Name” field and “post-integration-test” in “Goals” field and click “Run” button on “Edit configuration and launch” pane screen.  Confirm that test completes successfully in “Console” pane screen. 139 Exercise for Java Web System Test

Slide 140

Slide 140 text

• Execute JUnit through M2Eclipse™ for the created Java Test source file (Local Profile)  Edit $PAYARA_HOME/glassfish/domains/password.properties to change current admin password.  Shutdown Payara Server, if it’s working. Right click on the project and select “Run As”  “5 Maven build…”.  Put “test2 – post-integration-test (Local)” in “Name” field and “post-integration-test” in “Goals” field and “payara7x-local” in “Profile” field and click “Run” button on “Edit configuration and launch” pane screen.  Confirm that test completes successfully in “Console” pane screen. 140 Before proceeding this step, just confirm that $PAYARA_HOME/glassfish/domains/password.pro perties has a correct admin password. Exercise for Java Web System Test

Slide 141

Slide 141 text

Exercise 141

Slide 142

Slide 142 text

Java Unit Test Exercise • Check if the “dateToString” method of “DateUtility” class works correctly public class DateUtility { public String dateToString(LocalDateTime dateTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedString = dateTime.format(formatter); return formattedString; } } DateUtility.java Target Class 142

Slide 143

Slide 143 text

Java Unit Test with Mock Exercise • Check if the “printToday” method of “DateForm” class works correctly by mocking “DateUtility” class public class DateUtility { public String dateToString(LocalDateTime dateTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedString = dateTime.format(formatter); return formattedString; } } DateUtility.java public class DateForm { private DateUtility du; public String printToday() { LocalDateTime now = LocalDateTime.now(); String dateString = du.dateToString(now); return dateString; } } DateForm.java Target Class Mocked Class 143

Slide 144

Slide 144 text

Java CDI Integration Test Exercise • Check if the “printToday” method of “DateForm” CDI class works correctly along with “DateUtility” CDI class @RequestScoped public class DateUtility { public String dateToString(LocalDateTime dateTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedString = dateTime.format(formatter); return formattedString; } } DateUtility.java @RequestScoped public class DateForm { @Inject private DateUtility du; public String printToday() { LocalDateTime now = LocalDateTime.now(); String dateString = du.dateToString(now); return dateString; } } DateForm.java Injected by CDI Target Class Injected Class 144