Skip to content

Commit 7fff9c8

Browse files
committed
fix(perf): refactor some tests that don't require testfx
1 parent 5324186 commit 7fff9c8

23 files changed

Lines changed: 361 additions & 432 deletions

‎lefthook.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
pre-commit:
1717
parallel: false
1818
jobs:
19-
- name: "lint: mvn checkstyle:check"
20-
run: mvn -B checkstyle:check
21-
- name: "format: mvn spotless:check"
22-
run: mvn -B spotless:check
23-
- name: "test: mvn test (unit only, integration excluded)"
24-
run: mvn -B test
19+
- name: "lint: mvn checkstyle:check"
20+
run: mvn -B checkstyle:check
21+
- name: "format: mvn spotless:check"
22+
run: mvn -B spotless:check
23+
- name: "test: mvn test (unit only)"
24+
run: mvn -B clean test

‎src/main/java/com/DesktopApplicationClientJava/controllers/LoginFormController.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
public class LoginFormController extends Controller {
1515

16-
@FXML private TextField emailField;
17-
@FXML private PasswordField passwordField;
18-
@FXML private Label errorLabel;
19-
@FXML private Button submitButton;
16+
@FXML TextField emailField;
17+
@FXML PasswordField passwordField;
18+
@FXML Label errorLabel;
19+
@FXML Button submitButton;
2020

2121
@FXML
22-
private void onSubmit(ActionEvent event) {
22+
void onSubmit(ActionEvent event) {
2323
String email = emailField.getText() == null ? "" : emailField.getText().trim();
2424
String password = passwordField.getText() == null ? "" : passwordField.getText();
2525

‎src/main/java/com/DesktopApplicationClientJava/controllers/ResourceDetailsController.java‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ public class ResourceDetailsController extends Controller {
3030

3131
static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
3232

33-
@FXML private Label titleLabel;
34-
@FXML private Label descriptionLabel;
35-
@FXML private Label createdLabel;
36-
@FXML private Label updatedLabel;
37-
@FXML private Label uuidLabel;
38-
@FXML private Label errorLabel;
39-
@FXML private Label statusLabel;
40-
@FXML private TableView<FileInfo> filesTable;
41-
@FXML private TableColumn<FileInfo, String> nameColumn;
42-
@FXML private TableColumn<FileInfo, String> sizeColumn;
43-
@FXML private TableColumn<FileInfo, String> contentTypeColumn;
44-
@FXML private TableColumn<FileInfo, Void> downloadColumn;
45-
@FXML private Button backButton;
46-
@FXML private Button editButton;
33+
@FXML Label titleLabel;
34+
@FXML Label descriptionLabel;
35+
@FXML Label createdLabel;
36+
@FXML Label updatedLabel;
37+
@FXML Label uuidLabel;
38+
@FXML Label errorLabel;
39+
@FXML Label statusLabel;
40+
@FXML TableView<FileInfo> filesTable;
41+
@FXML TableColumn<FileInfo, String> nameColumn;
42+
@FXML TableColumn<FileInfo, String> sizeColumn;
43+
@FXML TableColumn<FileInfo, String> contentTypeColumn;
44+
@FXML TableColumn<FileInfo, Void> downloadColumn;
45+
@FXML Button backButton;
46+
@FXML Button editButton;
4747

4848
private UUID resourceId;
4949

@@ -101,11 +101,12 @@ public void load() {
101101
}
102102

103103
@FXML
104-
private void onBack(ActionEvent event) {
104+
void onBack(ActionEvent event) {
105105
navigator.goTo("/fxml/ResourceList.fxml");
106106
}
107107

108-
private void onDownload(FileInfo file) {
108+
@FXML
109+
void onDownload(FileInfo file) {
109110
File dir = chooseDirectory(backButton.getScene().getWindow());
110111
if (dir == null) {
111112
return;

‎src/main/java/com/DesktopApplicationClientJava/controllers/ResourceFormController.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public class ResourceFormController extends Controller {
1616
private static final int TITLE_MIN = 3;
1717
private static final int TITLE_MAX = 255;
1818

19-
@FXML private Label formTitleLabel;
20-
@FXML private TextField titleField;
21-
@FXML private TextArea descriptionArea;
22-
@FXML private Label errorLabel;
23-
@FXML private Button cancelButton;
24-
@FXML private Button saveButton;
19+
@FXML Label formTitleLabel;
20+
@FXML TextField titleField;
21+
@FXML TextArea descriptionArea;
22+
@FXML Label errorLabel;
23+
@FXML Button cancelButton;
24+
@FXML Button saveButton;
2525

2626
private UUID resourceId;
2727

@@ -39,7 +39,7 @@ public void setResourceId(UUID resourceId) {
3939
}
4040

4141
@FXML
42-
private void initialize() {
42+
void initialize() {
4343
hideError();
4444
if (resourceId == null) {
4545
formTitleLabel.setText("Новый документ");
@@ -60,7 +60,7 @@ private void loadExisting() {
6060
}
6161

6262
@FXML
63-
private void onSave(ActionEvent event) {
63+
void onSave(ActionEvent event) {
6464
hideError();
6565
String title = titleField.getText() == null ? "" : titleField.getText().trim();
6666
String description = descriptionArea.getText() == null ? "" : descriptionArea.getText().trim();
@@ -95,7 +95,7 @@ private void onSave(ActionEvent event) {
9595
}
9696

9797
@FXML
98-
private void onCancel(ActionEvent event) {
98+
void onCancel(ActionEvent event) {
9999
navigator.goTo("/fxml/ResourceList.fxml");
100100
}
101101

src/test/java/ui/LoginFormControllerTest.java renamed to src/test/java/com/DesktopApplicationClientJava/controllers/LoginFormControllerTest.java

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ui;
1+
package com.DesktopApplicationClientJava.controllers;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -18,89 +18,79 @@
1818
import com.DesktopApplicationClientJava.session.Session;
1919
import com.DesktopApplicationClientJava.utils.ControllerWiring;
2020
import java.util.UUID;
21-
import javafx.fxml.FXMLLoader;
22-
import javafx.scene.Parent;
23-
import javafx.scene.Scene;
21+
import javafx.application.Platform;
22+
import javafx.event.ActionEvent;
2423
import javafx.scene.control.Label;
25-
import javafx.stage.Stage;
24+
import javafx.scene.control.PasswordField;
25+
import javafx.scene.control.TextField;
26+
import org.junit.jupiter.api.BeforeAll;
2627
import org.junit.jupiter.api.BeforeEach;
2728
import org.junit.jupiter.api.Test;
28-
import org.testfx.api.FxToolkit;
2929

30-
class LoginFormControllerTest extends FxTestBase {
30+
class LoginFormControllerTest {
3131

3232
private static final String VALID_EMAIL = "user@example.com";
3333
private static final String VALID_PASSWORD = "secret123";
3434

3535
private AuthService authService;
3636
private Navigator navigator;
37-
38-
@BeforeEach
39-
void resetScene() throws Exception {
40-
FxToolkit.setupStage(
41-
stage -> {
42-
try {
43-
start(stage);
44-
} catch (Exception e) {
45-
throw new RuntimeException(e);
46-
}
47-
});
37+
private LoginFormController controller;
38+
39+
@BeforeAll
40+
static void bootstrapFx() {
41+
try {
42+
Platform.startup(() -> {});
43+
Platform.setImplicitExit(false);
44+
} catch (IllegalStateException alreadyStarted) {
45+
// JVM-wide singleton; another test class already started the toolkit.
46+
}
4847
}
4948

50-
void start(Stage stage) throws Exception {
49+
@BeforeEach
50+
void setUp() {
5151
authService = mock(AuthService.class);
52+
navigator = mock(Navigator.class);
53+
5254
ServiceRegistry services = mock(ServiceRegistry.class);
5355
when(services.getAuthService()).thenReturn(authService);
54-
navigator = mock(Navigator.class);
5556

56-
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/LoginForm.fxml"));
57-
loader.setControllerFactory(
58-
clazz -> {
59-
try {
60-
Object controller = clazz.getDeclaredConstructor().newInstance();
61-
ControllerWiring.wire(controller, navigator, new Session(), services);
62-
return controller;
63-
} catch (Exception e) {
64-
throw new RuntimeException("Cannot create controller: " + clazz, e);
65-
}
66-
});
67-
Parent root = loader.load();
68-
stage.setScene(new Scene(root));
69-
stage.show();
57+
controller = new LoginFormController();
58+
ControllerWiring.wire(controller, navigator, new Session(), services);
59+
60+
controller.emailField = new TextField();
61+
controller.passwordField = new PasswordField();
62+
controller.errorLabel = new Label();
7063
}
7164

7265
@Test
7366
void emptyFieldsShowErrorAndDoNotCallApi() {
74-
robot.clickOn("#submitButton");
67+
controller.onSubmit(new ActionEvent());
7568

76-
Label error = robot.lookup("#errorLabel").queryAs(Label.class);
77-
assertTrue(error.isVisible());
78-
assertEquals("Заполните все поля", error.getText());
69+
assertTrue(controller.errorLabel.isVisible());
70+
assertEquals("Заполните все поля", controller.errorLabel.getText());
7971
verify(authService, never()).login(anyString(), anyString());
8072
verify(navigator, never()).goTo(anyString());
8173
}
8274

8375
@Test
8476
void blankPasswordShowsErrorAndDoesNotCallApi() {
85-
fillText(robot, "#emailField", VALID_EMAIL);
86-
robot.clickOn("#submitButton");
77+
controller.emailField.setText(VALID_EMAIL);
78+
controller.onSubmit(new ActionEvent());
8779

88-
Label error = robot.lookup("#errorLabel").queryAs(Label.class);
89-
assertTrue(error.isVisible());
90-
assertEquals("Заполните все поля", error.getText());
80+
assertTrue(controller.errorLabel.isVisible());
81+
assertEquals("Заполните все поля", controller.errorLabel.getText());
9182
verify(authService, never()).login(anyString(), anyString());
9283
verify(navigator, never()).goTo(anyString());
9384
}
9485

9586
@Test
9687
void invalidEmailShowsErrorAndDoesNotCallApi() {
97-
fillText(robot, "#emailField", "not-an-email");
98-
fillText(robot, "#passwordField", VALID_PASSWORD);
99-
robot.clickOn("#submitButton");
88+
controller.emailField.setText("not-an-email");
89+
controller.passwordField.setText(VALID_PASSWORD);
90+
controller.onSubmit(new ActionEvent());
10091

101-
Label error = robot.lookup("#errorLabel").queryAs(Label.class);
102-
assertTrue(error.isVisible());
103-
assertEquals("Неверный формат", error.getText());
92+
assertTrue(controller.errorLabel.isVisible());
93+
assertEquals("Неверный формат", controller.errorLabel.getText());
10494
verify(authService, never()).login(anyString(), anyString());
10595
verify(navigator, never()).goTo(anyString());
10696
}
@@ -110,28 +100,26 @@ void successfulLoginNavigatesToResourceList() {
110100
User user = new User(UUID.randomUUID(), VALID_EMAIL, Role.USER, "Имя", "Фамилия", true);
111101
when(authService.login(VALID_EMAIL, VALID_PASSWORD)).thenReturn(user);
112102

113-
fillText(robot, "#emailField", VALID_EMAIL);
114-
fillText(robot, "#passwordField", VALID_PASSWORD);
115-
robot.clickOn("#submitButton");
103+
controller.emailField.setText(VALID_EMAIL);
104+
controller.passwordField.setText(VALID_PASSWORD);
105+
controller.onSubmit(new ActionEvent());
116106

117107
verify(authService).login(VALID_EMAIL, VALID_PASSWORD);
118108
verify(navigator).goTo("/fxml/ResourceList.fxml");
119-
Label error = robot.lookup("#errorLabel").queryAs(Label.class);
120-
assertFalse(error.isVisible());
109+
assertFalse(controller.errorLabel.isVisible());
121110
}
122111

123112
@Test
124113
void failedLoginShowsServerErrorAndDoesNotNavigate() {
125114
when(authService.login(VALID_EMAIL, VALID_PASSWORD))
126115
.thenThrow(new ServiceException("Неверный логин или пароль"));
127116

128-
fillText(robot, "#emailField", VALID_EMAIL);
129-
fillText(robot, "#passwordField", VALID_PASSWORD);
130-
robot.clickOn("#submitButton");
117+
controller.emailField.setText(VALID_EMAIL);
118+
controller.passwordField.setText(VALID_PASSWORD);
119+
controller.onSubmit(new ActionEvent());
131120

132-
Label error = robot.lookup("#errorLabel").queryAs(Label.class);
133-
assertTrue(error.isVisible());
134-
assertEquals("Неверный логин или пароль", error.getText());
121+
assertTrue(controller.errorLabel.isVisible());
122+
assertEquals("Неверный логин или пароль", controller.errorLabel.getText());
135123
verify(navigator, never()).goTo(anyString());
136124
}
137125
}

0 commit comments

Comments
 (0)