【问题标题】:Why does one more Scenario break Cucumber为什么还有一个场景会破坏 Cucumber
【发布时间】:2021-12-29 00:09:47
【问题描述】:

一切都很顺利,直到我将 SleepWithInterruptHandler Scenario 添加到我的 Lag.feature 文件中

Feature: Induced Lag
  There are many reasons we might want to induce lag into our code, such as for
  testing, benchmarking, experimenting, etc.

  Scenario: Minimal Duration
    Given a minimal Lag
    When I compute the minimal duration
    Then it should equal zero

  Scenario: Definite Duration
    Given a definite Lag
    When I compute the definite duration
    Then it should equal the minimum duration

  Scenario: Random Duration
    Given a random Lag
    When I compute the random duration
    Then it should not equal either the minimum or the maximum
    And it should not be outside the range

  Scenario: Sleep With Interrupt Handler
    Given a task with random Lag
    When I start it
    Then it should start normally
    And it should complete normally without interrupt

虽然其他场景继续运行良好,但在最后一个场景中,我得到了 infamous

io.cucumber.junit.platform.engine.UndefinedStepException: 
The step 'a task with random Lag' and 3 other step(s) are undefined.
You can implement these steps using the snippet(s) below:

我的步骤类在该场景中没有任何问题,谷歌搜索建议与glue 有关,但这无济于事。最后一个场景失败 在 Maven 和 IntelliJ 下。

public class SleepWithInterruptHandler implements En {

    Duration minimumDuration = Duration.ofMillis(10);
    Duration maximumDuration = Duration.ofMillis(20);

    Lag randomLag;
    Duration randomDuration;

    AtomicInteger value = new AtomicInteger();
    Lag lag = new Lag(minimumDuration, maximumDuration);
    Runnable withInterruptHandler = new LagTests.WithInterruptHandler(value, lag);
    Thread regularThread = new Thread(withInterruptHandler);

    SleepWithInterruptHandler() {

        Given("a task with random Lag", () -> {
            assertEquals(0, value.get());
        });

        When("I start it", () -> {
            regularThread.start();
        });

        Then("it should start normally", () -> {
            // Wait a little time, but not after our task ends...
            Thread.sleep(minimumDuration.dividedBy(2));
            assertEquals(1, value.get());
        });

        And("it should complete normally without interrupt", () -> {
            // Wait for our task to end...
            regularThread.join();
            assertEquals(3, value.get());
        });
    }
}


public class DefiniteDuration implements En {

    Duration minimumDuration = Duration.ofMillis(10);

    Lag definiteLag;
    Duration definiteDuration;

    public DefiniteDuration() {

        Given("a definite Lag", () -> {
            definiteLag = new Lag(minimumDuration);
        });

        When("I compute the definite duration", () -> {
            definiteDuration = definiteLag.getDuration();
        });

        Then("it should equal the minimum duration", () -> {
            assertEquals(minimumDuration, definiteDuration);
        });
    }
}

完整的项目可以在https://github.com/kolotyluk/loom-lab找到

然而,更重要的问题是,为什么 Cucumber 对 3 个场景满意而不是 4 个?

【问题讨论】:

  • 嘿,我认为现在没有人可以帮助你。您没有提供足够的信息来重现问题。看看stackoverflow.com/help/minimal-reproducible-example
  • 顺便说一句,您正在草率下结论。这不会帮助你调试。
  • 好的,我添加了更多上下文。如果这还不够,我可以添加更多。我可能会草率下结论,因为我是 Cucumber 的新手,几天前才开始使用它。
  • 你的项目不是最小的,所以很难说。但是第 3 和第 4 场景的步骤之间的区别在于 SleepWithInterruptHandler 的构造函数是不公开的。

标签: cucumber cucumber-jvm cucumber-java cucumber-junit


【解决方案1】:

好的,问题是我忘记将 SleepWithInterruptHandler 的构造函数设为 public...,非常感谢 M.P. Korstanje 的答案。

退一步说,这是我在某些软件中看到的常见问题。 诊断不符合根本原因。

io.cucumber.junit.platform.engine.UndefinedStepException: 
The step 'a task with random Lag' and 3 other step(s) are undefined.
You can implement these steps using the snippet(s) below:

后面是一堆代码示例,当我复制/粘贴它们时......也不起作用。

Korstanje 是对的,我是在草率下结论,我的结论是 Cucumber 诊断程序告诉了我一些有意义的事情,当它们不是时,它们具有误导性。我跳到的另一个结论是谷歌搜索会告诉我一些有意义的事情,但它在很大程度上也没有,因为它发现了很多涉及glue 的解决方案,而且这些搜索命中数如此之多,我得出的结论是胶水问题是臭名昭著,我得出的结论是我的问题是胶水问题。

现在,这方面可能有两种观点

  1. 说我是个白痴,会草率下结论并跛脚 没有检查我的构造函数是否也是公共的,这是错误的。
  2. 我们可以而且应该投资于更好的诊断,一旦我们 了解一个问题,尤其是根本原因,我们可以寻找这样的 问题,并使其成为诊断的一部分,或者至少有 通过多种解决方案诊断出多个问题。

再举一个例子,去年我不得不学习 Gradle。在使用了 Maven 十多年后,然后是 SBT,我发现 Gradle 的诊断与 Maven 相比非常不成熟。我花了无数个小时追查 Gradle 的问题,因为诊断非常错误和误导,而且自信如此。 SBT...好吧我不会评论...

现在,我还没有在 Kotlin 或 Scala 中尝试过,但我应该这样做,因为在这些语言中,我认为构造函数的默认值是公共的,这就是为什么我可能忘记了在 Java 中它们不是......

对肥皂盒感到抱歉,但当我意识到这个简单的问题时,我有一个 ah-ha 时刻,并且可以将它与一个更大的问题联系起来,一个更大的软件设计和实现模式,一个更大的我花费的模式这几天我的调试时间...

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多