本文介绍了自动将测试从 JUnit 3 迁移到 JUnit 4 的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆扩展 TestCase 的 JUnit 3 类,并希望自动将它们迁移为带有注释的 JUnit4 测试,例如 @Before@After、@Test 等
有什么工具可以在大批量运行中做到这一点?

I have a bunch of JUnit 3 classes which extend TestCase and would like to automatically migrate them to be JUnit4 tests with annotations such as @Before, @After, @Test, etc.
Any tool out there to do this in a big batch run?

推荐答案

在我看来,没有那么难.那就试试吧:

In my opinion, it cannot be that hard. So let's try it:

需要导入三个注解:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;`

在您进行接下来的几项更改后,您将不需要 import junit.framework.TestCase;.

After you've made the next few changes, you won't need import junit.framework.TestCase;.

所有以 public void test 开头的方法都必须以 @Test 注释开头.使用正则表达式可以轻松完成这项任务.

All methods beginning with public void test must be preceded by the @Test annotation.This task is easy with a regex.

Eclipse 生成以下 setUp() 方法:

Eclipse generates following setUp() method:

@Override
protected void setUp() throws Exception { }

必须替换为:

@Before
public void setUp() throws Exception { }

对于 tearDown() 也一样:

@Override
protected void tearDown() throws Exception { }

替换为

@After
public void tearDown() throws Exception { }

3.去掉extends TestCase

在字符串的每个文件中只删除一次

3. Get rid of extends TestCase

Remove exactly one occurence per file of the string

" extends TestCase"

4.删除主要方法?

可能有必要删除/重构将执行测试的现有主要方法.

4. Remove main methods?

Probably it's necessary to remove/refactor existing main methods that will execute the test.

根据saua的评论,肯定有suite()方法的转换.谢谢,莎阿!

According to saua's comment, there must be a conversion of the suite() method. Thanks, saua!

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestDog.class
  TestCat.class
  TestAardvark.class
})

结论

我认为,通过一组正则表达式很容易完成,即使它会杀死我的大脑;)

Conclusion

I think, it's done very easy via a set of regular expressions, even if it will kill my brain ;)

这篇关于自动将测试从 JUnit 3 迁移到 JUnit 4 的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:24