Eleza
Eleza

Reputation: 53

Karate- db connectivity using kotlin

Our project is in kotlin and I need to set up db connectivity. But I am continuously getting error no matter what I try. Could someone please help with the steps for db connectivity using kotlin for karate test execution?

I tried creating a Java until file and calling it in feature file using

Error: type error: access to host class feature.dbUtil is not allowed.

2nd option tried: Converted the util file into kotlin but I am unsure of the syntax to call the file with kotlin.

Upvotes: 1

Views: 102

Answers (1)

AndrewL
AndrewL

Reputation: 3410

Karate feature files can call a class written in Java or Kotlin; it makes not difference to Karate because both Java and Kotlin source files get compiled into the same thing.

Your syntax appears correct, but the error handling from Karate to Java is not always great. Some things to check:

  • Java / Kotlin developers follow the convention of starting their class names with an uppercase letter - you don't or is there a typo?
  • or are you referring to a package name (which by convention start with lower case letters) - it would be wrong to reference the package.
  • Try calling not your class but some standard java utility. You could look at the Base64 example given in https://github.com/karatelabs/karate#calling-java

If this does not help look at my working code and you can spot what your problem is.

Kotlin Utility class

package clinic.resethealth.utils

object MongoUtils {

    /**
     * Queries mongoDB and returns a list of results.
     *
     * @param collectionName Name of the collection.
     * @param query          Query in JSON format.
     * @param mongoURI       MongoDB connection URI.
     * @return The results of the query as an array in JSON format.
     */
    @JvmStatic
    fun find(collectionName: String?, query: String?, mongoURI: String?): String {
        return findDocuments(collectionName, query, mongoURI).map { document: Document ->
            document.toJson(CUSTOM_JSON_WRITER)
        }.toList().toString()
    }

    private fun findDocuments(....

Feature file

Feature: Events Role Based Access

  Background:
    * def MongoUtils = Java.type('clinic.resethealth.utils.MongoUtils')

  Scenario Outline: Users should see all events
    * def rawEvents = MongoUtils.find('event', '{}', mongoURI)
...

Upvotes: 1

Related Questions