Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
javafx-template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sigrid Festøy
javafx-template
Commits
3a408f4e
Commit
3a408f4e
authored
3 years ago
by
Hallvard Trætteberg
Browse files
Options
Downloads
Patches
Plain Diff
Wrote tests with TestFX
parent
b4c45dab
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
javafx-template/src/main/java/app/AppController.java
+9
-0
9 additions, 0 deletions
javafx-template/src/main/java/app/AppController.java
javafx-template/src/test/java/app/AppTest.java
+110
-2
110 additions, 2 deletions
javafx-template/src/test/java/app/AppTest.java
with
119 additions
and
2 deletions
javafx-template/src/main/java/app/AppController.java
+
9
−
0
View file @
3a408f4e
...
...
@@ -19,6 +19,15 @@ public class AppController {
calc
=
new
Calc
(
0.0
,
0.0
,
0.0
);
}
public
Calc
getCalc
()
{
return
calc
;
}
public
void
setCalc
(
Calc
calc
)
{
this
.
calc
=
calc
;
updateOperandsView
();
}
@FXML
private
ListView
<
Double
>
operandsView
;
...
...
This diff is collapsed.
Click to expand it.
javafx-template/src/test/java/app/AppTest.java
+
110
−
2
View file @
3a408f4e
...
...
@@ -3,22 +3,130 @@ package app;
import
javafx.fxml.FXMLLoader
;
import
javafx.scene.Parent
;
import
javafx.scene.Scene
;
import
javafx.scene.control.Label
;
import
javafx.scene.control.ListView
;
import
javafx.stage.Stage
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.Arguments
;
import
org.junit.jupiter.params.provider.MethodSource
;
import
org.opentest4j.AssertionFailedError
;
import
org.testfx.framework.junit5.ApplicationTest
;
import
org.testfx.matcher.control.LabeledMatchers
;
/**
* TestFX App test
*/
public
class
AppTest
extends
ApplicationTest
{
private
AppController
controller
;
private
Parent
root
;
@Override
public
void
start
(
Stage
stage
)
throws
IOException
{
FXMLLoader
fxmlLoader
=
new
FXMLLoader
(
this
.
getClass
().
getResource
(
"App.fxml"
));
Parent
parent
=
fxmlLoader
.
load
();
stage
.
setScene
(
new
Scene
(
parent
));
root
=
fxmlLoader
.
load
();
controller
=
fxmlLoader
.
getController
();
stage
.
setScene
(
new
Scene
(
root
));
stage
.
show
();
}
public
Parent
getRootNode
()
{
return
root
;
}
private
String
enterLabel
=
"""
E
n
t
e
r
"""
.
stripTrailing
();
private
void
click
(
String
...
labels
)
{
for
(
var
label
:
labels
)
{
clickOn
(
LabeledMatchers
.
hasText
(
label
));
}
}
private
String
getOperandString
()
{
return
((
Label
)
getRootNode
().
lookup
(
"#operandView"
)).
getText
();
}
private
ListView
<
Double
>
getOperandsView
()
{
return
(
ListView
<
Double
>)
getRootNode
().
lookup
(
"#operandsView"
);
}
private
void
checkView
(
double
...
operands
)
{
for
(
int
i
=
0
;
i
<
operands
.
length
;
i
++)
{
Assertions
.
assertEquals
(
operands
[
i
],
controller
.
getCalc
().
peekOperand
(
i
),
"Wrong value at #"
+
i
+
" of operand stack"
);
}
List
<
Double
>
viewItems
=
getOperandsView
().
getItems
();
for
(
int
i
=
0
;
i
<
operands
.
length
;
i
++)
{
Assertions
.
assertEquals
(
operands
[
i
],
viewItems
.
get
(
viewItems
.
size
()
-
i
-
1
),
"Wrong value at #"
+
i
+
" of operands view"
);
}
}
private
void
checkView
(
String
operandString
,
double
...
operands
)
{
Assertions
.
assertEquals
(
operandString
,
getOperandString
());
checkView
(
operands
);
}
// see https://www.baeldung.com/parameterized-tests-junit-5
// about @ParameterizedTest
@ParameterizedTest
@MethodSource
public
void
testClicksOperand
(
String
labels
,
String
operandString
)
{
for
(
var
label
:
labels
.
split
(
" "
))
{
click
(
label
);
}
checkView
(
operandString
);
}
private
static
Stream
<
Arguments
>
testClicksOperand
()
{
return
Stream
.
of
(
Arguments
.
of
(
"2 7"
,
"27"
),
Arguments
.
of
(
"2 7 ."
,
"27."
),
Arguments
.
of
(
"2 7 . 5"
,
"27.5"
),
Arguments
.
of
(
"2 7 . 5 ."
,
"27."
)
);
}
@ParameterizedTest
@MethodSource
public
void
testClicksOperands
(
String
labels
,
String
operandsString
)
{
for
(
var
label
:
labels
.
split
(
" "
))
{
click
(
label
.
equals
(
"\n"
)
?
enterLabel
:
label
);
}
checkView
(
Stream
.
of
(
operandsString
.
split
(
" "
)).
mapToDouble
(
Double:
:
valueOf
).
toArray
());
}
private
static
Stream
<
Arguments
>
testClicksOperands
()
{
return
Stream
.
of
(
Arguments
.
of
(
"2 7 . 5 \n"
,
"27.5"
),
Arguments
.
of
(
"2 7 \n"
,
"27.0"
),
Arguments
.
of
(
"2 \n 7 \n 5 \n"
,
"5.0"
,
"7.0"
,
"2.0"
),
Arguments
.
of
(
"2 7 . \n"
,
"27.0"
),
Arguments
.
of
(
"2 7 . 5 \n"
,
"27.5"
),
Arguments
.
of
(
"2 \n 7 +"
,
"9.0"
),
Arguments
.
of
(
"2 \n 7 -"
,
"-5.0"
),
Arguments
.
of
(
"2 \n 7 *"
,
"14.0"
),
Arguments
.
of
(
"6 \n 3 /"
,
"2.0"
),
Arguments
.
of
(
"2 5 \n √"
,
"5.0"
)
);
}
@Test
public
void
testPi
()
{
click
(
"π"
);
checkView
(
""
,
Math
.
PI
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment