1

I'm trying to get test code coverage for java code using the karate test cases. I know Junit is the best approach for unit testing and code coverage but I need to do this specifically because I want to test the java code and get coverage through karate test cases. I am able to run the karate test case and get the cucumber HTML report and coverage report(provided by jacoco plugin) but karate test case coverage is not shown. I need to know if this is possible or not and if it is possible then what changes/additions do I need to make.

Karate feature file code

Feature: Test ElasticUtilsProperties
 
      Background:
        * def ElasticUtilsProperties = Java.type('com.staff.util.ElasticUtilsProperties')
        * def elasticProps = new ElasticUtilsProperties()
        
      Scenario: Get Host
          Given elasticProps.setHost("host-1")
           When def host = elasticProps.getHost()
           Then print 'host-->', host
        
      Scenario: Get Port
          Given elasticProps.setPort("host-1")
           When def port = elasticProps.getPort()
           Then print 'port-->', port
      
      Scenario: Get Scheme
          Given elasticProps.setScheme("scheme")
           When def scheme = elasticProps.getScheme()
           Then print 'scheme-->', scheme
      
      Scenario: Get trustStorePath
          Given elasticProps.setTrustStorePath("trustStorePath")
           When def trustStorePath = elasticProps.getTrustStorePath()
           Then print 'trustStorePath-->', trustStorePath
      
      Scenario: Get trustStorePass
          Given elasticProps.setTrustStorePass("trustStorePass")
           When def trustStorePass = elasticProps.getTrustStorePass()
           Then print 'trustStorePass-->', trustStorePass
      
      Scenario: Get pathPrefix
          Given elasticProps.setPathPrefix("PathPrefix")
           When def pathPrefix = elasticProps.getPathPrefix()
           Then print 'pathPrefix-->', pathPrefix
      
      Scenario: Get index
          Given elasticProps.setIndex("Index")
           When def index = elasticProps.getIndex()
           Then print 'Index-->', index
      
      Scenario: Get RestHighLevelClient
          Given elasticProps.setHost("host-1")
           When def host = elasticProps.getHost()
           Then print 'host-->', host

Java file code

import org.elasticsearch.client.RestHighLevelClient;

public class ElasticUtilsProperties {
private String host;
private int port;
private String scheme;
private String trustStorePath;
private String trustStorePass;
private String pathPrefix;
private String index;
private RestHighLevelClient restHighLevelClient;

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}

public int getPort() {
    return port;
}

public void setPort(int port) {
    this.port = port;
}

public String getScheme() {
    return scheme;
}

public void setScheme(String scheme) {
    this.scheme = scheme;
}

public String getTrustStorePath() {
    return trustStorePath;
}

public void setTrustStorePath(String trustStorePath) {
    this.trustStorePath = trustStorePath;
}

public String getTrustStorePass() {
    return trustStorePass;
}

public void setTrustStorePass(String trustStorePass) {
    this.trustStorePass = trustStorePass;
}

public String getPathPrefix() {
    return pathPrefix;
}

public void setPathPrefix(String pathPrefix) {
    this.pathPrefix = pathPrefix;
}

public String getIndex() {
    return index;
}

public void setIndex(String index) {
    this.index = index;
}

public RestHighLevelClient getRestHighLevelClient() {
    return restHighLevelClient;
}

public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
    this.restHighLevelClient = restHighLevelClient;
}
}
Rohit oswal
  • 23
  • 1
  • 7

1 Answers1

0

I don't think this is about Eclipse or JUnit - it is a Maven question.

The best reference that is available is this: https://github.com/intuit/karate/tree/master/karate-demo#code-coverage-using-jacoco

You can also try to search for other answers: https://stackoverflow.com/search?q=%5Bkarate%5D+coverage

But I think you will have to figure this out on your own. It would be good if you post your findings here for the benefit of others :)

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    I was able to get the code coverage by adding a runner in the test package and running the coverage through this runner. It gives coverage for all the karate tests for java code. I'm still working on how to get the karate and junit consolidated code coverage. – Rohit oswal Sep 27 '21 at 14:06