Skip to content

Commit e12ebf0

Browse files
author
Linnea Vardal
committed
Tests are green
1 parent a08d470 commit e12ebf0

2 files changed

Lines changed: 61 additions & 17 deletions

File tree

src/main/java/com/example/HelloModel.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,53 @@ public class HelloModel {
1414
/**
1515
* Returns a greeting based on the current Java and JavaFX versions.
1616
*/
17+
18+
private final ObservableList<NtfyMessage> messages = FXCollections.observableArrayList();
19+
private final BooleanProperty connected = new SimpleBooleanProperty(false);
20+
private boolean isTesting = false; // <-- Lägg till denna rad
21+
1722
public String getGreeting() {
1823
String javaVersion = System.getProperty("java.version");
1924
String javafxVersion = System.getProperty("javafx.version");
2025
return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".";
2126
}
2227

23-
//Nya fält för chat-funktionalitet
24-
private final ObservableList<NtfyMessage> messages = FXCollections.observableArrayList();
25-
private final BooleanProperty connected = new SimpleBooleanProperty(false);
26-
27-
//Getter för meddelandelistan
2828
public ObservableList<NtfyMessage> getMessages() {
2929
return messages;
3030
}
3131

32-
//Getter för anslutningstillstånd
32+
3333
public ReadOnlyBooleanProperty connectedProperty() {
3434
return connected;
3535
}
3636

37-
//Metod för att lägga till meddelande
37+
public void setTesting(boolean testing) { // <-- Lägg till denna metod
38+
this.isTesting = testing;
39+
}
40+
41+
// Ersätt den gamla runOnFX-metoden med denna:
42+
private void runOnFX(Runnable task) {
43+
if (isTesting) { // <-- Ny logik för tester
44+
task.run();
45+
} else if (Platform.isFxApplicationThread()) {
46+
task.run();
47+
} else {
48+
try {
49+
Platform.runLater(task);
50+
} catch (IllegalThreadStateException notInitialized) {
51+
task.run(); // Fallback för enhetstester (om JavaFX inte är initierat)
52+
}
53+
}
54+
55+
}
56+
3857
public void addMessage(NtfyMessage message) {
3958
runOnFX(() -> messages.add(message));
4059
}
4160

42-
//Metod för att uppdatera anslutningstillstånd
4361
public void setConnected(boolean connected) {
4462
runOnFX(() -> this.connected.set(connected));
4563
}
4664

47-
//Dispatcher Utility
48-
private static void runOnFX(Runnable task) {
49-
try {
50-
if (Platform.isFxApplicationThread()) task.run();
51-
else Platform.runLater(task);
52-
} catch (IllegalThreadStateException notInitialized) {
53-
task.run(); //Fallback för enhetstester
54-
}
55-
}
65+
5666
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
public class HelloModelTest {
7+
8+
@Test
9+
void testGetMessages_ReturnsEmptyListInitially() {
10+
HelloModel model = new HelloModel();
11+
assertThat(model.getMessages()).isEmpty();
12+
}
13+
14+
15+
@Test
16+
void testAddMessage_AddsMessageToList() {
17+
HelloModel model = new HelloModel();
18+
model.setTesting(true); //Aktivera testläge
19+
NtfyMessage testMessage = new NtfyMessage(
20+
"123",
21+
System.currentTimeMillis() / 1000,
22+
"message",
23+
"mytopic",
24+
"Testmeddelande"
25+
);
26+
model.addMessage(testMessage);
27+
assertThat(model.getMessages())
28+
.hasSize(1)
29+
.containsExactly(testMessage);
30+
31+
32+
}
33+
34+
}

0 commit comments

Comments
 (0)