JavaFX8 TestFX - CDI

163
20.10.2015 JavaFX8 - TestFX - CDI - JUG http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 2/164 Sven Ruppert has been coding java since 1996 Fellow / Senior Manager reply Group Germany - Munich @SvenRuppert @SvenRuppert 3/165

Transcript of JavaFX8 TestFX - CDI

Page 1: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 2/164

Sven Rupperthas been coding java since 1996

Fellow / Senior Manager

reply Group

Germany - Munich

@SvenRuppert

@SvenRuppert 3/165

Page 2: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 3/164

JavaFX 8 - JumpStartIntro

Swing was yesterday.... now JavaFX ;-)

@SvenRuppert 4/165

Page 3: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 4/164

JavaFX 8 - JumpStartIntro

Swing was yesterday.... now JavaFX ;-)

Since Java8...

.. JavaFX8 is part of the JDK

@SvenRuppert 5/165

Page 4: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 5/164

JavaFX 8 - JumpStartIntro

Swing was yesterday.... now JavaFX ;-)

Since Java8...

.. JavaFX8 is part of the JDK

.. you could use it with JDK7, but you have to add it by yourself.

@SvenRuppert 6/165

Page 5: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 6/164

JavaFX 8 - JumpStartIntro

Swing was yesterday.... now JavaFX ;-)

Since Java8...

.. JavaFX8 is part of the JDK

.. you could use it with JDK7, but you have to add it by yourself.

BUT: The switch from JavaFX 2.2 to JavaFX8 could be a lot of work..

@SvenRuppert 7/165

Page 6: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 7/164

JavaFX 8 - JumpStartIntro

Swing was yesterday.... now JavaFX ;-)

Since Java8...

.. JavaFX8 is part of the JDK

.. you could use it with JDK7, but you have to add it by yourself.

BUT: The switch from JavaFX 2.2 to JavaFX8 could be a lot of work..

for example: Builder-API is now deprecated and a lot of the constructors arechanged.

@SvenRuppert 8/165

Page 7: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 8/164

JavaFX 8 - JumpStartHello World - only to remember

a simple example with a button and a service...

@SvenRuppert 9/165

Page 8: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 9/164

JavaFX 8 - JumpStartHello World - only to remember

a simple example with a button and a service...

different ways to implement..

@SvenRuppert 10/165

Page 9: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 10/164

JavaFX 8 - JumpStartHello World - only to remember

a simple example with a button and a service...

different ways to implement..

.. lets start without fxml

@SvenRuppert 11/165

Page 10: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 11/164

JavaFX 8 - JumpStartHello World - only to remember

@SvenRuppert

public class JavaFXPlain extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { final AnchorPane rootNode = new AnchorPane(); HBox hBox = new HBox(); final TextField tf1 = new TextField(), tf2 = new TextField(), tfResult = new TextField(); final Button button = new Button(); button.setText("Ohhh press me.."); button.setOnAction(event -> { String v1 = tf1.getText(); String v2 = tf2.getText(); int result = valueOf(v1) + valueOf(v2); tfResult.setText(result+""); }); hBox.getChildren().addAll(tf1, tf2, button, tfResult); rootNode.getChildren().add(hBox); final Scene scene = new Scene(rootNode, 640, 60); primaryStage.setScene(scene); primaryStage.show(); }}

JAVA

12/165

Page 11: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 12/164

JavaFX 8 - JumpStartHello World - only to remember

Application:The main class from an JavaFX Application will extendjavafx.application.Application.

@SvenRuppert 13/165

Page 12: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 13/164

JavaFX 8 - JumpStartHello World - only to remember

Application:The main class from an JavaFX Application will extendjavafx.application.Application.

Method: startThe start() Method will be the main start-point of the application.

@SvenRuppert 14/165

Page 13: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 14/164

JavaFX 8 - JumpStartHello World - only to remember

Application:The main class from an JavaFX Application will extendjavafx.application.Application.

Method: startThe start() Method will be the main start-point of the application.

Stage: This instance is the top level container for all JavaFX Components

@SvenRuppert 15/165

Page 14: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 15/164

JavaFX 8 - JumpStartHello World - only to remember

Application:The main class from an JavaFX Application will extendjavafx.application.Application.

Method: startThe start() Method will be the main start-point of the application.

Stage: This instance is the top level container for all JavaFX Components

Scene: This instance will be the container for the your content.

@SvenRuppert 16/165

Page 15: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 16/164

JavaFX 8 - JumpStartHello World - only to remember

@SvenRuppert

public class JavaFXPlain extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { final AnchorPane rootNode = new AnchorPane(); HBox hBox = new HBox(); final TextField tf1 = new TextField(), tf2 = new TextField(), tfResult = new TextField(); final Button button = new Button(); button.setText("Ohhh press me.."); button.setOnAction(event -> { String v1 = tf1.getText(); String v2 = tf2.getText(); int result = valueOf(v1) + valueOf(v2); tfResult.setText(result+""); }); hBox.getChildren().addAll(tf1, tf2, button, tfResult); rootNode.getChildren().add(hBox); final Scene scene = new Scene(rootNode, 640, 60); primaryStage.setScene(scene); primaryStage.show(); }}

JAVA

17/165

Page 16: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 17/164

JavaFX 8 - JumpStartHello World - only to remember

oh now..... how to test it ???

@SvenRuppert 18/165

Page 17: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 18/164

JavaFX 8 - JumpStartHello World - only to remember

oh now..... how to test it ???

we will start with jUnit....

@SvenRuppert 19/165

Page 18: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 19/164

JavaFX 8 - JumpStartHello World - only to remember

oh now..... how to test it ???

we will start with jUnit....

.. so we have to extract the logic

@SvenRuppert 20/165

Page 19: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 20/164

JavaFX 8 - JumpStartHello World - only to remember

oh now..... how to test it ???

we will start with jUnit....

.. so we have to extract the logic

@SvenRuppert

public class AddService { public int add(int a, int b){ return a +b; }}

JAVA

21/165

Page 20: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 21/164

JavaFX 8 - JumpStartHello World - only to remember

@SvenRuppert

public class AddService { public int add(int a, int b){ return a +b; }}@Test public void testAdd001() throws Exception { AddService service = new AddService(); int r = service.add(0, 0); Assert.assertEquals(0, r);}

JAVA

22/165

Page 21: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 22/164

JavaFX 8 - JumpStartHello World - only to remember

@SvenRuppert

public class AddService { public int add(int a, int b){ return a +b; }}@Test public void testAdd001() throws Exception { AddService service = new AddService(); int r = service.add(0, 0); Assert.assertEquals(0, r);}public class String2Int { public int convert(String s){ return Integer.valueOf(s); }}

JAVA

23/165

Page 22: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 23/164

JavaFX 8 - JumpStartHello World - only to remember

@SvenRuppert

public class AddService { public int add(int a, int b){ return a +b; }}@Test public void testAdd001() throws Exception { AddService service = new AddService(); int r = service.add(0, 0); Assert.assertEquals(0, r);}public class String2Int { public int convert(String s){ return Integer.valueOf(s); }}@Test public void test001() throws Exception { String2Int s = new String2Int(); int r = s.convert("0"); Assert.assertEquals(r, 0);}

JAVA

24/165

Page 23: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 24/164

JavaFX 8 - JumpStartHello World - only to remember

What is reached?

@SvenRuppert 25/165

Page 24: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 25/164

JavaFX 8 - JumpStartHello World - only to remember

What is reached?

.. UI with less logic

@SvenRuppert 26/165

Page 25: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 26/164

JavaFX 8 - JumpStartHello World - only to remember

What is reached?

.. UI with less logic

.. we could test the logic with jUnit

@SvenRuppert 27/165

Page 26: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 27/164

JavaFX 8 - JumpStartHello World - only to remember

What is reached?

.. UI with less logic

.. we could test the logic with jUnit

.. we increased the code coverage

@SvenRuppert 28/165

Page 27: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 28/164

JavaFX 8 - JumpStartHello World - only to remember

What is reached?

.. UI with less logic

.. we could test the logic with jUnit

.. we increased the code coverage

but how much?

@SvenRuppert 29/165

Page 28: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 29/164

JavaFX 8 - JumpStartHello World - only to remember

@SvenRuppert

private AddService addService = new AddService();private String2Int string2Int = new String2Int();

public void start(Stage primaryStage) { final AnchorPane rootNode = new AnchorPane(); HBox hBox = new HBox(); final TextField tf1 = new TextField(), tf2 = new TextField(), tfResult = new TextField(); final Button button = new Button(); button.setText("Ohhh press me.."); button.setOnAction(event -> { String v1 = tf1.getText(); String v2 = tf2.getText(); int intA = string2Int.convert(v1); int intB = string2Int.convert(v2); int result = addService.add(intA, intB); tfResult.setText(result+""); }); hBox.getChildren().addAll(tf1, tf2, button, tfResult); rootNode.getChildren().add(hBox); final Scene scene = new Scene(rootNode, 640, 60); primaryStage.setScene(scene); primaryStage.show();}

JAVA

30/165

Page 29: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 30/164

JavaFX 8 - JumpStartHello World - only to remember

performance is part of TDD

@SvenRuppert 31/165

Page 30: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 31/164

JavaFX 8 - JumpStartHello World - only to remember

performance is part of TDD

.. how fast are the services?

@SvenRuppert 32/165

Page 31: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 32/164

JavaFX 8 - JumpStartHello World - only to remember

performance is part of TDD

.. how fast are the services?

.. start with microbenchmarks

@SvenRuppert 33/165

Page 32: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 33/164

JavaFX 8 - JumpStartHello World - only to remember

performance is part of TDD

.. how fast are the services?

.. start with microbenchmarks

.. use JMH -> http://openjdk.java.net/projects/code-tools/jmh/

@SvenRuppert 34/165

Page 33: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 34/164

JavaFX 8 - JumpStartHello World - only to remember

performance is part of TDD

.. how fast are the services?

.. start with microbenchmarks

.. use JMH -> http://openjdk.java.net/projects/code-tools/jmh/

show the code v003 ;-)

@SvenRuppert 35/165

Page 34: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 35/164

JavaFX 8 - TestFX

testing - Basics : plain jUnit

@SvenRuppert 36/165

Page 35: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 36/164

JavaFX 8 - TestFX

testing - Basics : plain jUnit

testing - Frameworks

@SvenRuppert 37/165

Page 36: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 37/164

JavaFX 8 - TestFX

testing - Basics : plain jUnit

testing - Frameworks

testing - (C)DI

@SvenRuppert 38/165

Page 37: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 38/164

JavaFX 8 - TestFX

integration tests - including UI ?

@SvenRuppert 39/165

Page 38: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 39/164

JavaFX 8 - TestFX

integration tests - including UI ?

but....

@SvenRuppert 40/165

Page 39: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 40/164

JavaFX 8 - TestFX

integration tests - including UI ?

but....

unit tests - how to test a UI component

@SvenRuppert 41/165

Page 40: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 41/164

JavaFX 8 - TestFX

integration tests - including UI ?

but....

unit tests - how to test a UI component

system tests - how to test an UI workflow

@SvenRuppert 42/165

Page 41: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 42/164

JavaFX 8 - TestFXmanual testing

a tester tests the complete app

@SvenRuppert 43/165

Page 42: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 43/164

JavaFX 8 - TestFXmanual testing

a tester tests the complete app

create a test plan

@SvenRuppert 44/165

Page 43: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 44/164

JavaFX 8 - TestFXmanual testing

a tester tests the complete app

create a test plan

update the test plan for each release

@SvenRuppert 45/165

Page 44: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 45/164

JavaFX 8 - TestFXmanual testing

a tester tests the complete app

create a test plan

update the test plan for each release

test each release

@SvenRuppert 46/165

Page 45: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 46/164

JavaFX 8 - TestFXCI / CD

update the test plan for each commit

@SvenRuppert 47/165

Page 46: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 47/164

JavaFX 8 - TestFXCI / CD

update the test plan for each commit

test each commit

@SvenRuppert 48/165

Page 47: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 48/164

JavaFX 8 - TestFXIDE based Tools like Selenium

QF-Test

@SvenRuppert 49/165

Page 48: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 49/164

JavaFX 8 - TestFXIDE based Tools like Selenium

QF-Test

commercial product

@SvenRuppert 50/165

Page 49: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 50/164

JavaFX 8 - TestFXIDE based Tools like Selenium

QF-Test

commercial product

developer licence costs around 1995 €

@SvenRuppert 51/165

Page 50: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 51/164

JavaFX 8 - TestFXIDE based Tools like Selenium

QF-Test

commercial product

developer licence costs around 1995 €

no JUnit approach

@SvenRuppert 52/165

Page 51: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 52/164

JavaFX 8 - TestFXIDE based Tools like Selenium

QF-Test

commercial product

developer licence costs around 1995 €

no JUnit approach

CI integration

@SvenRuppert 53/165

Page 52: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 53/164

JavaFX 8 - TestFXIDE based Tools like Selenium

QF-Test

commercial product

developer licence costs around 1995 €

no JUnit approach

CI integration

nearly the same as froglogic...

@SvenRuppert 54/165

Page 53: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 54/164

JavaFX 8 - TestFXJemmyFX

is for JavaFX 2.2

last commit is over 2 yearsago

looks like there is no development activity

@SvenRuppert 55/165

Page 54: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 55/164

JavaFX 8 - TestFXJemmyFX

is for JavaFX 2.2

last commit is over 2 yearsago

looks like there is no development activity

Don´t use it

@SvenRuppert 56/165

Page 55: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 56/164

JavaFX 8 - TestFXMarvinFX

https://github.com/guigarage/MarvinFX

Provides Supervisors for JavaFX Properties

@SvenRuppert 57/165

Page 56: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 57/164

JavaFX 8 - TestFXMarvinFX

https://github.com/guigarage/MarvinFX

Provides Supervisors for JavaFX Properties

we (Hendrik Ebbers and Sven) are merging into TestFX

@SvenRuppert 58/165

Page 57: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 58/164

JavaFX 8 - TestFXMarvinFX

https://github.com/guigarage/MarvinFX

Provides Supervisors for JavaFX Properties

we (Hendrik Ebbers and Sven) are merging into TestFX

Don´t use it

@SvenRuppert 59/165

Page 58: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 59/164

JavaFX 8 - TestFXMarvinFX

define

@SvenRuppert

PropertySupervisor<String> textSupervisor = new PropertySupervisor<>(textfield.textProperty());

JAVA

60/165

Page 59: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 60/164

JavaFX 8 - TestFXMarvinFX

rules

@SvenRuppert

textPropertySupervisor.assertWillChange();textPropertySupervisor.assertWillChangeByDefinedCount(3);textPropertySupervisor.assertWillChangeThisWay("A", "B", "C");

JAVA

61/165

Page 60: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 61/164

JavaFX 8 - TestFXMarvinFX

interaction

//interact with UI by using TestFX

@SvenRuppert 62/165

Page 61: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 62/164

JavaFX 8 - TestFXMarvinFX

check

@SvenRuppert

textPropertySupervisor.confirm(); JAVA

63/165

Page 62: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 63/164

JavaFX 8 - TestFXfinally TestFX

active development

LTS branch for Java7 is available

active branch JavaFX8 only

@SvenRuppert 64/165

Page 63: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 64/164

JavaFX 8 - TestFXfinally TestFX

verifying the behavior of JavaFX applications

@SvenRuppert 65/165

Page 64: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 65/164

JavaFX 8 - TestFXfinally TestFX

verifying the behavior of JavaFX applications

API for interacting with JavaFX applications.

@SvenRuppert 66/165

Page 65: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 66/164

JavaFX 8 - TestFXfinally TestFX

verifying the behavior of JavaFX applications

API for interacting with JavaFX applications.

fluent and clean API

@SvenRuppert 67/165

Page 66: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 67/164

JavaFX 8 - TestFXfinally TestFX

verifying the behavior of JavaFX applications

API for interacting with JavaFX applications.

fluent and clean API

Supports Hamcrest Matchers and Lambda expressions.

@SvenRuppert 68/165

Page 67: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 68/164

JavaFX 8 - TestFXfinally TestFX

verifying the behavior of JavaFX applications

API for interacting with JavaFX applications.

fluent and clean API

Supports Hamcrest Matchers and Lambda expressions.

Screenshots of failed tests.

@SvenRuppert 69/165

Page 68: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 69/164

JavaFX 8 - TestFXfinally TestFX

verifying the behavior of JavaFX applications

API for interacting with JavaFX applications.

fluent and clean API

Supports Hamcrest Matchers and Lambda expressions.

Screenshots of failed tests.

internal used by Oracle for JavaFX

@SvenRuppert 70/165

Page 69: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 70/164

JavaFX 8 - TestFXfinally TestFX

start with a Login Screen.

@SvenRuppert 71/165

Page 70: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 71/164

JavaFX 8 - TestFXfinally TestFX

start with a Login Screen.

... TextField / Password-Field / Button

@SvenRuppert 72/165

Page 71: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 72/164

JavaFX 8 - TestFXfinally TestFX

start with a Login Screen.

... TextField / Password-Field / Button

@SvenRuppert

click(".text-field").type("steve");click(".password-field").type("duke4ever");click(".button:default");assertNodeExists( ".dialog" );

JAVA

73/165

Page 72: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 73/164

JavaFX 8 - TestFXfinally TestFX

each test must extend the GUITest class

@SvenRuppert

public class MyTest extends GuiTest {

@Test public void testLogin() { . . . }

}

JAVA

74/165

Page 73: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 74/164

JavaFX 8 - TestFXfinally TestFX

each test must extend the GUITest class

.. and provide the the root node

@SvenRuppert

public class MyTest extends GuiTest { protected Node getRootNode(){..}

@Test public void testLogin() { . . . }

}

JAVA

75/165

Page 74: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 75/164

JavaFX 8 - TestFXfinally TestFX

The GuiTest class provides a lot of functions that can be used to interact withJavaFX

.. and provide the the root node

@SvenRuppert

public class MyTest extends GuiTest { protected Node getRootNode(){..}

@Test public void testLogin() { click(".text-field"); type("steve"); //... }

}

JAVA

76/165

Page 75: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 76/164

JavaFX 8 - TestFXfinally TestFX

ok, how we would do it ?

@SvenRuppert 77/165

Page 76: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 77/164

JavaFX 8 - TestFXfinally TestFX

ok, how we would do it ?

..extract a component

@SvenRuppert 78/165

Page 77: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 78/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class AddComponent extends HBox { private Controller controller = new Controller(); final private TextField tf1 = new TextField(); final private TextField tf2 = new TextField(); final private TextField tfResult = new TextField(); final Button button = new Button(); { tf1.setId("tf1"); tf2.setId("tf2"); tfResult.setId("tfResult"); button.setId("btnAdd"); button.setText("Ohhh press me.."); button.setOnAction(event -> { String v1 = tf1.getText(); String v2 = tf2.getText(); String result = controller.add(v1, v2); tfResult.setText(result); }); this.getChildren().addAll(tf1, tf2, button, tfResult); }}

JAVA

79/165

Page 78: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 79/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class JavaFXPlain extends Application { public static void main(String[] args) { launch(args); }

@Override public void start(Stage primaryStage) { final AnchorPane rootNode = new AnchorPane(); rootNode.getChildren().add(new AddComponent()); final Scene scene = new Scene(rootNode, 640, 60); primaryStage.setScene(scene); primaryStage.show(); }

}

JAVA

80/165

Page 79: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 80/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

@SvenRuppert 81/165

Page 80: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 81/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

the main class is nearly only pure JDK/JavaFX8

@SvenRuppert 82/165

Page 81: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 82/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

the main class is nearly only pure JDK/JavaFX8

the component AddComponent needs to be tested at the Button Action Event

@SvenRuppert 83/165

Page 82: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 83/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

the main class is nearly only pure JDK/JavaFX8

the component AddComponent needs to be tested at the Button Action Event

if the Button Action Event is tested -> code coverage is increased

@SvenRuppert 84/165

Page 83: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 84/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

the main class is nearly only pure JDK/JavaFX8

the component AddComponent needs to be tested at the Button Action Event

if the Button Action Event is tested -> code coverage is increased

.. extend from GUITest

@SvenRuppert 85/165

Page 84: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 85/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

the main class is nearly only pure JDK/JavaFX8

the component AddComponent needs to be tested at the Button Action Event

if the Button Action Event is tested -> code coverage is increased

.. extend from GUITest

.. create RootNode

@SvenRuppert 86/165

Page 85: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 86/164

JavaFX 8 - TestFXfinally TestFX

Controller is tested !

the main class is nearly only pure JDK/JavaFX8

the component AddComponent needs to be tested at the Button Action Event

if the Button Action Event is tested -> code coverage is increased

.. extend from GUITest

.. create RootNode

.. write your tests

@SvenRuppert 87/165

Page 86: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 87/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class AddComponentTest extends GuiTest {

@Override protected Parent getRootNode() { AddComponent addComponent = new AddComponent(); addComponent.setId("c"); return addComponent; }

JAVA

88/165

Page 87: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 88/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

@Test public void test001() throws Exception { Node c = find("#c"); Assert.assertNotNull(c); TextField tf1 = find("#tf1", c); TextField tf2 = find("#tf2", c); clickOn(tf1).write("1"); clickOn(tf2).write("2"); clickOn((Button)find("#btnAdd")).clickOn(); TextField tfResult = find("#tfResult"); Assertions.verifyThat(tfResult, hasText("3")); Thread.sleep(5_000); }

JAVA

89/165

Page 88: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 89/164

JavaFX 8 - TestFXfinally TestFX

.. and now code and running test -> v004

@SvenRuppert

@Test public void test001() throws Exception { Node c = find("#c"); Assert.assertNotNull(c); TextField tf1 = find("#tf1", c); TextField tf2 = find("#tf2", c); clickOn(tf1).write("1"); clickOn(tf2).write("2"); clickOn((Button)find("#btnAdd")).clickOn(); TextField tfResult = find("#tfResult"); Assertions.verifyThat(tfResult, hasText("3")); Thread.sleep(5_000); }

JAVA

90/165

Page 89: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 90/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

@SvenRuppert 91/165

Page 90: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 91/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

@SvenRuppert 92/165

Page 91: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 92/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

.. but how ?

@SvenRuppert 93/165

Page 92: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 93/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

.. but how ?

Create Components... beginning from the top.

@SvenRuppert 94/165

Page 93: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 94/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

.. but how ?

Create Components... beginning from the top.

.. every Component will have

@SvenRuppert 95/165

Page 94: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 95/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

.. but how ?

Create Components... beginning from the top.

.. every Component will have

.. .. a class called XYZPane

@SvenRuppert 96/165

Page 95: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 96/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

.. but how ?

Create Components... beginning from the top.

.. every Component will have

.. .. a class called XYZPane

.. .. a class called XYZPaneController

@SvenRuppert 97/165

Page 96: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 97/164

JavaFX 8 - TestFXfinally TestFX

Now... FXML

.. with FXML you could describe the UI

.. but how ?

Create Components... beginning from the top.

.. every Component will have

.. .. a class called XYZPane

.. .. a class called XYZPaneController

.. .. a file called XYZPane.fxml

@SvenRuppert 98/165

Page 97: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 98/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class Main extends Application {

@Override public void start(Stage primaryStage) throws Exception { MainPane root = new MainPane(); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 300, 275)); primaryStage.show(); }

public static void main(String[] args) { launch(args); }}

JAVA

99/165

Page 98: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 99/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class BasePane<T> extends AnchorPane { public T controller;

public BasePane() { final String fxmlFile = this.getClass().getSimpleName()+".fxml"; FXMLLoader loader = createFXMLLoader(fxmlFile); controller = loader.getController(); AnchorPane.setBottomAnchor(this, 0.0); AnchorPane.setTopAnchor(this, 0.0); AnchorPane.setLeftAnchor(this, 0.0); AnchorPane.setRightAnchor(this, 0.0); } private FXMLLoader createFXMLLoader(final String fxmlFile) { URL resource = getClass().getResource(fxmlFile); FXMLLoader loader = new FXMLLoader(resource); loader.setRoot(this); try { loader.load(); } catch (IOException e) { e.printStackTrace(); } return loader; }}

JAVA

100/165

Page 99: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 100/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class MainPane extends BasePane<MainPaneController> { public MainPane(){ }}

JAVA

101/165

Page 100: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 101/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

public class MainPane extends BasePane<MainPaneController> { public MainPane(){ }}public class MainPaneController {

@FXML Button btn; @FXML EditPane editPane;

public MainPaneController() { System.out.println("MainPaneController = OK" ); }

private int counter = 0;

public void onDoSomething(ActionEvent actionEvent){ btn.setText("Main pressed " + counter); counter = counter +1; //label from Edit changing editPane.setLabelText(); }}

JAVA

102/165

Page 101: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 102/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

<?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.VBox?> <?import org.rapidpm.demo.jdkio2015.v005.fx.main.edit.EditPane?> <fx:root type="org.rapidpm.demo.jdkio2015.v005.fx.main.MainPane" fx:controller="org.rapidpm.demo.jdkio2015.v005.fx.main.MainPaneController" xmlns:fx="http://javafx.com/fxml" fx:id="mainPane" >

<!--style="-fx-scale-x: 4; -fx-scale-y: 4"--> <VBox> <Label fx:id="lb" text="Hello World"/> <Button fx:id="btn" onAction="#onDoSomething" text="Main Button"/> <EditPane fx:id="editPane"/> </VBox> </fx:root>

JAVA

103/165

Page 102: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 103/164

JavaFX 8 - TestFXfinally TestFX

@SvenRuppert

@Override protected Parent getRootNode() { return new MainPane(); }

@Test public void testMainButton01() throws Exception { final Label lbMain = find("#lb"); Assert.assertNotNull(lbMain); Assertions.verifyThat(lbMain, hasText("Hello World"));

final Button btnMain = find("#btn"); Assert.assertNotNull(btnMain); Assertions.verifyThat(btnMain, hasText("Main Button"));

final EditPane editPane = find("#editPane"); Assert.assertNotNull(editPane); }

JAVA

104/165

Page 103: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 104/164

Why CDI

why CDI ? ;-)

@SvenRuppert 105/165

Page 104: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 105/164

Why CDI

we want to get rid of hard connections

@SvenRuppert 106/165

Page 105: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 106/164

Why CDI

we want to get rid of hard connections

Have a look at the definition of a List. List will be the return value -> List.

@SvenRuppert

public List<Data> execute(){ ... }; JAVA

107/165

Page 106: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 107/164

Why CDI

the caller of the method dont know if it is an ArrayList or LinkedList... but

@SvenRuppert

import java.util.List;import java.util.ArrayList;

public List<Data> execute(){ final List<Data> result = new ArrayList<>(); //.... return result;}

JAVA

108/165

Page 107: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 108/164

Why CDI

the caller of the method dont know if it is an ArrayList or LinkedList... but

.. inside the method you will have a static ref to ArrayList

@SvenRuppert

import java.util.List;import java.util.ArrayList;

public List<Data> execute(){ final List<Data> result = new ArrayList<>(); //.... return result;}

JAVA

109/165

Page 108: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 109/164

Why CDI

if you are not using a special method from ArrayList ... -> trimToSize()

@SvenRuppert 110/165

Page 109: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 110/164

Why CDI

if you are not using a special method from ArrayList ... -> trimToSize()

Why not get rid of the static ref ??

@SvenRuppert 111/165

Page 110: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 111/164

Why CDI

if you are not using a special method from ArrayList ... -> trimToSize()

Why not get rid of the static ref ??

Maybe at runtime a LinkedList would be better.. we dont want to compile allagain..

@SvenRuppert 112/165

Page 111: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 112/164

Why CDI

if you are not using a special method from ArrayList ... -> trimToSize()

Why not get rid of the static ref ??

Maybe at runtime a LinkedList would be better.. we dont want to compile allagain..

ok, we could use Factories..

@SvenRuppert 113/165

Page 112: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 113/164

Why CDI

we are using plain SE

.. an example of a factory implementation

@SvenRuppert

import java.util.ArrayList; import java.util.LinkedList; import java.util.List;

public class ListFactory { public List createArrayList() { return new ArrayList(); } public List createLinkedList() { return new LinkedList(); } public List createList() { return new ArrayList(); }}

JAVA

114/165

Page 113: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 114/164

Why CDI

Do we have something better reached?

@SvenRuppert 115/165

Page 114: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 115/164

Why CDI

Do we have something better reached?

the developers in your project are using this factory...

@SvenRuppert 116/165

Page 115: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 116/164

Why CDI

Do we have something better reached?

the developers in your project are using this factory... if they will know that thereis a factory ;-)

@SvenRuppert 117/165

Page 116: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 117/164

Why CDI

@SvenRuppert

import java.util.List; import java.util.ArrayList;public List<Data> execute(){ final List<Data> result = new ArrayList<>(); //.... return result;}

JAVA

118/165

Page 117: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 118/164

Why CDI

@SvenRuppert

import java.util.List; import java.util.ArrayList;public List<Data> execute(){ final List<Data> result = new ArrayList<>(); //.... return result;}import java.util.List;import org.rapidpm.demo.ListFactory;public List<Data> execute(){ final List list = new ListFactory().createArrayList(); //.... return result;}

JAVA

119/165

Page 118: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 119/164

Why CDI

ok, no static ref to ArrayList..

@SvenRuppert 120/165

Page 119: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 120/164

Why CDI

ok, no static ref to ArrayList..

but we have a static ref to the factory method

@SvenRuppert 121/165

Page 120: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 121/164

Why CDI

ok, no static ref to ArrayList..

but we have a static ref to the factory method

The implementation of the ListFactory itself will have all ref to allimplementations

@SvenRuppert 122/165

Page 121: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 122/164

Solution Nr 1based on Java SE

a little bit more dynamic..

@SvenRuppert 123/165

Page 122: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 123/164

Solution Nr 1based on Java SE

a little bit more dynamic..

@SvenRuppert

import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.rapidpm.demo.cdi.commons.registry.ContextResolver;

public class ListFactory { public List createList(final ContextResolver contextResolver){ if(contextResolver == null){ return createArrayList(); } else { if(contextResolver.resolveContext()){ return createArrayList(); } else{ return createLinkedList(); } } }}

JAVA

124/165

Page 123: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 124/164

Solution Nr 1based on Java SE

a little bit more dynamic..

the complexity is now -> ContextResolver

@SvenRuppert 125/165

Page 124: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 125/164

Solution Nr 1based on Java SE

a little bit more dynamic..

the complexity is now -> ContextResolver

Added dep to the ContextResolver

@SvenRuppert 126/165

Page 125: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 126/164

Solution Nr 1based on Java SE

a little bit more dynamic..

the complexity is now -> ContextResolver

Added dep to the ContextResolver

you got: at runtime you could switch the implementation

@SvenRuppert 127/165

Page 126: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 127/164

Solution Nr 1based on Java SE

if you want to get rid of the deps inside the factory.. create a registry

@SvenRuppert 128/165

Page 127: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 128/164

Solution Nr 1based on Java SE

if you want to get rid of the deps inside the factory.. create a registry

at runtime you could register / unregister implemenattaions..

@SvenRuppert 129/165

Page 128: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 129/164

Solution Nr 1based on Java SE

if you want to get rid of the deps inside the factory.. create a registry

at runtime you could register / unregister implemenattaions..

CDI will help...

@SvenRuppert 130/165

Page 129: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 130/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

@SvenRuppert 131/165

Page 130: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 131/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

@SvenRuppert

@Inject List list; JAVA

132/165

Page 131: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 132/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

@SvenRuppert

@Inject @CDILegacyTest List list; JAVA

133/165

Page 132: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 133/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

@SvenRuppert

@Inject @CDILegacyTest List list;

@Produces @CDILegacyTestpublic List createList(...){...}

JAVA

134/165

Page 133: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 134/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

@SvenRuppert

@Inject @CDILegacyTest List list;

@Produces @CDILegacyTestpublic List createList(InjectionPoint injectionPoint, BeanManager beanManager, ContextResolver contextResolver){ ....}

JAVA

135/165

Page 134: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 135/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

@SvenRuppert

@Inject @CDILegacyTest List list;

@Produces @CDILegacyTestpublic List createList(InjectionPoint injectionPoint, BeanManager beanManager, ContextResolver contextResolver){ boolean b = contextResolver.resolveContext(...); //treffen der Entscheidungen... if(b){ return new LinkedList(); } else { return new ArrayList(); }}

JAVA

136/165

Page 135: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 136/164

finally CDI ;-)based on Weld / WELD-SE

instances are given from the container via @Inject

the time to create the instance is to early..

@SvenRuppert

@Inject @CDILegacyTest List list;

@Produces @CDILegacyTestpublic List createList(InjectionPoint injectionPoint, BeanManager beanManager, ContextResolver contextResolver){ boolean b = contextResolver.resolveContext(...); //treffen der Entscheidungen... if(b){ return new LinkedList(); } else { return new ArrayList(); }}

JAVA

137/165

Page 136: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 137/164

finally CDI ;-)based on Weld / WELD-SE

but it could be done at runtime..

@SvenRuppert 138/165

Page 137: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 138/164

finally CDI ;-)based on Weld / WELD-SE

but it could be done at runtime..

decide as late as possible

@SvenRuppert 139/165

Page 138: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 139/164

finally CDI ;-)based on Weld / WELD-SE

but it could be done at runtime..

decide as late as possible

@SvenRuppert

@Inject @CDILegacyTest Instance<List> listInstance; JAVA

140/165

Page 139: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 140/164

finally CDI ;-)based on Weld / WELD-SE

but it could be done at runtime..

decide as late as possible

@SvenRuppert

@Inject @CDILegacyTest Instance<List> listInstance;//..späterfinal List list = listInstance.get();

JAVA

141/165

Page 140: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 141/164

finally CDI ;-)based on Weld / WELD-SE

but it could be done at runtime..

decide as late as possible

@SvenRuppert

@Inject @CDILegacyTest Instance<List> listInstance;//..späterfinal List list = listInstance.get();

@Produces @CDILegacyTestpublic List createList(InjectionPoint injectionPoint, BeanManager beanManager, ContextResolver contextResolver){ boolean b = contextResolver.resolveContext(...); //treffen der Entscheidungen... if(b){ return new LinkedList(); } else { return new ArrayList(); }}

JAVA

142/165

Page 141: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 142/164

finally CDI ;-)based on Weld / WELD-SE

static bound via AnnotationsLiteral:

@SvenRuppert

@Inject @MyQualifier List liste; JAVA

143/165

Page 142: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 143/164

finally CDI ;-)based on Weld / WELD-SE

static bound via AnnotationsLiteral:

Generics are not well supported

@SvenRuppert

@Inject @MyQualifier List liste; JAVA

144/165

Page 143: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 144/164

finally CDI ;-)based on Weld / WELD-SE

static bound via AnnotationsLiteral:

Generics are not well supported

but still complex to use

@SvenRuppert

@Inject @MyQualifier List liste; JAVA

145/165

Page 144: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 145/164

CDI explained by codeQualifier und AnnotationsLiterale

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

146/165

Page 145: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 146/164

CDI explained by codeQualifier und AnnotationsLiterale

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

147/165

Page 146: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 147/164

CDI explained by codeQualifier und AnnotationsLiterale

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

@Producer @CDILogger Logger create(...); JAVA

148/165

Page 147: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 148/164

CDI explained by codeQualifier und AnnotationsLiterale

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

@Producer @CDILogger Logger create(...); JAVA

@Producer @CDILogger Logger create(..){ AnnotationsLiteral prodAL = contextResolver.resolve(..);};

JAVA

149/165

Page 148: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 149/164

CDI explained by codeQualifier und AnnotationsLiterale

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

@Producer @CDILogger Logger create(...); JAVA

@Producer @CDILogger Logger create(BeanManager bm,ContextResolver cr){ AnnotationsLiteral prodAL = contextResolver.resolve(..); return creator.getManagedInstance(Logger.class, annotationLiteral);};

JAVA

150/165

Page 149: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 150/164

CDI explained by codeQualifier und AnnotationsLiterale

you will get a dynamic AnnotationLiteral

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

@Producer @CDILogger Logger create(...); JAVA

@Producer @CDILogger Logger create(BeanManager bm,ContextResolver cr){ AnnotationsLiteral prodAL = contextResolver.resolve(..); return creator.getManagedInstance(Logger.class, annotationLiteral);};

JAVA

151/165

Page 150: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 151/164

CDI explained by codeQualifier und AnnotationsLiterale

you will get a dynamic AnnotationLiteral

AnnotationsLiteral and class -> select the Producers

@SvenRuppert

@Inject @CDILogger Logger logger; JAVA

@Producer @CDILogger Logger create(...); JAVA

@Producer @CDILogger Logger create(BeanManager bm,ContextResolver cr){ AnnotationsLiteral prodAL = contextResolver.resolve(..); return creator.getManagedInstance(Logger.class, annotationLiteral);};

JAVA

152/165

Page 151: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 152/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

@SvenRuppert 153/165

Page 152: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 153/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

@SvenRuppert 154/165

Page 153: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 154/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

@SvenRuppert 155/165

Page 154: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 155/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

combines: FXML, Convention over Configuration and JSR-330 / @Inject

integrated with maven 3

@SvenRuppert 156/165

Page 155: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 156/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

combines: FXML, Convention over Configuration and JSR-330 / @Inject

integrated with maven 3

under active development

@SvenRuppert 157/165

Page 156: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 157/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

combines: FXML, Convention over Configuration and JSR-330 / @Inject

integrated with maven 3

under active development

injection over a few steps is working

@SvenRuppert 158/165

Page 157: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 158/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

combines: FXML, Convention over Configuration and JSR-330 / @Inject

integrated with maven 3

under active development

injection over a few steps is working

postconstruct is working

@SvenRuppert 159/165

Page 158: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 159/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

combines: FXML, Convention over Configuration and JSR-330 / @Inject

integrated with maven 3

under active development

injection over a few steps is working

postconstruct is working

using existing CDI Services with Annotations (Scopes and so on) is not working with afterburner

@SvenRuppert 160/165

Page 159: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 160/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner ...

apache licensed

as lean as possible: 3 classes, no external dependencies

combines: FXML, Convention over Configuration and JSR-330 / @Inject

integrated with maven 3

under active development

injection over a few steps is working

postconstruct is working

using existing CDI Services with Annotations (Scopes and so on) is not working with afterburner

no mixed mode with CDI and afterburner.fx

@SvenRuppert 161/165

Page 160: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 161/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with afterburner.fx ... show the code ..

@SvenRuppert 162/165

Page 161: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 162/164

CDI explained by codeQualifier und AnnotationsLiterale

Now... JavaFX with CDI bootstrap ... code only..

@SvenRuppert 163/165

Page 162: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 163/164

@SvenRupperthttp://www.rapidpm.org // http://www.codecentric.de

(privater) Tech - Newsletter -> Core Java, Reflection, IoT, uvmBei Interesse bitte bei mir eintragen -> plain old Zettel ;-)

g+: www.google.com/+SvenRuppertgithub : github.com/svenruppert

Page 163: JavaFX8   TestFX - CDI

20.10.2015 JavaFX8 - TestFX - CDI - JUG

http://localhost:63342/java-courses/doc/javafx-testfx-tdd.html#1 164/164

<Thank You!>

g+ www.google.com/+SvenRupperttwitter @SvenRuppertwww www.rapidpm.orggithub github.com/svenruppert