java .before_java – 用于配置@BeforeClass和@Before方法的模式

我现在有一种情况,我有类似以下的东西:

public class SomeTest extends AbstractMyTest {

@Test

public void something() {

//Test something, related to the AbstractMyTest config

}

@Override

public String getConfiguration() {

return "myConfigFile.ini";

}

}

public abstract class AbstractMyTest {

@Before

public void before() {

//Do some init stuff that calls getConfiguration()...

}

abstract String getConfiguration();

}

现在,我正在考虑摆脱AbstractMyTest类,并具有如下内容:

@MyTestConfig(value="myConfigFile.ini")

public class SomeTest {

@Test

public void something() {

//Test something, related to the AbstractMyTest config

}

}

所以我可能有一个自定义Runner来完成AbstractMyTest类负责的工作.我希望能够以@BeforeClass或@Before的方式做一些事情,而不必在每个TestClass中都这样做.如何组织这样的跑步者?

解决方法:

我不太明白为什么你需要一个自定义的Runner.

看起来你可以使用Rules完全按照自己的意愿行事.特别是,看看ExternalResource rule:

public class SomeTest {

@Rule

public MyTestRule myRule = new MyTestRule("myConfigFile.ini");

...

}

public class MyTestRule extends ExternalResource {

...

protected void before() { ... }

...

}

标签:java,junit4

来源: https://codeday.me/bug/20190703/1364515.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值