gpt4 book ai didi

java - 如何设置 Scenario 对象中属性的默认值(未调用构造函数!)

转载 作者:行者123 更新时间:2023-12-01 10:09:08 25 4
gpt4 key购买 nike

考虑以下因素:

@Given("^this stuff:$")
public void this_stuff(List<ScenarioStuff> stuffList) throws Throwable {
stuffList.get(0).isHappy();
}

和功能:

Given this stuff:
|Name|
|Miguel|

最后是场景对象:

ScenarioStuff{
private String name;
private boolean happy;
(getters and setters for name and happy, inlcuding:)
public boolean isHappy(){
return happy;
}

这是我发现的:

  • stuffList.get(0).isHappy();是假的;
  • 即使private boolean happy=true;它仍然是假的
  • 对于 ScenarioStuff(){ happy=true) 的默认构造函数,它仍然为 false
  • 代码不会因该构造函数中的断点而中断。

问题:

如果功能表中未将 happy=true 作为参数提供,我如何默认它?

最佳答案

cucumber 用途XStream将步骤定义参数转换为 Java 对象,因此要回答这个问题,我们必须深入研究 XStream的方法来做到这一点。

this answer to an XStream question 中所述,一种选择是使用 readResolve 方法(XStream 显然会使用该方法(如果可用)来设置对象。

在我的特定情况下,将 happy 变量从 boolean 更改为 Boolean 后,我最终得到了如下实现:

ScenarioStuff{
private String Name;
private Boolean happy;

private Object readResolve() {
if(happy == null){
happy = true;
}
return this;
}
}

我还读到可以实现一个转换器来编码/解码对象,但我没有探索这个选项,因为 1) 它似乎不太简单,2) 我没有立即看到如何注册这个新转换器在 cucumber 设置中。

关于java - 如何设置 Scenario 对象中属性的默认值(未调用构造函数!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36258229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com