gpt4 book ai didi

java - Junit 课前(非静态)

转载 作者:IT老高 更新时间:2023-10-28 11:50:51 27 4
gpt4 key购买 nike

是否有任何最佳实践可以让 Junit 在测试文件中执行一次函数,并且它也不应该是静态的。

喜欢非静态函数上的@BeforeClass

这是一个丑陋的解决方案:

@Before void init(){
if (init.get() == false){
init.set(true);
// do once block
}
}

这是我不想做的事情,我正在寻找一个集成的 junit 解决方案。

最佳答案

一个简单的 if 语句似乎也很有效:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class myTest {

public static boolean dbInit = false;

@Autowired
DbUtils dbUtils;

@Before
public void setUp(){

if(!dbInit){

dbUtils.dropTables();
dbUtils.createTables();
dbInit = true;

}
}

...

关于java - Junit 课前(非静态),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2825615/

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