Skip to main content
Version: 1.0.0

Replay Tests & Mocks for Java

To run KTests and KMocks you can follow any one of these methods:

Method 1

Set Environment Variable `KEPLOY_MODE`
export KEPLOY_MODE="test"

Run application and find test report summary on the Keploy Server Logs and detailed test report in directory where Keploy Server is running.

Test report summary

Method 2 [preferred]

Testing using Unit Test File
  • Add below code in your unit testfile, say, Sample_Test.java in test/java directory.
          @Test
          public void TestKeploy() throws InterruptedException {

             CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch();
             mode.setTestMode();
             new Thread(() -> {
                 SamplesJavaApplication.main(new String[]{""});
                 countDownLatch.countDown();
             }).start();

             countDownLatch.await();
          }
  • Run mvn test

Method 3

Get Test-Coverage with Surgefire

To get test coverage, in addition to above Method-2 follow below instructions

  • Add maven-surefire-plugin to your pom.xml.
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>

            <!-- <skipTests>true</skipTests> -->

                    <systemPropertyVariables>
                        <jacoco-agent.destfile>target/jacoco.exec
                        </jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
  • Add Jacoco plugin to your pom.xml.
            <plugin>
               <groupId>org.jacoco</groupId>
               <artifactId>jacoco-maven-plugin</artifactId>
               <version>0.8.5</version>
               <executions>
                   <execution>
                       <id>prepare-agent</id>
                       <goals>
                           <goal>prepare-agent</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>report</id>
                       <phase>prepare-package</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>post-unit-test</id>
                       <phase>test</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                       <configuration>
                           <!-- Sets the path to the file which contains the execution data. -->

                           <dataFile>target/jacoco.exec</dataFile>
                           <!-- Sets the output directory for the code coverage report. -->
                           <outputDirectory>target/my-reports</outputDirectory>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
  • Run your tests using command : mvn test.

Method 4

Run Tests in CI/CD

After following METHOD 2 above ^, Keploy will be integrated to junit. If you already have junit no changes are required in the CI/CD pipeline.