Skip to content

Commit b44c1de

Browse files
Achanandhi-MAchanandhiSonichigo
authored andcommitted
chore: update java employee docs (#512)
* fixed:flask-mongo-quickstart * chore: update flask mongo tests docs (#509) * fixed:flask-mongo-quickstart Signed-off-by: Achanandhi <[email protected]> * chore: update flask mongo test Signed-off-by: Achanandhi <[email protected]> Signed-off-by: Achanandhi <[email protected]> --------- Signed-off-by: Achanandhi <[email protected]> Signed-off-by: Achanandhi <[email protected]> Co-authored-by: Achanandhi <[email protected]> Co-authored-by: Animesh Pathak <[email protected]> * Added docker compose support for Java employee app Signed-off-by: Achanandhi-M <[email protected]> --------- Signed-off-by: Achanandhi <[email protected]> Signed-off-by: Achanandhi <[email protected]> Signed-off-by: Achanandhi-M <[email protected]> Co-authored-by: Achanandhi <[email protected]> Co-authored-by: Animesh Pathak <[email protected]>
1 parent eee87e0 commit b44c1de

File tree

3 files changed

+134
-58
lines changed

3 files changed

+134
-58
lines changed
240 KB
Loading

β€Žversioned_docs/version-2.0.0/concepts/installation.mdβ€Ž

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,3 @@ By default, Docker Desktop may not be configured to work with all WSL 2 distros
141141
> For detailed instructions on how to configure `Docker Desktop` for WSL 2, please refer to the [official Docker documentation](https://docs.docker.com/desktop/wsl/).
142142
143143
</details>
144-
145-
<details>
146-
<summary>With Arkade</summary>
147-
148-
### With Arkade
149-
150-
1. Installing Arkade
151-
152-
```bash
153-
# Note: you can also run without `sudo` and move the binary yourself
154-
curl -sLS https://get.arkade.dev | sudo sh
155-
156-
arkade --help
157-
ark --help # a handy alias
158-
159-
# Windows users with Git Bash
160-
curl -sLS https://get.arkade.dev | sh
161-
```
162-
163-
2. Install Keploy
164-
165-
```bash
166-
arkade get keploy
167-
```
168-
169-
Or you can also download specific version of Keploy using the following command:
170-
171-
```bash
172-
173-
```
174-
175-
</details>

β€Žversioned_docs/version-2.0.0/quickstart/samples-java.mdβ€Ž

Lines changed: 134 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ keyword:
2929
A sample Employee-Manager app to test Keploy integration capabilities using **SpringBoot**
3030
and **PostgreSQL**.
3131

32-
> This sample application is **not written for macOS users** since this application doesn't have a docker file yet.
32+
> If you are **macOS users** please try the application using docker compose.
3333
3434
import InstallationGuide from '../concepts/installation.md'
3535

@@ -49,23 +49,50 @@ git clone https://github.com/keploy/samples-java && cd samples-java/employee-man
4949
mvn clean install -Dmaven.test.skip=true
5050
```
5151

52+
You can start the backend using Keploy in 2 ways:
53+
54+
- [Using Keploy's binary](#instructions-for-starting-using-binary)
55+
- [Using Keploy's docker image](#instructions-for-starting-using-docker)
56+
57+
# Instructions For Starting Using Binary
58+
59+
## Setup the backend
60+
61+
You need to update the postgresql properties, go to
62+
`spring-petclinic/spring-petclinic-rest/src/main/resources/application-postgresql.properties`
63+
and change
64+
65+
```bash
66+
spring.datasource.url=jdbc:postgresql://postgres:5432/keploy-test/
67+
```
68+
69+
to
70+
71+
```bash
72+
spring.datasource.url=jdbc:postgresql://localhost:5432/keploy-test/
73+
```
74+
75+
and then build the jar using:
76+
77+
```bash
78+
mvn clean install -Dmaven.test.skip=true
79+
```
80+
5281
## Start the Postgres DB 🐳
5382

5483
```bash
55-
docker compose up -d
84+
docker run -e POSTGRES_USER=keploy-user -e POSTGRES_PASSWORD=keploy -e POSTGRES_DB=postgres -p 5432:5432 --name postgres postgres:15.2
5685
```
5786

5887
Note: You may have to use sudo if you are not part of the docker group.
5988

6089
### Capture the testcases 🎬
6190

62-
Once we have our jar file ready,this command will start the recording of API calls using ebpf:-
63-
6491
```bash
6592
keploy record -c "java -jar target/springbootapp-0.0.1-SNAPSHOT.jar"
6693
```
6794

68-
![Testcases](https://github.com/keploy/samples-java/blob/main/employee-manager/img/test-cases.png?raw=true)
95+
![Testcases](https://github.com/keploy/samples-java/blob/main/employee-manager/img/keploy-record-docker.png)
6996

7097
Now let's run a few tests to capture some more scenarios:
7198

@@ -131,10 +158,10 @@ keploy test -c "java -jar target/springbootapp-0.0.1-SNAPSHOT.jar" --delay 10
131158
This will run the testcases and generate the report in `keploy/testReports` folder. You will see the following output:-
132159

133160
````shell
134-
🐰 Keploy: 2024-02-20T13:49:20Z INFO starting test for of {"test case": "test-1", "test set": "test-set-1"}
135-
2024-02-20 13:49:20.778 INFO 18888 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
136-
2024-02-20 13:49:20.778 INFO 18888 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
137-
2024-02-20 13:49:20.779 INFO 18888 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
161+
🐰 Keploy: 2025-04-17T13:30:11+05:30 INFO starting test for of {"test case": "[test-1]", "test set": "[test-set-0]"}
162+
2025-04-17 13:30:11.410 INFO 28035 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
163+
2025-04-17 13:30:11.410 INFO 28035 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
164+
2025-04-17 13:30:11.410 INFO 28035 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
138165
Testrun failed for testcase with id: "test-1"
139166

140167
--------------------------------------------------------------------
@@ -148,49 +175,130 @@ Testrun failed for testcase with id: "test-1"
148175
| |
149176
| EXPECT BODY | ACTUAL BODY |
150177
| -----------------------------------------------------+----------------------------------------------------- |
151-
| { | { |
152-
| "email": "[email protected]", | "email": "[email protected]", |
153-
| "firstName": "Myke", | "firstName": "Myke", |
154-
| "id": 1, | "id": 1, |
155-
| "lastName": "Tyson", | "lastName": "Tyson", |
156-
| - "timestamp": 1.70843653e+09 | + "timestamp": 1.70843696e+09 |
178+
| { | { |
179+
| "timestamp": "1744871332" , | "timestamp": "1744876811" , |
180+
| lastName:Tyson | lastName:Tyson |
157181
| } | } |
158182
| | |
159183
| |
160184
+-------------------------------------------------------------------------------------------------------------+
161-
🐰 Keploy: 2024-02-20T13:49:20Z INFO result {"testcase id": "test-1", "testset id": "test-set-1", "passed": "false"}
162-
🐰 Keploy: 2024-02-20T13:49:21Z INFO starting test for of {"test case": "test-2", "test set": "test-set-1"}
185+
🐰 Keploy: 2025-04-17T13:30:11+05:30 INFO result {"testcase id": "[test-1]", "testset id": "[test-set-0]", "passed": "[false]"}
186+
🐰 Keploy: 2025-04-17T13:30:11+05:30 INFO starting test for of {"test case": "[test-2]", "test set": "[test-set-0]"}
163187
Testrun passed for testcase with id: "test-2"
164188

165189
--------------------------------------------------------------------
166190

167-
🐰 Keploy: 2024-02-20T13:49:21Z INFO result {"testcase id": "test-2", "testset id": "test-set-1", "passed": "true"}
168-
🐰 Keploy: 2024-02-20T13:49:21Z INFO test report for test-set-1: {"name: ": "report-2", "path: ": "/Users/neha/open-source/samples-java/employee-manager/keploy/report-2"}
191+
🐰 Keploy: 2025-04-17T13:30:11+05:30 INFO result {"testcase id": "[test-2]", "testset id": "[test-set-0]", "passed": "[true]"}
169192

170-
<=========================================>
171-
TESTRUN SUMMARY. For testrun with id: "test-set-1"
172-
Total tests: 2
173-
Total test passed: 1
174-
Total test failed: 1
193+
<=========================================>
194+
TESTRUN SUMMARY. For test-set: "test-set-0"
195+
Total tests: 2
196+
Total test passed: 1
197+
Total test failed: 1
198+
Time Taken: "10.37 s"
199+
<=========================================>
175200
<=========================================>```
176201
````
177202
178203
Did you spot that the `timestamp` is showing some differences? Yep, time has a way of doing that! πŸ•°οΈ
179204
180205
Worry not, just add the ever-changing fields (like our **ts** here) to the **noise parameter** to **dodge those assertions**.
181206
182-
> Pro tip: Add `body.timestamp` to noise in `test-1.yaml`.
207+
> Pro tip: Add `body.timestamp` to noise in `keploy.yml`.
183208
184-
<img src="/docs/img/code-snippets/java-sample-employee-manager-noise.png" alt="Adding Noise to Test case Java Postgres Employee Manager App" width="70%" style={{ borderRadius: '5px' }}/>
209+
<img src="https://github.com/keploy/samples-java/blob/main/employee-manager/img/test-noise.png" alt="Adding Noise to Test case Java Postgres Employee Manager App" width="70%" style={{ borderRadius: '5px' }}/>
185210
186211
Run that `keploy test` command once more and watch as everything falls into place with all tests passing! 🌟
187212
188213
Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the `mocks.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold! βœ¨πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»βœ¨
189214
215+
Next we move on to the instructions to start the application using docker.
216+
217+
# Instructions For Starting Using Docker
218+
219+
Prerequisites For Docker:
220+
221+
1. Docker Desktop 4.25.2 and above
222+
223+
Here we just need to change the command used to start the application.
224+
225+
```bash
226+
keploy record -c "docker compose up" --container-name javaApp --build-delay 100
227+
```
228+
229+
<img src="https://github.com/keploy/samples-java/blob/main/employee-manager/img/Keploy-record-docker-compose.png" alt="Sample Keploy Record Java" width="100%" style={{ borderRadius: '5px' }} />
230+
231+
Now let's run a few tests to capture some more scenarios:
232+
233+
#### Generate testcases πŸ“
234+
235+
To generate testcases we just need to **make some API calls.** You can use [Postman](https://www.postman.com/)
236+
, [Hoppscotch](https://hoppscotch.io/), or simply `curl`
237+
238+
1. Make an employee entry πŸ“₯
239+
240+
```bash
241+
curl --location --request POST 'http://localhost:8080/api/employees' \
242+
--header 'Content-Type: application/json' \
243+
--data-raw '{
244+
"firstName": "Myke",
245+
"lastName": "Tyson",
246+
"email": "[email protected]",
247+
"timestamp":1
248+
}'
249+
```
250+
251+
this will return the response or an entry. The timestamp would automatically be ignored during testing because it'll
252+
always be different.
253+
254+
```bash
255+
{
256+
"id": 1,
257+
"firstName": "Myke",
258+
"lastName": "Tyson",
259+
"email": "[email protected]",
260+
"timestamp": 1661493301
261+
}
262+
```
263+
264+
2. Fetch recorded info about employees
265+
266+
```bash
267+
curl --location --request GET 'http://localhost:8080/api/employees/1'
268+
```
269+
270+
or by querying through the browser `http://localhost:8080/api/employees/1`
271+
272+
Now both these API calls were captured as **editable** testcases and written to `keploy/test` folder. The keploy
273+
directory would also have `mock.yml` file.
274+
275+
Now, let's see the magic! πŸͺ„πŸ’«
276+
277+
## Running the testcases using Keploy
278+
279+
```bash
280+
keploy test -c "docker compose up" --container-name javaApp --build-delay 50 --delay 20
281+
```
282+
283+
Your CLI should look something like this
284+
<img src="/static/img/keploy-test-docker-compose-command.png" alt="Sample Keploy Test Java" width="100%" style={{ borderRadius: '5px' }} />
285+
286+
This is a summary of the test cases recorded
287+
<img src="https://github.com/keploy/samples-java/blob/main/employee-manager/img/Keploy-test-docker-compose.png" alt="Sample Keploy Test Summary Java" width="100%" style={{ borderRadius: '5px' }} />
288+
289+
Here `delay` is the time it takes for your application to get started, after which Keploy will start running the testcases. If your application takes longer than 10s to get started, you can change the `delay` accordingly.
290+
`buildDelay` is the time that it takes for the image to get built. This is useful when you are building the docker image from your docker compose file itself.
291+
190292
### πŸŽ‰ Wrapping it up
191293
192294
Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. πŸ˜ŠπŸš€
193295
296+
### πŸš€ Wanna try Keploy in CI/CD?
297+
298+
We got you 😎
299+
Here’s how to set it up with GitHub Actions:
300+
πŸ‘‰ [Keploy + GitHub CI/CD Guide](https://keploy.io/docs/ci-cd/github)
301+
194302
Hope this helps you out, if you still have any questions, reach out to us .
195303
196304
import GetSupport from '../concepts/support.md'

0 commit comments

Comments
Β (0)