[英]How to make stateful JBehave steps safe for multithreaded execution
目前,我正在使用Spring將一個步驟類注入到JUnit測試中:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration...
class MyTest extends JUnitStories {
@Autowired private MySteps mySteps;
public List<CandidateSteps> candidateSteps() {
return new InstanceStepsFactory(configuration(), mySteps).createCandidateSteps();
}
...
}
我想用多線程運行JBehave ,所以我不能再注入一個單獨的MySteps bean了。
我已經閱讀了http://jira.codehaus.org/browse/JBEHAVE-492,但仍然無法看到如何解決我的問題。
這是很久以前的問題,但答案是:只在ThreadLocal變量中保持狀態,或者更好地將ThreadLocal變量包裝在您自己的一個類中。 例如,請使用此代碼
國家階級
public class State {
private ThreadLocal<String> state;
public void setState(String state) {
this.state.set(state);
}
public String getState() {
return this.state.get();
}
}
和步驟
public class Steps {
private State state;
public Steps(State state) {
this.state = state;
}
@Given("the following happens: $string")
public void somethingHappens(String string) {
state.setState(string);
}
@When("who knows")
public void whoKnows() {
String stat = state.getState();
}
@BeforeScenario
public void cleanup() {
state.setState(null);
}
}
我建議你在方案清除變量之前要有一些東西,以防止不同場景之間的任何泄漏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.