Spring Boot + Serenity + Cucumber for Rest API 集成测试

Spring Boot + Serenity + Cucumber for Rest API Integration Test

我正在使用 gradle 项目,并且我遵循了 CucumberSerenity 和 Spring 通过使用此 link 引导集成方法:https://medium.com/@manu.me/bdd-simplified-with-springboot-b56ffdcadb2b 当我 运行 gradle 任务 cucumber 时,我收到以下错误消息

            SEVERE: Exception while executing pickle
            java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: org/openqa/selenium/internal/Killable
                    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
                    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
                    at io.cucumber.core.runtime.Runtime.run(Runtime.java:108)
                    at net.serenitybdd.cucumber.cli.Main.run(Main.java:31)
                    at net.serenitybdd.cucumber.cli.Main.main(Main.java:19)
            Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/internal/Killable
                    at java.lang.ClassLoader.defineClass1(Native Method)
                    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
                    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
                    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
                    at java.net.URLClassLoader.access0(URLClassLoader.java:73)
                    at java.net.URLClassLoader.run(URLClassLoader.java:368)
                    at java.net.URLClassLoader.run(URLClassLoader.java:362)
                    at java.security.AccessController.doPrivileged(Native Method)
                    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
                    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
                    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
                    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
                    at net.thucydides.core.webdriver.SupportedWebDriver.<clinit>(SupportedWebDriver.java:31)
                    at net.thucydides.core.configuration.WebDriverConfiguration.lookupSupportedDriverTypeFor(WebDriverConfiguration.java:49)
                    at net.thucydides.core.configuration.WebDriverConfiguration.getDriverType(WebDriverConfiguration.java:33)
                    at net.thucydides.core.webdriver.SerenityWebdriverManager.getDefaultDriverType(SerenityWebdriverManager.java:178)
                    at net.thucydides.core.webdriver.SerenityWebdriverManager.getWebdriver(SerenityWebdriverManager.java:116)
                    at net.thucydides.core.webdriver.ThucydidesWebDriverSupport.getDriver(ThucydidesWebDriverSupport.java:147)
                    at net.thucydides.core.webdriver.ThucydidesWebDriverSupport.getPages(ThucydidesWebDriverSupport.java:208)
                    at net.serenitybdd.core.Serenity.getPages(Serenity.java:188)
                    at net.serenitybdd.core.Serenity.getDefaultDependencyInjectors(Serenity.java:84)
                    at net.serenitybdd.core.Serenity.getDependencyInjectors(Serenity.java:74)
                    at net.serenitybdd.core.Serenity.resetDependencyInjectors(Serenity.java:67)
                    at net.serenitybdd.core.Serenity.done(Serenity.java:167)
                    at cucumber.runtime.SerenityObjectFactory.stop(SerenityObjectFactory.java:31)
                    at io.cucumber.core.runner.Runner.disposeBackendWorlds(Runner.java:175)
                    at io.cucumber.core.runner.Runner.runPickle(Runner.java:70)
                    at io.cucumber.core.runtime.Runtime.lambda$run(Runtime.java:100)
                    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                    at io.cucumber.core.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:243)
                    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
                    at io.cucumber.core.runtime.Runtime.lambda$run(Runtime.java:100)
                    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
                    at java.util.stream.SliceOps.accept(SliceOps.java:204)
                    at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
                    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
                    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
                    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
                    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
                    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
                    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
                    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
                    at io.cucumber.core.runtime.Runtime.run(Runtime.java:101)
                    ... 2 more
            Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.internal.Killable
                    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
                    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
                    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
                    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
                    ... 46 more
            
            Aug 13, 2020 12:22:47 PM io.cucumber.core.runtime.Runtime run
            SEVERE: Exception while executing pickle
            java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: Could not initialize class net.thucydides.core.webdriver.SupportedWebDriver
                    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
                    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
                    at io.cucumber.core.runtime.Runtime.run(Runtime.java:108)
                    at net.serenitybdd.cucumber.cli.Main.run(Main.java:31)
                    at net.serenitybdd.cucumber.cli.Main.main(Main.java:19)

build.gradle

Runner.java - 我将 运行 与“CucumberWithSerenity”一起使用,并将 @ActiveProfiles 与我的其他[=17] link 一起使用=]

          import io.cucumber.junit.CucumberOptions;
          import net.serenitybdd.cucumber.CucumberWithSerenity;
          import org.junit.runner.RunWith;
          import org.springframework.test.context.ActiveProfiles;
          
          @RunWith(CucumberWithSerenity.class)
          @CucumberOptions(features="./src/applicationTest/resources/features/sample.feature", glue={"definitions"}, tags={"@test"},strict = true)
          @ActiveProfiles("test")
          public class Runner{
          }
  

定义文件

           @SpringBootTest
           public class SampleDefinitions{
           
            @Given("^user sends message\"([^\"]*)\"$")
               public void user_sends_message(String usermessage){
                   System.out.println("usermessage -->"+usermessage);
               }
           }

当 classes 在 运行 时间不可用时会发生这种情况。

您正在使用自定义源集,即 applicationTest 未正确配置 运行 cucumber 任务。

一个解决方案是更改 cucumber 任务中的 class 路径,并包括主要源集以使用 applicationTest classes

  1. 黄瓜任务
classpath = sourceSets.applicationTest.runtimeClasspath
  1. 源集
main {
    compileClasspath += applicationTest.output
    runtimeClasspath += applicationTest.output
}

这应该有效。

有这个 spring、cucumber/ghjerkin 库可以轻松测试冲刺休息 api:https://github.com/RedFroggy/spring-cucumber-rest-api