-
Notifications
You must be signed in to change notification settings - Fork 0
Adds Unit tests for HelloModel #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,7 +89,7 @@ public boolean sendFile(File file){ | |
| .PUT(HttpRequest.BodyPublishers.ofFile(file.toPath())) | ||
| .build(); | ||
|
|
||
| http.sendAsync(request, HttpResponse.BodyHandlers.discarding()); | ||
| http.send(request, HttpResponse.BodyHandlers.discarding()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking HTTP call can freeze the JavaFX UI thread. Changing from The TODO comment at lines 36-38 acknowledges the same issue for the Revert to the asynchronous approach: - http.send(request, HttpResponse.BodyHandlers.discarding());
+ http.sendAsync(request, HttpResponse.BodyHandlers.discarding());
|
||
| return true; | ||
|
|
||
| } catch (Exception e) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ | |
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
|
|
@@ -33,13 +36,96 @@ void sendMessageToFakeServer(WireMockRuntimeInfo wmRuntimeInfo) { | |
| var con = new NtfyConnectionImpl("http://localhost:" + wmRuntimeInfo.getHttpPort()); | ||
| var model = new HelloModel(con); | ||
| model.setMessageToSend("Hello World"); | ||
| stubFor(post("/mytopic").willReturn(ok())); | ||
| stubFor(post("/adam").willReturn(ok())); | ||
|
|
||
| model.sendMessage(); | ||
|
|
||
| // Verify call made to server | ||
| verify(postRequestedFor(urlEqualTo("/mytopic")) | ||
| verify(postRequestedFor(urlEqualTo("/adam")) | ||
| .withRequestBody(matching("Hello World"))); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given file when calling sendFile then connection.sendFile should be called") | ||
| void sendFileCallConnectionSendFile() { | ||
| var spy = new NtfyConnectionSpy(); | ||
| var model = new HelloModel(spy); | ||
|
|
||
| File file = new File("dummy.txt"); | ||
| model.sendFile(file); | ||
|
|
||
| assertThat(spy.sentFile).isEqualTo(file); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("sendFile should upload file to the server using PUT") | ||
| void sendFileToFakeServer(WireMockRuntimeInfo wmRuntimeInfo) throws Exception { | ||
| var con = new NtfyConnectionImpl("http://localhost:" + wmRuntimeInfo.getHttpPort()); | ||
| var model = new HelloModel(con); | ||
|
|
||
| File temp = File.createTempFile("upload_test", ".txt"); | ||
|
|
||
| stubFor(put("/adam").willReturn(ok())); | ||
|
|
||
| model.sendFile(temp); | ||
|
|
||
| verify(putRequestedFor(urlEqualTo("/adam"))); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Model should receive messages when connection invokes handler") | ||
| void receiveMessageShouldAddToModelViewHander(){ | ||
| var spy = new NtfyConnectionSpy(); | ||
| var model = new HelloModel(spy); | ||
|
|
||
| NtfyMessageDto incoming = new NtfyMessageDto( | ||
| "123", | ||
| 1000, | ||
| 0, | ||
| "message", | ||
| "adam", | ||
| "this is a test hehehe", | ||
| null | ||
| ); | ||
|
|
||
| spy.simulateIncoming(incoming); | ||
|
|
||
| assertThat(model.getMessages()).containsExactly(incoming); | ||
| } | ||
|
Comment on lines
+76
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainTest may not properly verify JavaFX threading behavior. The test calls In a proper JavaFX test environment, you should either:
Consider verifying whether JavaFX is properly initialized in the test environment: Additionally, search for TestFX or similar JavaFX testing utilities: 🏁 Script executed: #!/bin/bash
# Check if JavaFX test dependencies or initialization are present in the test setup
rg -n "Platform\.startup|FxToolkit|ApplicationTest|@BeforeAll.*Platform" --type=javaLength of output: 0 🏁 Script executed: #!/bin/bash
# Look for JavaFX testing frameworks in dependencies
rg -n "testfx|WaitForAsyncUtils" Length of output: 0 I need to gather more information about the actual implementation to verify the review concern. Let me search for the relevant source code: 🏁 Script executed: #!/bin/bash
# Find HelloModel class and check receiveMessage implementation
rg -n "class HelloModel" --type=java -A 20Length of output: 3266 🏁 Script executed: #!/bin/bash
# Find the receiveMessage method in HelloModel
rg -n "receiveMessage" --type=java -B 2 -A 10Length of output: 2816 🏁 Script executed: #!/bin/bash
# Find NtfyConnectionSpy and simulateIncoming implementation
rg -n "class NtfyConnectionSpy|simulateIncoming" --type=java -B 2 -A 10Length of output: 2522 🏁 Script executed: #!/bin/bash
# Check for pom.xml to see dependencies
fd -n "pom.xml|build.gradle" -x cat {}Length of output: 230 🏁 Script executed: #!/bin/bash
# Look for test setup or configuration methods in the test file
rg -n "@BeforeEach|@Before|@BeforeAll|static.*{|setUp" src/test/java/com/example/HelloModelTest.java -B 1 -A 5Length of output: 168 🏁 Script executed: #!/bin/bash
# Check imports and class declaration at top of HelloModelTest
head -20 src/test/java/com/example/HelloModelTest.javaLength of output: 688 🏁 Script executed: #!/bin/bash
# Check HelloModel imports for JavaFX
head -15 src/main/java/com/example/HelloModel.javaLength of output: 360 🏁 Script executed: #!/bin/bash
# Search for any Platform or javafx usage in the codebase
rg -n "import.*Platform|import.*javafx" --type=javaLength of output: 1882 🏁 Script executed: #!/bin/bash
# Fix the fd command to list pom.xml and build.gradle files
fd "pom.xml|build.gradle"Length of output: 7 🏁 Script executed: #!/bin/bash
# Check pom.xml for dependencies
cat pom.xmlLength of output: 3194 Test relies on IllegalStateException fallback instead of verifying actual JavaFX threading. The test passes because:
The production code's try-catch (lines 56-60 in Fix options:
🤖 Prompt for AI Agents |
||
|
|
||
| @Test | ||
| @DisplayName("Model constructor should register receive handler on connection") | ||
| void constructorShouldRegisterHandler() { | ||
|
|
||
| var spy = new NtfyConnectionSpy(); | ||
| var model = new HelloModel(spy); | ||
|
|
||
| assertThat(spy.handler).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("messageToSendProperty should update when setting message") | ||
| void messagePropertyShouldUpdate() { | ||
|
|
||
| var spy = new NtfyConnectionSpy(); | ||
| var model = new HelloModel(spy); | ||
|
|
||
| model.setMessageToSend("Hello!"); | ||
|
|
||
| assertThat(model.getMessageToSend()).isEqualTo("Hello!"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getGreeting should return the expected greeting text") | ||
| void greetingShouldBeCorrect() { | ||
|
|
||
| var model = new HelloModel(new NtfyConnectionSpy()); | ||
|
|
||
| assertThat(model.getGreeting()).isEqualTo("Chat Client by Adam"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,33 @@ | ||
| package com.example; | ||
|
|
||
| import java.io.File; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public class NtfyConnectionSpy implements NtfyConnection { | ||
|
|
||
| String message; | ||
| File sentFile; | ||
| Consumer<NtfyMessageDto> handler; | ||
|
|
||
| @Override | ||
| public boolean send(String message) { | ||
| this.message = message; | ||
| return true; | ||
| } | ||
|
|
||
| public boolean sendFile(File file){ | ||
| this.sentFile = file; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void receive(Consumer<NtfyMessageDto> messageHandler) { | ||
| this.handler = messageHandler; | ||
| } | ||
|
|
||
| public void simulateIncoming(NtfyMessageDto message){ | ||
| if (handler !=null){ | ||
| handler.accept(message); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback bypasses JavaFX thread safety and can cause concurrency issues.
The catch block that directly adds messages to the
ObservableListviolates JavaFX threading requirements.ObservableListmodifications must occur on the JavaFX Application Thread, but this fallback executes on whatever thread invokes the handler (likely a background HTTP thread fromNtfyConnectionImpl.receive).If
Platform.runLaterthrowsIllegalStateException(JavaFX toolkit not initialized), the correct approach is to either:HelloModel, orThe current fallback can cause
ConcurrentModificationExceptionor corrupt the observable list if UI components are bound to it.Consider removing the catch block entirely and ensuring JavaFX is properly initialized:
🤖 Prompt for AI Agents