spring 为什么我的cucumber上下文配置无法识别?

idv4meu8  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(176)

我使用Sping Boot 3,我想使用JUnit/Cucumber运行测试。在执行测试时无法启动应用。
我有这个stacktrace:
io.cucumber.core.backend.CucumberBackendException:请使用一些上下文配置来注解胶水类。
举例来说:
@CucumberContextConfiguration @SpringBootTest(classes = TestConfig.class)public class CucumberSpringConfiguration { }或者:
@CucumberContextConfiguration @ContextConfiguration(.)
public class Uncategorized {
我想知道我如何才能配置它工作得很好。
下面是我的测试入口类:

  1. package com.alten.shop;
  2. import io.cucumber.java.en.Given;
  3. import io.cucumber.junit.Cucumber;
  4. import io.cucumber.junit.CucumberOptions;
  5. import io.cucumber.spring.CucumberContextConfiguration;
  6. import org.junit.Assert;
  7. import org.junit.jupiter.api.Test;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.test.context.ContextConfiguration;
  11. @RunWith(Cucumber.class)
  12. @CucumberOptions(
  13. monochrome = true,
  14. features = { "classpath:features/products.feature" },
  15. glue = { "com.alten.shop.steps" },
  16. plugin = {"pretty", "json:target/cucumber-report.json"}
  17. )
  18. @CucumberContextConfiguration
  19. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  20. public class ShopApplicationTests {
  21. }

字符串
可以识别定义步骤,但配置不正确。
我在StepDefinitions类型上没有任何注解,只有@Given@When@Then方法注解来适应功能:

  1. public class StepDefinitions {
  2. }


这是我的pom.xml:

  1. <properties>
  2. <java.version>17</java.version>
  3. <jjwt.version>0.11.5</jjwt.version>
  4. <swagger.version>2.6.0</swagger.version>
  5. <cucumber.version>7.14.1</cucumber.version>
  6. </properties>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-test</artifactId>
  11. <scope>test</scope>
  12. <exclusions>
  13. <exclusion>
  14. <groupId>org.junit.jupiter</groupId>
  15. <artifactId>junit-jupiter-engine</artifactId>
  16. </exclusion>
  17. </exclusions>
  18. </dependency>
  19. <dependency>
  20. <groupId>io.cucumber</groupId>
  21. <artifactId>cucumber-java</artifactId>
  22. <version>${cucumber.version}</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
  26. <dependency>
  27. <groupId>io.cucumber</groupId>
  28. <artifactId>cucumber-junit</artifactId>
  29. <version>${cucumber.version}</version>
  30. <scope>test</scope>
  31. </dependency>
  32. <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-spring -->
  33. <dependency>
  34. <groupId>io.cucumber</groupId>
  35. <artifactId>cucumber-spring</artifactId>
  36. <version>${cucumber.version}</version>
  37. <scope>test</scope>
  38. </dependency>
  39. <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit-platform-engine -->
  40. <dependency>
  41. <groupId>io.cucumber</groupId>
  42. <artifactId>cucumber-junit-platform-engine</artifactId>
  43. <version>${cucumber.version}</version>
  44. <scope>test</scope>
  45. </dependency>
  46. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  47. <dependency>
  48. <groupId>org.apache.httpcomponents</groupId>
  49. <artifactId>httpclient</artifactId>
  50. <version>4.5.14</version>
  51. </dependency>
  52. </dependencies>


有人能帮帮我吗?

h4cxqtbf

h4cxqtbf1#

您已经指示Cucumber在steps包中查找胶水:

  1. glue = { "com.alten.shop.steps" },

字符串
但是,用@CucumberContextConfiguration注解的类在com.alten.shop包中。
这样小 cucumber 就找不到了
您可以删除粘合(cucumber将默认为runner类的包),或者将@CucumberContextConfiguration@SpringBootTest注解移动到一个单独的类steps包。

相关问题