【问题标题】:org.graalvm.polyglot.PolyglotException while comparing 2 Strings in Karate framework - Java [duplicate]org.graalvm.polyglot.PolyglotException 比较空手道框架中的 2 个字符串 - Java [重复]
【发布时间】:2021-06-17 13:25:05
【问题描述】:

当我使用空手道自动化 API 时,我遇到了一个奇怪的行为,我发现 org.graalvm.polyglot.PolyglotException 在将响应与

Scenario: create a user and then get it by id
    * def user = read('resources/request.json')
    * def assertResponse = Java.type('util.AssertResponse')
    * json expectedresponse = read('resources/response.json')
    * set expectedresponse.name = 'Test'

    Given url 'https://jsonplaceholder.typicode.com/users'
    And request user
    When method post
    Then status 201
    And assert response != null
    And print assertResponse.assertStringResponse(expectedresponse, response)

   

现在我有一个这样的 java 函数定义 -

public static Boolean assertStringResponse(String expected, String actual) throws JSONException {
        JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT.LENIENT);
        return true;
    }

当我运行此功能时,它给了我以下错误 -

org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (assertStringResponse) on util.AssertResponse failed due to: Cannot convert '{address={zipcode=54321-6789, suite=Apt. 123, city=Electri, street=Has No Name}, name=Test User, id=11, email=test@user.com, username=tes...'(language: Java, type: com.intuit.karate.graal.JsMap) to Java type 'java.lang.String': Invalid or lossy primitive coercion.

你能帮我解决这个问题吗?

谢谢!

【问题讨论】:

    标签: java bdd karate


    【解决方案1】:

    因为response 是一个JSON - 当你调用Java 时它变成了Map,但是你在Java 方法中的第二个参数是一个字符串。

    在Java调用前添加这一行,参考:https://github.com/intuit/karate#type-conversion

    * string response = response
    

    我可以多说几句吗?我是 Karate 的作者,如果您使用 JSONAssert 进行验证,那么在我看来,您犯了一个 HUGE 错误。如果其他人决定这样做,请让他们知道。

    【讨论】:

    • 解决了我的问题,我必须将两者都转换为字符串,这很成功。
    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多