Skip to content

Commit cc511cc

Browse files
authored
Merge pull request #37 from gouravkrosx/readMe_update
docs: update instructions to use java-sdk with sample application
2 parents 029b123 + 1784a2b commit cc511cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+239
-1323
lines changed

README.md

Lines changed: 110 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Example Employee-Manager App
22

3-
A sample Employee-Manager app to test Keploy integration capabilities using [SpringBoot](https://spring.io) and PostgreSQL.
3+
A sample Employee-Manager app to test Keploy integration capabilities using [SpringBoot](https://spring.io) and
4+
PostgreSQL.
45

56
## Pre-requisites
67

78
- [Java 8+](https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing)
89

9-
## Quick Installation
10+
## Quick Installation of "KEPLOY" server
1011

1112
### **MacOS**
1213

@@ -38,7 +39,6 @@ curl --silent --location "https://github.com/keploy/keploy/releases/latest/downl
3839
sudo mv /tmp/keploy /usr/local/bin && keploy
3940
```
4041

41-
The UI can be accessed at http://localhost:6789
4242

4343
</details>
4444

@@ -47,15 +47,19 @@ The UI can be accessed at http://localhost:6789
4747
<details>
4848
<summary>Windows</summary>
4949

50-
- Download the [Keploy Windows AMD64](https://github.com/keploy/keploy/releases/latest/download/keploy_windows_amd64.tar.gz), and extract the files from the zip folder.
50+
- Download
51+
the [Keploy Windows AMD64](https://github.com/keploy/keploy/releases/latest/download/keploy_windows_amd64.tar.gz), and
52+
extract the files from the zip folder.
5153
- Run the `keploy.exe` file.
5254

5355
</details>
5456

5557
<details>
5658
<summary>Windows ARM</summary>
5759

58-
- Download the [Keploy Windows ARM64](https://github.com/keploy/keploy/releases/latest/download/keploy_windows_arm64.tar.gz), and extract the files from the zip folder.
60+
- Download
61+
the [Keploy Windows ARM64](https://github.com/keploy/keploy/releases/latest/download/keploy_windows_arm64.tar.gz), and
62+
extract the files from the zip folder.
5963
- Run the `keploy.exe` file.
6064

6165
</details>
@@ -75,32 +79,10 @@ Add *keploy-sdk* as a dependency to your *pom.xml*:
7579

7680
or to *build.gradle*:
7781

78-
implementation 'io.keploy:keploy-sdk:N.N.N' (eg: 1.2.6)
82+
implementation 'io.keploy:keploy-sdk:N.N.N' (eg: 1.2.8)
7983

8084
## Usage
81-
82-
- **For Spring based application**
83-
84-
Add `@Import(KeployMiddleware.class)` below `@SpringBootApplication` in your main class.
85-
- **Run along with agent to mock external calls of your API 🤩🔥**
86-
87-
- Download the latest - Download the latest agent jar
88-
from [here](https://search.maven.org/remotecontent?filepath=io/keploy/agent/1.2.8/agent-1.2.8.jar) (eg: 1.2.8)
89-
90-
*Note: you can find all agent jars from [here](https://search.maven.org/artifact/io.keploy/agent/1.2.7/jar).*
91-
- Prefix `-javaagent:` with absolute classpath of agent jar (eg: `-javaagent:<your full path to agent jar>/agent-1.2.8.jar`) is possible through 3 ways:-
92-
93-
1. **Using Intellij :** Go to Edit Configuration-> add VM options -> paste _java agent_ edited above.
94-
2. **Using Command Line :**
95-
96-
```
97-
export JAVA_OPTS="$JAVA_OPTS -javaagent:<your full path to agent jar>/agent-1.2.8.jar"
98-
```
99-
3. **Running via Tomcat Server :**
100-
101-
```
102-
export CATALINA_OPTS="$CATALINA_OPTS -javaagent:<your full path to agent jar>/agent-1.2.8.jar"
103-
```
85+
- Refer [this](https://github.com/gouravkrosx/java-sdk#usage).
10486

10587
## Setup Employee-Manager App
10688

@@ -117,24 +99,17 @@ docker-compose up -d
11799
### Maven clean install
118100

119101
```shell
120-
mvn clean install
102+
mvn clean install -Dmaven.test.skip=true
121103
```
122104

123105
### Set KEPLOY_MODE to record
124106

125-
```
126-
To record testcases use `KEPLOY_MODE` env variable and set the same to `record` mode.
127-
```
128-
129-
### Run the application
130-
131-
```shell
132-
mvn spring-boot:run
133-
```
107+
- To record testcases use `KEPLOY_MODE` env variable and set the same to `record` mode.
134108

135109
## Generate testcases
136110

137-
To generate testcases we just need to **make some API calls.** You can use [Postman](https://www.postman.com/), [Hoppscotch](https://hoppscotch.io/), or simply `curl`
111+
To generate testcases we just need to **make some API calls.** You can use [Postman](https://www.postman.com/)
112+
, [Hoppscotch](https://hoppscotch.io/), or simply `curl`
138113

139114
### 1. Make an employee entry
140115

@@ -149,7 +124,8 @@ curl --location --request POST 'http://localhost:8080/api/employees' \
149124
}'
150125
```
151126

152-
this will return the response or an entry. The timestamp would automatically be ignored during testing because it'll always be different.
127+
this will return the response or an entry. The timestamp would automatically be ignored during testing because it'll
128+
always be different.
153129

154130
```
155131
{
@@ -169,7 +145,8 @@ curl --location --request GET 'http://localhost:8080/api/employees/1'
169145

170146
or by querying through the browser `http://localhost:8080/api/employees/1`
171147

172-
Now both these API calls were captured as **editable** testcases and written to `test/e2e/keploy-tests` folder. The keploy directory would also have `mocks` folder.
148+
Now both these API calls were captured as **editable** testcases and written to `test/e2e/keploy-tests` folder. The
149+
keploy directory would also have `mocks` folder.
173150

174151
![testcases](https://i.imgur.com/rhNndcF.png)
175152

@@ -180,100 +157,116 @@ Now, let's see the magic! 🪄💫
180157
There are 2 ways to test the application with Keploy.
181158

182159
1. [Unit Test File](/README.md#testing-using-unit-test-file)
183-
2. [KEPLOY_MODE environment variable](/README.md#testing-using-keploy_mode-env-variable)
160+
2. [Without Unit Test File](/README.md#testing-without-using-unit-test-file)
184161

185162
### **Testing using Unit Test File**
186163

187-
Now that we have our testcase captured, run the unit test file (``SampleJavaApplication_Test.java`) already present in the sample app repo.
164+
165+
- Set `KEPLOY_MODE = test` (default "off")
188166

189-
If not present, you can add ``SampleJavaApplication_Test.java`` in the test module of your sample application.
167+
_NOTE: You will be required to set the `javaagent` again in your test profile just like below._
190168

191-
```java
192-
@Test
193-
public void TestKeploy() throws InterruptedException {
194-
CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch();
195-
mode.setTestMode();
169+
![run_configuration](./src/main/resources/Run_Configuration.png "Run_Configuration")
196170

197-
new Thread(() -> {
198-
SamplesJavaApplication.main(new String[]{""});
199-
countDownLatch.countDown();
200-
}).start();
171+
- Now that we have our testcase captured, run the unit test file (`SampleJavaApplication_Test.java`) already present in
172+
the sample app repo.
201173

202-
countDownLatch.await();
203-
}
204-
205-
```
174+
- If not present, you can make and add below code in `SampleJavaApplication_Test.java` in the test module of your sample application.
206175

207-
1. To automatically download and run the captured test-cases. Let's run the test-file.
208-
2. To get test coverage, in addition to above follow below instructions.
209-
3. Add maven-surefire-plugin to your *pom.xml*.
176+
```java
177+
@Test
178+
public void TestKeploy() throws InterruptedException {
210179

211-
```xml
212-
<plugin>
213-
<groupId>org.apache.maven.plugins</groupId>
214-
<artifactId>maven-surefire-plugin</artifactId>
215-
<version>2.22.2</version>
216-
<configuration>
180+
CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch();
181+
Mode.setTestMode();
217182

218-
<!-- <skipTests>true</skipTests> -->
183+
new Thread(() -> {
184+
<Your Application Class>.main(new String[]{""});
185+
countDownLatch.countDown();
186+
}).start();
219187

220-
<systemPropertyVariables>
221-
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
222-
</systemPropertyVariables>
223-
</configuration>
224-
</plugin>
188+
countDownLatch.await();
189+
assertTrue(AssertKTests.result(), "Keploy Test Result");
190+
}
225191
```
226-
227-
4. Add Jacoco plugin to your *pom.xml*.
228-
229-
```xml
230-
<plugin>
231-
<groupId>org.jacoco</groupId>
232-
<artifactId>jacoco-maven-plugin</artifactId>
233-
<version>0.8.5</version>
234-
<executions>
235-
<execution>
236-
<id>prepare-agent</id>
237-
<goals>
192+
- **Using IDE:** _(for local use-case we prefer running tests via IDE)_
193+
1. Run your application.
194+
2. You can also run the application with coverage to see the test coverage.
195+
196+
- **Using command line**
197+
1. Add maven-surefire-plugin to your *pom.xml*. In `<argLine> </argLine>` don't add jacoco agent if you don't want coverage report.
198+
199+
```xml
200+
<plugin>
201+
<groupId>org.apache.maven.plugins</groupId>
202+
<artifactId>maven-surefire-plugin</artifactId>
203+
<version>2.22.2</version>
204+
<configuration>
205+
206+
<!-- <skipTests>true</skipTests> -->
207+
<argLine>
208+
-javaagent:<your full path to agent jar>.jar
209+
-javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/0.8.7/org.jacoco.agent-0.8.7-runtime.jar=destfile=target/jacoco.exec-->
210+
</argLine>
211+
212+
<systemPropertyVariables>
213+
<jacoco-agent.destfile>target/jacoco.exec
214+
</jacoco-agent.destfile>
215+
</systemPropertyVariables>
216+
</configuration>
217+
</plugin>
218+
```
219+
220+
2. If you want coverage report also add Jacoco plugin to your *pom.xml*.
221+
222+
```xml
223+
<plugin>
224+
<groupId>org.jacoco</groupId>
225+
<artifactId>jacoco-maven-plugin</artifactId>
226+
<version>0.8.5</version>
227+
<executions>
228+
<execution>
229+
<id>prepare-agent</id>
230+
<goals>
238231
<goal>prepare-agent</goal>
239-
</goals>
240-
</execution>
241-
<execution>
242-
<id>report</id>
243-
<phase>prepare-package</phase>
244-
<goals>
245-
<goal>report</goal>
246-
</goals>
247-
</execution>
248-
<execution>
249-
<id>post-unit-test</id>
250-
<phase>test</phase>
251-
<goals>
252-
<goal>report</goal>
253-
</goals>
254-
<configuration>
255-
<!-- Sets the path to the file which contains the execution data. -->
256-
257-
<dataFile>target/jacoco.exec</dataFile>
258-
<!-- Sets the output directory for the code coverage report. -->
259-
<outputDirectory>target/my-reports</outputDirectory>
260-
</configuration>
261-
</execution>
262-
</executions>
263-
</plugin>
264-
```
232+
</goals>
233+
</execution>
234+
<execution>
235+
<id>report</id>
236+
<phase>prepare-package</phase>
237+
<goals>
238+
<goal>report</goal>
239+
</goals>
240+
</execution>
241+
<execution>
242+
<id>post-unit-test</id>
243+
<phase>test</phase>
244+
<goals>
245+
<goal>report</goal>
246+
</goals>
247+
<configuration>
248+
<!-- Sets the path to the file which contains the execution data. -->
249+
250+
<dataFile>target/jacoco.exec</dataFile>
251+
<!-- Sets the output directory for the code coverage report. -->
252+
<outputDirectory>target/my-reports</outputDirectory>
253+
</configuration>
254+
</execution>
255+
</executions>
256+
</plugin>
257+
```
258+
3. Run your tests using command : `mvn test`.
265259

266-
5. Run your tests using command : `mvn test`.
267260

268261
It will create .html files as test-reports which can be found in your target folder !!
269262

270-
**_We got 75.3% without writing any testcases. 🎉_**
263+
**_We got 53% without writing any testcases. 🎉_**
271264

272265
Go to the Keploy Console TestRuns Page to get deeper insights on what testcases ran, what failed.
273266

274267
![testruns](https://i.imgur.com/tg6OT0n.png "Summary")
275268

276-
### **Testing using **KEPLOY_MODE** Env Variable**
269+
### **Testing without using Unit Test File**
277270

278271
To test using `KEPLOY_MODE` env variable, set the same to `test` mode.
279272

@@ -284,7 +277,7 @@ export KEPLOY_MODE=test
284277
Now simply run the application either by ide or using command:
285278

286279
```shell
287-
mvn test
280+
java -javaagent:<your full path to agent jar>.jar -jar <your full path to appliation jar>.jar
288281
```
289282

290283
Keploy will run all the captures test-cases, compare and show the results on the console.
@@ -310,11 +303,10 @@ Hibernate: select employee0_.id as id1_0_, employee0_.email as email2_0_, employ
310303
2022-08-26 14:13:19.414 INFO 11560 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
311304
```
312305
313-
> **Note** : With this method coverage will not be calculated.
314-
315306
## Let's add a Bug in the App
316307
317-
Now let's introduce a bug! Let's try changing something like adding some extra headers in controllers `./EmployeeController.java` on line 35 like :
308+
Now let's introduce a bug! Let's try changing something like adding some extra headers in
309+
controllers `./EmployeeController.java` on line 35 like :
318310
319311
```java
320312
return ResponseEntity.ok().header("MyNewHeader","abc").body(employee);

0 commit comments

Comments
 (0)