【发布时间】: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