[英]Executing Initialization Code only once for multiple test classes
我正在为我的代码编写单元测试用例。 我和Junit一起使用PowerMockito。 我编写了一个初始化代码,它将处理我的应用程序中的所有初始化内容。 以下是我的代码结构的方式:
Class ServiceInitializer{
public static isInitialized = Boolean.FALSE;
public static void initialize(){
//Initilization Code Goes Here
isInitialized = Boolean.TRUE;
}
}
@RunWith(PowerMockRunner.class)
class BaseTest{
@Before
public void preTest(){
//some code
}
static{
if(!ServiceInitializer.isInitialized){
ServiceInitializer.initialize();
}
}
@After
public void postTest(){
//some code
}
}
public TestClass1 extends BaseTest{
@Test
public void testMethodA_1(){//test code}
@Test
public void testMethodA_2(){//test code}
@Test
public void testMethodA_3(){//test code}
}
public TestClass2 extends BaseTest{
@Test
public void testMethodB_1(){//test code}
@Test
public void testMethodB_2(){//test code}
@Test
public void testMethodB_3(){//test code}
}
我正在使用Junit和batchtest目标执行这些测试用例作为ant脚本,如下所示:
<junit printsummary="yes" haltonfailure="yes" showoutput="yes">
<classpath refid="JUnitTesting.classpath"/>
<batchtest haltonerror="false" haltonfailure="no" todir="${junit.output.dir}/raw">
<formatter type="plain"/>
<formatter type="xml"/>
<fileset dir="${basedir}/src">
<include name="**/Test*.java"/>
</fileset>
</batchtest>
</junit>
我能够很好地执行测试用例但它的初始化代码是在BaseTest类的静态块中编写的,这对我来说是个问题。 每当New Test Class开始执行时,“ServiceInitializer.initialize();” 每次都会被调用。 所以,如果我有10个测试类,那么这个方法被调用10次。 我希望能够控制它,这样无论我有多少个测试类,它只能执行一次。 我怎样才能做到这一点? JUnit甚至可以实现吗?
--Update--
我知道有@BeforeClass
注释可以为每个测试类执行一次代码块。 但这并不符合我的要求,因为只要JUnit运行另一个测试类,它就会调用“@BeforeClass”注释下的方法,并在那里进行初始化检查时再次运行初始化代码。
考虑在ant-junit任务中使用forkmode元素。 默认值为“perTest”:它为每个测试类创建一个新的JVM。 如果将forkmode定义为“perBatch”,则只有一个JVM用于批处理。 因此,您的ServiceInitializer类保持初始化状态。
另请参阅此处的junit任务帮助
您可以使用类规则:
public class ServiceInitializer extends ExternalResource {
public static final TestRule INSTANCE = new ServiceInitializer();
private final AtomicBoolean started = new AtomicBoolean();
@Override protected void before() throws Throwable {
if (!started.compareAndSet(false, true)) {
return;
}
// Initialization code goes here
}
@Override protected void after() {
}
}
然后,您可以使用@ClassRule
注释在测试中使用它:
@RunWith(PowerMockRunner.class)
class BaseTest{
@Rule
public PowerMockRule powerMockRule = new PowerMockRule();
@ClassRule
public static final TestRule serviceInitializer = ServiceInitializer.INSTANCE;
@Before
public final void preTest() {
// some code
}
@After
public final void postTest() {
//some code
}
}
编辑: PowerMockRunner
显然不支持@ClassRule
,因此我将测试切换为使用PowerMockRule
。 您可以从这里阅读更多相关信息。 无论如何,我个人更喜欢自定义跑步者的规则。
您可以在JUnit中使用@BeforeClass注释。
这将为测试类(BaseTest)执行一次。
你甚至不需要'public static isInitialized = Boolean.FALSE;'。 只要相信JUnit。 :)
要在所有测试类之前运行,您必须创建一个测试套件并在测试套件类中使用@ ClassRule 。
尝试@BeforeClass在每个测试之前每个类只进行一次设置。
在通过标准类加载器加载测试类之后,PowerMockRunner会安装自己的类加载器。 最好在公共静态方法中执行静态初始化,该方法由注释
@BeforeClass
BTW:除非你需要在测试类级别使用@PrepareForTest注释,否则不必使用PowerMockRunner运行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.