使用 Spring Boot DI 提升您的空手道测试水平

使用 spring boot di 提升您的空手道测试水平

几年来,我一直使用 cucumber 进行更高级别的测试,最近才开始使用空手道。虽然 cucumber 是一个很棒的工具,但我认为空手道真正的亮点在于减少了步骤定义带来的样板文件,并使快速编写有意义的测试变得容易,尤其是在 api 测试方面。

对于简单的应用程序,用纯 javascript 编写功能文件就足够了。随着应用程序和测试的增长,重用一些 java 代码可能会变得有价值。 spring boot api 可以从空手道测试中受益匪浅,但是如何在空手道测试中直接利用 spring boot 的强大功能呢?

一些示例用例

虽然 karate 支持通过 karate-config.js 文件进行配置,但有些人可能喜欢通过 spring yaml/properties 进行配置。这对于重建代码之外的重新配置也很有帮助。在应用程序和测试之间同步某些 spring boot 配置属性。验证 api 调用之间的数据库状态。 jpa 存储库/实体 bean 可以在空手道测试中使用。一些 spring beans 在测试中可能非常有用。

如何将spring融入空手道

完整示例项目:https://github.com/trey-pero/karate-spring

空手道可以通过简单的 junit 测试来执行。要开始连接 spring,请将 junit 测试设置为 @springboottest。

@requiredargsconstructor@springboottest(classes = main.class)public class karatetest {    private final applicationcontext applicationcontext;    @test    void test() {        applicationcontextholder.setapplicationcontext(this.applicationcontext);        // since this one junit test runs all karate tests,        // fail the test if any underlying karate tests fail        assertequals(0, runner.path("classpath:org/tpero")                .parallel(optional.ofnullable(system.getproperty("karate.threads"))                        .map(integer::parseint)                        .orelse(5)                ).getfailcount());    }}

登录后复制

为了访问 spring 上下文(提供对所有 bean 和配置的访问),它需要存储在 karate 可以静态访问的地方。

/** * provides karate static access to the spring application context. */@utilityclasspublic class applicationcontextholder {    @setter    @getter    private applicationcontext applicationcontext;}

登录后复制

从 karate 配置中,可以访问静态持有者,以使用以下示例将应用程序上下文连接到 karate 的全局配置映射中:

/** * define common feature file configuration here. * @returns common configuration as a json object. */function getconfig() {    // global values    const appcontext = java.type("org.tpero.applicationcontextholder")        .getapplicationcontext()    const environment = appcontext.getenvironment()    return {        appcontext: appcontext,        environment: environment,        baseurl: `http://localhost:${environment.getproperty('app.server.port', '8080')}`    }}

登录后复制

使用上述设置代码,可以从 karate 功能文件访问 beans 和配置,如本示例所示,该示例测试返回 jwt 令牌的简单登录 api。

Feature: Login  Background:    * url baseUrl    * path '/login'    # Load the JWT service bean from Spring DI    * def jwtService = appContext.getBean('jwtService')  Scenario: Login with valid credentials    Given request { username: 'user', password: 'password' }    When method post    Then status 200    * print response    # Use the JWT service bean to decode the JWT from the response    * def decodedJwt = jwtService.decode(response)    * print decodedJwt    * def decodedBody = decodedJwt.getBody()    * print decodedBody    And match decodedBody['sub'] == 'user'    * def issuedAt = Number(decodedBody['iat'])    # Ensure the issuedAt is in the past    And assert issuedAt < Java.type('java.lang.System').currentTimeMillis()    * def configuredExpirationInMinutes = Number(environment.getProperty('jwt.expiration.ms')) / 1000    # Ensure the expiration is the configurable amount of minutes beyond the issuedAt    And match Number(decodedBody['exp']) == issuedAt + configuredExpirationInMinutes

登录后复制

此示例演示了将 spring boot 的强大功能集成到 karate 中以构建功能更强大的测试套件是多么容易。

以上就是使用 Spring Boot DI 提升您的空手道测试水平的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2611805.html

(0)
上一篇 2025年3月6日 21:24:02
下一篇 2025年3月6日 21:24:08

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论