Slide 1

Slide 1 text

TEST TOOL (VER. 3.1) 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. 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 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 Windows 10™ + Linux (Rocky Linux™) Total EWA Development Exercise Jakarta Batch Java SE (Oracle JDK™/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/OpenJDK (Version: 11) • Eclipse IDE for Enterprise Java Developers (Version: 2021-09 (4.21.0)) • Payara Server (Version: 6.2021.1.Alpha1) – 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™) TestEngineAPI 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([messag e,] 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 JUnit5Test 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 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 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

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 @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 (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 Integration 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™ Project src main test target pom.xml Package Folders xxxTest.java “maven-surefire-plugin” fetches these classes as test class. 37

Slide 38

Slide 38 text

Java Unit Test • pom.xml for JUnit Test Application (1) ... maven-surefire-plugin 2.22.2 ... 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.8.2 test ... ... org.junit.jupiter junit-jupiter-api 5.8.2 test org.junit.jupiter junit-jupiter-engine 5.8.2 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 2.2 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 4.1.0 test org.mockito mockito-junit-jupiter 4.1.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 3.0.0.Final test org.jboss.weld.se weld-se-core 4.0.2.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 Integration 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™ Project src main test target pom.xml Package Directories xxxIT.java “maven-failsafe-plugin” fetches these classes as integration test class. 44

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 4.0.1 test org.seleniumhq.selenium selenium-devtools-v96 4.1.0 test ... Including Selenium-Jupiter functionalities 45 ... org.seleniumhq.selenium selenium-chrome-driver 4.1.0 test org.seleniumhq.selenium selenium-firefox-driver 4.1.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)

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

Exercise to prepare for Java Test Environment • Create a new Eclipse™ workspace and configure “Installed JREs” for the workspace 51 Eclipse™ Workspace C:\Users\\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.

Slide 52

Slide 52 text

Exercise to prepare for Java Test Environment • 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. C:\Users\\eclipse-workspace\ws3 52

Slide 53

Slide 53 text

• (Option) Configure Installed JRE (1) [If JDK11 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

Slide 54

Slide 54 text

• (Option) Configure Installed JRE (2) [If JDK11 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 JDK11 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

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.xml Make the Calculator Java Class Make a JUnit Test Class for the Calculator Execute JUnit from Eclipse Execute JUnit from Maven 57

Slide 58

Slide 58 text

Exercise for Java Unit Test • Procedure of the Exercise Make a Project and edit pom.xml Make the Calculator Java Class Make a JUnit Test Class for the Calculator Execute JUnit from Eclipse Execute 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.4” 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 “11”.  Change the version of “maven-surefire-plugin” to “2.22.2” and delete other plug-ins. maven-surefire-plugin 2.22.2 UTF-8 11 11 62 UTF-8 1.7 1.7

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 2.2 test 63 org.junit.jupiter junit-jupiter-api 5.8.2 test org.junit.jupiter junit-jupiter-engine 5.8.2 test

Slide 64

Slide 64 text

Java Unit Test Exercise • Procedure of the Exercise Make a Project and edit pom.xml Make the Calculator Java Class Make a JUnit Test Class for the Calculator Execute JUnit from Eclipse Execute JUnit from Maven 64

Slide 65

Slide 65 text

Java Unit Test Exercise • 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

Slide 66

Slide 66 text

Java Unit Test Exercise • 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

Slide 67

Slide 67 text

Java Unit Test Exercise • Procedure of the Exercise Make a Project and edit pom.xml Make the Calculator Java Class Make a JUnit Test Class for the Calculator Execute JUnit from Eclipse Execute JUnit from Maven 67

Slide 68

Slide 68 text

Java Unit Test Exercise • 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

Slide 69

Slide 69 text

Java Unit Test Exercise • 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

Slide 70

Slide 70 text

Java Unit Test Exercise • 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

Slide 71

Slide 71 text

Java Unit Test Exercise • Update Apache Maven™ Project to remove a warning icon on the project. Confirm that there is a warning icon for “test1” project in Project Explorer. Right click on “test1” project and select “Maven”  “Update Project …”.  Select “test1” from Maven Codebases and click “OK” button on “Update Maven Project” pane screen. 71 Warning Icon Warning Icon is disappeared.

Slide 72

Slide 72 text

Java Unit Test Exercise • Procedure of the Exercise Make a Project and edit pom.xml Make the Calculator Java Class Make a JUnit Test Class for the Calculator Execute JUnit from Eclipse Execute JUnit from Maven 72

Slide 73

Slide 73 text

Java Unit Test Exercise • 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. 73

Slide 74

Slide 74 text

Java Unit Test Exercise • 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. 74 Error Dialog

Slide 75

Slide 75 text

Java Unit Test Exercise • Procedure of the Exercise Make a Project and edit pom.xml Make the Calculator Java Class Make a JUnit Test Class for the Calculator Execute JUnit from Eclipse Execute JUnit from Maven 75

Slide 76

Slide 76 text

Java Unit Test Exercise • 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. 76

Slide 77

Slide 77 text

Java Unit Test with Mock Exercise • 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 Target Class Mocked Class 77 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 78

Slide 78 text

Java Unit Test with Mock Exercise • Procedure of the Exercise Edit pom.xml Make the Score Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 78

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

Java Unit Test with Mock Exercise • Procedure of the Exercise Edit pom.xml Make the Score Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 80

Slide 81

Slide 81 text

Java Unit Test with Mock Exercise • 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. 81

Slide 82

Slide 82 text

Java Unit Test with Mock Exercise • 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; } } 82

Slide 83

Slide 83 text

Java Unit Test with Mock Exercise • Procedure of the Exercise Edit pom.xml Make the Score Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 83

Slide 84

Slide 84 text

Java Unit Test with Mock Exercise • 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. 84

Slide 85

Slide 85 text

Java Unit Test with Mock Exercise • 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 “Junit Test Case” pane screen. 85

Slide 86

Slide 86 text

Java Unit Test with Mock Exercise • 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); } } 86

Slide 87

Slide 87 text

Java Unit Test with Mock Exercise • Procedure of the Exercise Edit pom.xml Make the Score Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 87

Slide 88

Slide 88 text

Java Unit Test with Mock Exercise • 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. 88

Slide 89

Slide 89 text

Java Unit Test with Mock Exercise • Procedure of the Exercise Edit pom.xml Make the Score Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 89

Slide 90

Slide 90 text

Java Unit Test with Mock Exercise • 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. 90

Slide 91

Slide 91 text

Java CDI Integration Test Exercise • 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 Target Class Injected Class 91 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 92

Slide 92 text

Java CDI Integration Test Exercise • Procedure of the Exercise Edit pom.xml Edit the Score and Calculator Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 92

Slide 93

Slide 93 text

Java CDI Integration Test Exercise • 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 3.0.0.Final test org.jboss.weld.se weld-se-core 4.0.2.Final : 93

Slide 94

Slide 94 text

Java CDI Integration Test Exercise • Procedure of the Exercise Edit pom.xml Edit the Score and Calculator Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 94

Slide 95

Slide 95 text

Java CDI Integration Test Exercise • 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 for DI. 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; } } 95

Slide 96

Slide 96 text

Java CDI Integration Test Exercise • 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; } } 96

Slide 97

Slide 97 text

Java CDI Integration Test Exercise • Procedure of the Exercise Edit pom.xml Edit the Score and Calculator Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 97

Slide 98

Slide 98 text

Java CDI Integration Test Exercise • 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 98

Slide 99

Slide 99 text

Java CDI Integration Test Exercise • 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. 99

Slide 100

Slide 100 text

Java CDI Integration Test Exercise • 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); } } 100

Slide 101

Slide 101 text

Java CDI Integration Test Exercise • Procedure of the Exercise Edit pom.xml Edit the Score and Calculator Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 101

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

Java CDI Integration Test Exercise • Procedure of the Exercise Edit pom.xml Edit the Score and Calculator Java Class Make a JUnit Test Class for the Score Execute JUnit from Eclipse Execute JUnit from Maven 103

Slide 104

Slide 104 text

Java CDI Integration Test Exercise • 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. 104

Slide 105

Slide 105 text

Java Web System Test Exercise • 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) Bind Target Web Menu 105 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 106

Slide 106 text

Java Web System Test Exercise • 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 Execute JUnit from Maven 106

Slide 107

Slide 107 text

Java Unit Test Exercise • 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. 107

Slide 108

Slide 108 text

Java Unit Test Exercise • 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 “myfaces-archetype-codi-jsf20” in Filter field and select “org.apache.myfaces.buildtools myfaces-archetype-codi-jsf20 1.0.4” and click “Next >” button on “Select an Archetype” pane screen. 108

Slide 109

Slide 109 text

Java Unit Test Exercise • 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. 109

Slide 110

Slide 110 text

Java Web System Test Exercise • Modify the Maven Object Model file (pom.xml) (1)  Replace all parameters to access Payara Server™ which accommodates the target war file. UTF-8 11 11 8080 4848 admin xxxxxxxx localhost domain1 C:\payara6 ${payara.home}/glassfish/domains 110

Slide 111

Slide 111 text

Java Web System Test Exercise • Modify the Maven Object Model file (pom.xml) (2)  Replace the build element to execute system test during “integration-test” phase. package ${project.artifactId} maven-surefire-plugin 2.22.2 org.apache.maven.plugins maven-failsafe-plugin 2.22.2 perform-it integration-test ${payara.hostname} ${servlet.port} ${project.artifactId} 111

Slide 112

Slide 112 text

Java Web System Test Exercise • Modify the Maven Object Model file (3) (pom.xml)  Replace all dependencies. (Continued to next slide) org.junit.jupiter junit-jupiter-api 5.8.2 test org.junit.jupiter junit-jupiter-engine 5.8.2 test org.hamcrest hamcrest-core 2.2 test jakarta.platform jakarta.jakartaee-api 9.1.0 provided : 112

Slide 113

Slide 113 text

: io.github.bonigarcia selenium-jupiter 4.0.1 test org.seleniumhq.selenium selenium-devtools-v96 4.1.0 test Java Web System Test Exercise • Modify the Maven Object Model file (4) (pom.xml)  Replace all dependencies. org.seleniumhq.selenium selenium-chrome-driver 4.1.0 test org.seleniumhq.selenium selenium-firefox-driver 4.1.0 test 113

Slide 114

Slide 114 text

Java Web System Test Exercise • Modify the Maven Object Model file (pom.xml) (5)  Replace all profiles to start, stop Payara Server and deploy and undeploy war file to it. Profile description part will be provided by teacher 114

Slide 115

Slide 115 text

Java Web System Test Exercise • 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 Execute JUnit from Maven 115

Slide 116

Slide 116 text

Java Web System Test Exercise • 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 “GreetingService.java” and “HelloWorldController.java”.  Update the Apache Maven™ Configuration of the “test2” project. (Hint: Right click on “test2” project and select “Maven” and click “Update project …”.) Delete Copy & Paste 116 Update Maven

Slide 117

Slide 117 text

Java Web System Test Exercise • 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;} } 117

Slide 118

Slide 118 text

Java Web System Test Exercise • 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 Execute JUnit from Maven 118

Slide 119

Slide 119 text

Java Web System Test Exercise • 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. 119

Slide 120

Slide 120 text

Java Web System Test Exercise • 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. 120

Slide 121

Slide 121 text

Java Web System Test Exercise • 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. 121

Slide 122

Slide 122 text

Java Web System Test Exercise • 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; 122

Slide 123

Slide 123 text

Java Web System Test Exercise • 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 ("96.0.4664.93"); } : @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; 123

Slide 124

Slide 124 text

Java Web System Test Exercise • 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 5.0. 124

Slide 125

Slide 125 text

Java Web System Test Exercise • Modify the Web XML file (web.xml) (2) Rename Application Configuration Parameters starting “javax.faces….” to “jakarta.faces….”. 125 Project stage for the application (new in 2.0). Expects one of the following values: Development, Production, SystemTest, UnitTest jakarta.faces.PROJECT_STAGE Development If this parameter is set to true and the submitted value of a component is the empty string, the submitted value will be set to null jakarta.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL true Define the state method to be used. There are two different options defined by the specification: 'client' and 'server' state. jakarta.faces.STATE_SAVING_METHOD server

Slide 126

Slide 126 text

Java Web System Test Exercise • Modify the Web XML file (web.xml) (3) Delete all the Application Configuration Parameters for Apache MyFaces. Delete All 126 Only applicable if state saving method is "server" (= default). Defines the amount (default = 20) of the latest views are stored in session. org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION 20 Only applicable if state saving method is "server" (= default). If true (default) the state will be serialized to a byte stream before it is written to the session. If false the state will not be serialized to a byte stream. org.apache.myfaces.SERIALIZE_STATE_IN_SESSION false Only applicable if state saving method is "server" (= default) and if org.apache.myfaces.SERIALIZE_STATE_IN_SESSION is true (= default) If true (default) the serialized state will be compressed before it is written to the session. If false the state will not be compressed. org.apache.myfaces.COMPRESS_STATE_IN_SESSION false Defines which packages to scan for beans, separated by commas. Useful for when using maven and jetty:run (version 6) or tomcat:run org.apache.myfaces.annotation.SCAN_PACKAGES org.example.test2

Slide 127

Slide 127 text

Java Web System Test Exercise • Modify the Web XML file (web.xml) (4) Delete “Listener” element which uses Apache OpenWebBeans. Delete 127 org.apache.webbeans.servlet.WebBeansConfigurationListener

Slide 128

Slide 128 text

Java Web System Test Exercise • Modify the Web XML file (web.xml) (5) Rename the Faces Servlet class name of “javax.faces….” to “jakarta.faces….”. 128 Faces Servlet jakarta.faces.webapp.FacesServlet 1

Slide 129

Slide 129 text

Java Web System Test Exercise • Modify the JSF Facelet file (helloWorld.xhtml)  Open “Deployed Resources”  “webapp”  “helloWorld.xhtml”  Change “” element of the Facelet file : : 129

Slide 130

Slide 130 text

Java Web System Test Exercise • 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 “5.0”. 130

Slide 131

Slide 131 text

Java Web System Test Exercise • 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 Execute JUnit from Maven 131

Slide 132

Slide 132 text

Java Web System Exercise • 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. 132

Slide 133

Slide 133 text

Java Web System Exercise • Execute JUnit through M2Eclipse™ for the created Java Test source file (Local Profile)  Edit C:\payara5\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 “payara6x-local” in “Profile” field and click “Run” button on “Edit configuration and launch” pane screen.  Confirm that test completes successfully in “Console” pane screen. 133 Before proceeding this step, just confirm that $PAYARA_HOME/glassfish/domains/password .properties has a correct admin password.

Slide 134

Slide 134 text

Exercise 134

Slide 135

Slide 135 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 135

Slide 136

Slide 136 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 ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC")); 136

Slide 137

Slide 137 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 Target Class Injected Class 137