You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java执行Cucumber异常:Examples语法错误,是否为文件编码问题?

问题分析与解答

问题描述

执行Java代码时触发异常:输入多余内容'Examples:',期望{<EOF>, SCENARIO, SCENARIO_OUTLINE, TAGS, NEWLINE}。

对应的Feature文件代码如下:

@common @account_search
Feature: Account Search
  As a producer
  I want to search for an account
  So that I can review or update the account and associated policies

  @DesignatedFunction
  Scenario Outline: Search for an account with account holder's name
    Given a Personal account with "<filename>" name
    When I search for that account with the account holder's "<filename>" name
    Then the account is found
    Examples:
      |filename       |
      |createAccount  |

提问者对该框架不熟悉,仅能判断问题与Examples相关,询问该异常是否由文件编码问题导致。

解答

这个异常不是文件编码问题导致的,核心原因是Gherkin语法的缩进错误:

你的Examples块缩进级别错误——它应该和Scenario Outline处于同一缩进层级,而不是和下面的Given/When/Then步骤同级。

看你提供的代码,Examples:是和步骤(Given等)一样的缩进(多了2个空格),这会让Gherkin解析器认为Scenario Outline的步骤已经结束,接下来应该是文件结束、新的场景、标签之类的内容(也就是异常提示里的期望内容),但它却看到了不属于这个位置的Examples:,所以判定为多余内容。

修正后的正确格式应该是:

@common @account_search
Feature: Account Search
  As a producer
  I want to search for an account
  So that I can review or update the account and associated policies

  @DesignatedFunction
  Scenario Outline: Search for an account with account holder's name
    Given a Personal account with "<filename>" name
    When I search for that account with the account holder's "<filename>" name
    Then the account is found
  Examples:  # 和Scenario Outline对齐,缩进级别一致
    |filename       |
    |createAccount  |

调整Examples:的缩进,和Scenario Outline保持同一级别,就能解决这个异常。

内容的提问来源于stack exchange,提问作者user3934203

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 19:17:13