最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Intellij + Spring + Cucumber: A method annotated with Before, After, BeforeStep or AfterStep must have one of these signa

programmeradmin1浏览0评论

Im using:

  • Intellij 2023.3.1
  • Spring Boot 3.2.4
  • Cucumber 7.15

I want to start single scenarios using this button:

However, when I do so the test always runs into the error:

    Suppressed: io.cucumber.java.InvalidMethodSignatureException: A method annotated with Before, After, BeforeStep or AfterStep must have one of these signatures:
 * public void before_or_after(io.cucumber.java.Scenario scenario)
 * public void before_or_after()
at .citrusframework.cucumber.CitrusLifecycleHooks.before(.citrusframework.cucumber.backend.Scenario)

But in fact my before method has the correct singature:

  @Before
  public void before_or_after() {
   
  }

And when I run my tests using the TestRunnerIT I wrote, the tests are all running fine:

@RunWith(Cucumber.class)
@CucumberOptions
    (
        plugin =
            {
                "pretty",
                ".citrusframework.cucumber.CitrusReporter"
            },
        features =
            {
                "classpath:gherkin"
            }
    )
public class TestRunnerIT {

}

What can I do to make running single scenarios from feature file possible? Seems like the whole spring context is missing when starting a scenario in that way.

EDIT

added pom.xml

<project xmlns=".0.0"
  xmlns:xsi=";
  xsi:schemaLocation=".0.0 .xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.project.my</groupId>
  <artifactId>test-service</artifactId>
  <version>${revision}${changelist}</version>

  <name>test-service</name>

  <properties>
    <revision>0.4.5</revision>
    <changelist></changelist>

    <!-- cucumber -->
    <cucumber.version>7.15.0</cucumber.version>

  </properties>

  <dependencies>
    <!-- Spring Dependencies Start -->
    <dependency>
      <groupId>.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Dependencies End -->

    <!-- Cucumber Dependencies Start -->
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-spring</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- Cucumber Dependencies End -->

    <!-- Lombok start -->
    <dependency>
      <groupId>.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <!-- Lombok end -->

    <!-- Test Dependencies Start -->
    <dependency>
      <groupId>.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Test Dependencies End -->
  </dependencies>

</project>

Im using:

  • Intellij 2023.3.1
  • Spring Boot 3.2.4
  • Cucumber 7.15

I want to start single scenarios using this button:

However, when I do so the test always runs into the error:

    Suppressed: io.cucumber.java.InvalidMethodSignatureException: A method annotated with Before, After, BeforeStep or AfterStep must have one of these signatures:
 * public void before_or_after(io.cucumber.java.Scenario scenario)
 * public void before_or_after()
at .citrusframework.cucumber.CitrusLifecycleHooks.before(.citrusframework.cucumber.backend.Scenario)

But in fact my before method has the correct singature:

  @Before
  public void before_or_after() {
   
  }

And when I run my tests using the TestRunnerIT I wrote, the tests are all running fine:

@RunWith(Cucumber.class)
@CucumberOptions
    (
        plugin =
            {
                "pretty",
                ".citrusframework.cucumber.CitrusReporter"
            },
        features =
            {
                "classpath:gherkin"
            }
    )
public class TestRunnerIT {

}

What can I do to make running single scenarios from feature file possible? Seems like the whole spring context is missing when starting a scenario in that way.

EDIT

added pom.xml

<project xmlns="http://maven.apache./POM/4.0.0"
  xmlns:xsi="http://www.w3./2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache./POM/4.0.0 http://maven.apache./maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.project.my</groupId>
  <artifactId>test-service</artifactId>
  <version>${revision}${changelist}</version>

  <name>test-service</name>

  <properties>
    <revision>0.4.5</revision>
    <changelist></changelist>

    <!-- cucumber -->
    <cucumber.version>7.15.0</cucumber.version>

  </properties>

  <dependencies>
    <!-- Spring Dependencies Start -->
    <dependency>
      <groupId>.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Dependencies End -->

    <!-- Cucumber Dependencies Start -->
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-spring</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- Cucumber Dependencies End -->

    <!-- Lombok start -->
    <dependency>
      <groupId>.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <!-- Lombok end -->

    <!-- Test Dependencies Start -->
    <dependency>
      <groupId>.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Test Dependencies End -->
  </dependencies>

</project>

Share Improve this question edited Apr 2 at 9:33 Mulgard asked Apr 1 at 7:21 MulgardMulgard 10.7k36 gold badges142 silver badges243 bronze badges 2
  • Let's show your pom.xml . – Vy Do Commented Apr 2 at 1:59
  • @VyDo added pom.xml – Mulgard Commented Apr 2 at 9:34
Add a comment  | 

2 Answers 2

Reset to default 0

Make sure Cucumber and Spring context are working together

@CucumberOptions(
    plugin = {
        "pretty",
        ".citrusframework.cucumber.CitrusReporter"
    },
    features = "classpath:gherkin",
    glue = "your.stepdefinitions.package" // Ensure your glue points to the correct step definitions package
)
@SpringBootTest // Ensures Spring context is loaded
public class TestRunnerIT {
}

Logging of event before, after

import io.cucumber.java.Before;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class YourStepDefinitions {

    @Autowired
    private SomeSpringBean someSpringBean; // Check if Spring dependencies can be injected

    @Before
    public void before_or_after() {
        System.out.println("Before scenario");
    }
}

Check plugin of Cucumber inside IntelliJ IDEA

Reading the stacktrace, the exception is caused by a step definition in a library.

at .citrusframework.cucumber.CitrusLifecycleHooks.before(.citrusframework.cucumber.backend.Scenario)

Citrus is not included in your pom.xml so it is likely that you've been editing the pom.xml without reloading the project in IDEA.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论