Skip to content

Commit fe6695a

Browse files
authored
fix(mocklib): fixed false positive file existence checks for mocks in mockLib (#138)
- While recording mocks using mockLib, it was showing that mock already exists since functionality was checking only folder name and not the actual file in the path. Signed-off-by: gouravkrosx <gouravgreatkr@gmail.com>
1 parent 93b0771 commit fe6695a

File tree

3 files changed

+89
-57
lines changed

3 files changed

+89
-57
lines changed

README.md

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
[//]: # ([![Maven Central]&#40;https://img.shields.io/maven-central/v/io.keploy/keploy-sdk/1.2.6.svg?label=Maven%20Central&#41;]&#40;https://search.maven.org/search?q=g:%22io.keploy%22%20AND%20a:%22keploy-sdk%22%20AND%20v:%221.2.6%22&#41;)
77

8-
98
# Keploy
109

1110
[Keploy](https://keploy.io) is a no-code testing platform that generates tests from API calls.
1211

1312
This is the client SDK for Keploy API testing platform. There are 2 modes:
13+
1414
1. **Record mode**
1515
1. Record requests, response and all external calls and sends to Keploy server.
1616
2. It runs the request on the API again to identify noisy fields.
@@ -20,7 +20,6 @@ This is the client SDK for Keploy API testing platform. There are 2 modes:
2020
2. Calls the API with same request payload in testcase.
2121
3. Validates the respones and uploads results to the keploy server
2222

23-
2423
The Keploy Java SDK helps you to integrate keploy with java applications. (For other languages,
2524
see [KEPLOY SDKs](https://docs.keploy.io/application-development))
2625

@@ -53,6 +52,17 @@ or to *build.gradle*:
5352

5453
implementation 'io.keploy:keploy-sdk:N.N.N' (eg: 1.2.8)
5554

55+
### KEPLOY_MODE
56+
57+
There are 3 modes:
58+
59+
- **Record**: Sets to record mode.
60+
- **Test**: Sets to test mode.
61+
- **Off**: Turns off all the functionality provided by the API
62+
63+
**Note:** `KEPLOY_MODE` value is case sensitive.
64+
65+
5666
## Usage
5767

5868
- **Start keploy server [refer](https://github.com/keploy/keploy#start-keploy-server)**
@@ -77,11 +87,12 @@ or to *build.gradle*:
7787
- Download the latest - Download the latest agent jar
7888
from [here](https://search.maven.org/artifact/io.keploy/keploy-sdk/1.2.8/jar) (eg: 1.2.8)
7989

80-
- Prefix `-javaagent:` with absolute classpath of agent jar (eg: `-javaagent:<your full path to agent jar>/agent-1.2.8.jar`).
90+
- Prefix `-javaagent:` with absolute classpath of agent jar (
91+
eg: `-javaagent:<your full path to agent jar>/agent-1.2.8.jar`).
8192

82-
1. **Using Intellij :** Go to Edit Configuration-> add VM options -> paste _java agent_ edited above.
93+
1. **Using Intellij:** Go to Edit Configuration-> add VM options -> paste _java agent_ edited above.
8394

84-
2. **Using Command Line :**
95+
2. **Using command line:**
8596
- First add below plugins in your *pom.xml* file.
8697
```xml
8798
<plugin>
@@ -114,8 +125,11 @@ or to *build.gradle*:
114125
</configuration>
115126
</plugin>
116127
```
117-
- And then run this command:`java -javaagent:<your full path to agent jar>/agent-1.2.8.jar -jar <your full path to application jar>.jar`. This command will attach agent jar and also run the application. You need to set some required env variables written below in order to generate test cases. So, run this command after setting the env variables.
118-
128+
- And then run this
129+
command:`java -javaagent:<your full path to agent jar>/agent-1.2.8.jar -jar <your full path to application jar>.jar`
130+
. This command will attach agent jar and also run the application. You need to set some required env
131+
variables written below in order to generate test cases. So, run this command after setting the env
132+
variables.
119133

120134

121135
- **Configure Environment Variables**
@@ -130,11 +144,11 @@ or to *build.gradle*:
130144
- `DENOISE` (default DENOISE = false)
131145
**Note:** By enabling denoise, it will filter out noisy fields for the testcase.
132146
- `RUN_TEST_BEFORE_RECORD` (default RUN_TEST_BEFORE_RECORD = false)
133-
**Note:** It is used to maintain the same database state.
147+
**Note:** It is used to maintain the same database state when mocking is disabled.
134148

135149

136150
- **Generate testcases**
137-
- To generate/capture TestCases set and run your application.
151+
- To generate/capture TestCases set and run your application.
138152
1. Set `KEPLOY_MODE = record` (default "off")
139153
2. Run your application.
140154
3. Make some API calls.
@@ -143,51 +157,57 @@ or to *build.gradle*:
143157
- **Note:** Before running tests stop the sample application.
144158

145159
- Set `KEPLOY_MODE = test` (default "off")
146-
- Using IDE
160+
- **Using IDE:** _(for local use-case we prefer running tests via IDE)_
147161
1. Run your application.
148162
2. You can also run the application with coverage to see the test coverage.
149163

150-
- Using command line
151-
1. Add below code in your testfile and run `mvn test -DargLine="-javaagent:<your full path to agent jar>.jar"`.
152-
153-
```java
154-
@Test
155-
public void TestKeploy() throws InterruptedException {
164+
- If you want to run keploy tests along with other unit testcases. You will be required to set the `javaagent` again in your test profile just like below.
156165

157-
CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch();
158-
Mode.setTestMode();
166+
![run_configuration](./src/main/resources/Run_Configuration.png "Run_Configuration")
167+
168+
1. Add below code in your testfile and run it with or without coverage.
159169

160-
new Thread(() -> {
161-
<Your Application Class>.main(new String[]{""});
162-
countDownLatch.countDown();
163-
}).start();
170+
```java
171+
@Test
172+
public void TestKeploy() throws InterruptedException {
164173

165-
countDownLatch.await();
166-
assertTrue(AssertKTests.result(), "Keploy Test Result");
167-
}
168-
```
174+
CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch();
175+
Mode.setTestMode();
169176

170-
2. To get test coverage, in addition to above follow below instructions.
177+
new Thread(() -> {
178+
<Your Application Class>.main(new String[]{""});
179+
countDownLatch.countDown();
180+
}).start();
171181

172-
3. Add maven-surefire-plugin to your *pom.xml*.
182+
countDownLatch.await();
183+
assertTrue(AssertKTests.result(), "Keploy Test Result");
184+
}
185+
```
186+
187+
2. **Using command line**
188+
- Add maven-surefire-plugin to your *pom.xml*. In `<argLine> </argLine>` don't add jacoco agent if you don't want coverage report.
173189

174190
```xml
175-
<plugin>
176-
<groupId>org.apache.maven.plugins</groupId>
177-
<artifactId>maven-surefire-plugin</artifactId>
178-
<version>2.22.2</version>
179-
<configuration>
191+
<plugin>
192+
<groupId>org.apache.maven.plugins</groupId>
193+
<artifactId>maven-surefire-plugin</artifactId>
194+
<version>2.22.2</version>
195+
<configuration>
180196

181-
<!-- <skipTests>true</skipTests> -->
197+
<!-- <skipTests>true</skipTests> -->
198+
<argLine>
199+
-javaagent:<your full path to agent jar>.jar
200+
-javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/0.8.7/org.jacoco.agent-0.8.7-runtime.jar=destfile=target/jacoco.exec-->
201+
</argLine>
182202

183-
<systemPropertyVariables>
184-
<jacoco-agent.destfile>target/jacoco.exec
185-
</jacoco-agent.destfile>
186-
</systemPropertyVariables>
187-
</configuration>
188-
</plugin>
203+
<systemPropertyVariables>
204+
<jacoco-agent.destfile>target/jacoco.exec
205+
</jacoco-agent.destfile>
206+
</systemPropertyVariables>
207+
</configuration>
208+
</plugin>
189209
```
190-
4. Add Jacoco plugin to your *pom.xml*.
210+
- If you want coverage report also add Jacoco plugin to your *pom.xml*.
191211
```xml
192212
<plugin>
193213
<groupId>org.jacoco</groupId>
@@ -215,7 +235,7 @@ or to *build.gradle*:
215235
</goals>
216236
<configuration>
217237
<!-- Sets the path to the file which contains the execution data. -->
218-
238+
219239
<dataFile>target/jacoco.exec</dataFile>
220240
<!-- Sets the output directory for the code coverage report. -->
221241
<outputDirectory>target/my-reports</outputDirectory>
@@ -224,18 +244,31 @@ or to *build.gradle*:
224244
</executions>
225245
</plugin>
226246
```
227-
5. Run your tests using command : `mvn test -DargLine="-javaagent:<your full path to agent jar>.jar"
228-
`.
229-
230-
231-
### KEPLOY_MODE
232-
There are 3 modes:
233-
- **Record**: Sets to record mode.
234-
- **Test**: Sets to test mode.
235-
- **Off**: Turns off all the functionality provided by the API
236-
237-
**Note:** `KEPLOY_MODE` value is case sensitive.
238-
247+
- Run your tests using command : `mvn test`.
248+
249+
## Want stubs for unit test cases as well?
250+
- Java-sdk also supports mocking feature for unit testcase, where you write your own unit test cases and use keploy generated mocks as stubs.
251+
252+
### Usage
253+
- Set `javaagent` in your unit test file configuration.
254+
- You just need to set the name of the mock as shown below.
255+
```java
256+
@Test
257+
public void testHttpCall() throws Exception {
258+
new MockLib("okhttpCall"); //setting name of the mock
259+
``` your unit test case code goes here ```
260+
}
261+
```
262+
- You can also provide location where your mocks can be stored using `KEPLOY_MOCK_PATH`. (default **/src/test/e2e/mocks** directory of your application)
263+
264+
265+
- **Generate mocks**
266+
1. Record mocks by setting `KEPLOY_MODE=record` and run your test file.
267+
2. You will be able to see _editable_ and _readable_ mocks at your provided location.
268+
- **Run your unit testcases**
269+
1. Just set `KEPLOY_MODE=test`, run your test file and you are good to go.
270+
271+
#### 🤩 See, you didn't even need to create a stub for your unit test cases, it's all generated using the java-sdk mock library.
239272

240273
## Community support
241274

api/src/main/java/io/keploy/service/mock/MockLib.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public Kcontext NewContext() {
101101
}
102102
System.out.println(name + " -=-==-=-=-= " + mode.value);
103103
logger.info("Keploy created new mocking context in {} mode {}.If you dont see any logs about your dependencies below, your dependency/s are NOT wrapped.", mode, name);
104-
boolean exists = StartRecordingMocks(kctx, mpath, mode.value, name, Config.Overwrite);
104+
boolean exists = StartRecordingMocks(mpath + "/" + name + ".yaml", mode.value, name, Config.Overwrite);
105105
if (exists && !Config.Overwrite) {
106106
logger.error(" Keploy failed to record dependencies because yaml file already exists {} in directory: {}.", name, mpath);
107107
Config.MockId.put(name, true);
@@ -111,7 +111,7 @@ public Kcontext NewContext() {
111111
}
112112

113113

114-
public static boolean StartRecordingMocks(Kcontext kctx, String path, String mode, String name, Boolean overWrite) {
114+
public static boolean StartRecordingMocks(String path, String mode, String name, Boolean overWrite) {
115115
Service.StartMockReq startMockReq = Service.StartMockReq.newBuilder().setMode(mode).setPath(path).setName(name).setOverWrite(overWrite).build();
116116
Service.StartMockResp startMockResp = blockingStub.startMocking(startMockReq);
117117
if (startMockResp == null) { // TODO - check how to handle this error
@@ -155,5 +155,4 @@ public static boolean PutMock(String path, Service.Mock mock) {
155155
}
156156

157157

158-
159158
}
184 KB
Loading

0 commit comments

Comments
 (0)