自动将测试从JUnit 3迁移到JUnit 4的最佳方法是什么?

时间:2008-11-05 09:32:46

标签: java junit migration

我有一堆JUnit 3类,它们扩展了TestCase,并希望自动将它们迁移到带有@Before@After@Test等注释的JUnit4测试中。登记/> 有哪些工具可以在大批量运行中执行此操作?

7 个答案:

答案 0 :(得分:54)

在我看来,它不会那么难。所以让我们试试吧:

0。进口

您需要导入三个注释:

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

完成下几项更改后,您将不需要import junit.framework.TestCase;

1。注释test*方法

所有以public void test开头的方法都必须以@Test注释开头。 使用正则表达式很容易完成这项任务。

2。注释SetUp和TearDown方法

Eclipse生成以下setUp()方法:

@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

每个字符串

的文件只删除一个
" extends TestCase"

4。删除主要方法?

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

5。将suite()方法转换为@RunWithClass

根据saua的评论,必须转换suite()方法。谢谢,saua!

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

结论

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

答案 1 :(得分:35)

以下是我用来执行furtelwart建议的实际正则表达式:

// Add @Test
Replace:
^[ \t]+(public +void +test)
With:
    @Test\n    $1
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @Test's on already @Test annotated files
Replace:
^[ \t]+@Test\n[ \t]+@Test
With:
    @Test
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all empty setUp's
Replace:
^[ \*]+((public|protected) +)?void +setUp\(\)[^\{]*\{\s*(super\.setUp\(\);)?\s*\}\n([ \t]*\n)?
With nothing
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Add @Before to all setUp's
Replace:
^([ \t]+@Override\n)?[ \t]+((public|protected) +)?(void +setUp\(\))
With:
    @Before\n    public void setUp()
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @Before's on already @Before annotated files
Replace:
^[ \t]+@Before\n[ \t]+@Before
With:
    @Before
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all empty tearDown's
Replace:
^[ \*]+((public|protected) +)?void +tearDown\(\)[^\{]*\{\s*(super\.tearDown\(\);)?\s*\}\n([ \t]*\n)?
With nothing
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Add @After to all tearDown's
Replace:
^([ \t]+@Override\n)?[ \t]+((public|protected) +)?(void +tearDown\(\))
With:
    @After\n    public void tearDown()
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @After's on already @After annotated files
Replace:
^[ \t]+@After\n[ \t]+@After
With:
    @After
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove old imports, add new imports
Replace:
^([ \t]*import[ \t]+junit\.framework\.Assert;\n)?[ \t]*import[ \t]+junit\.framework\.TestCase;
With:
import org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\nimport static org.junit.Assert.*;
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all extends TestCase
Replace:
[ \t]+extends[ \t]+TestCase[ \t]+\{
With:
 {
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java



// Look for import junit.framework;
Find:
import junit\.framework
Manually fix
Regular Expression: on
Case sensitive: on


// Look for ignored tests (FIXME, disabled, ...)
Find:
public[ \t]+void[ \t]+\w+test
Manually fix
Regular Expression: on
Case sensitive: on


// Look for dummy/empty tests
Find:
public[ \t]+void[ \t]+test[\w\d]*\(\s*\)\s*\{\s*(//[^\n]*)?\s*\}
Manually fix
Regular Expression: on
Case sensitive: on

注意:按照上面显示的顺序执行它们非常重要。

答案 2 :(得分:4)

我们正在将一个相当大的代码库迁移到JUnit4。由于这是我第二次进行迁移,所以我决定将代码保存在某处:

https://github.com/FranciscoBorges/junit3ToJunit4

它处理的角落案例比上面答案中列举的案例更多。如:

  • 致电TestCase.setUp()TestCase.tearDown()
  • 在子类构造函数
  • 中调用TestCase(String)构造函数
  • 调用移至TestCase.assert*的{​​{1}}方法。
  • 将包名称Assert修复为junit.framework

答案 3 :(得分:3)

我不知道目前会有这样做的工具 - 我希望Eclipse能够很快提供一些插件 - 但你可以找到一个简单的源代码树来探索Java类,如果你能做到这一点你只想做一个基本的转换。我必须编写类似的东西来为遗留应用程序自动生成框架测试用例,所以我已经有了相当数量的支持代码。欢迎您使用它。

答案 4 :(得分:3)

据我所知,还没有可用的迁移工具。我所知道的是:

  • 去年,在纳什维尔的OOPSLA,有一篇关于API迁移的论文,但是他们的工具似乎并不公开。我将提供论文的链接,(尽管我认为它对您来说没什么用处,因为它理论上很重):"Annotation Refactoring: Inferring Upgrade Transformations for Legacy Applications"

  • 上面,我写了“没有可用的工具(还)”,因为我的学生LeaHänsenberger目前正在开发一个auotmated API迁移,而不是onyl,JUnit 4 a到JExample,还有JUnit 3到JUnit 4请follow JExample on Twitter在发布第一个测试版时收到通知。

我希望这些信息对您有所帮助。

答案 5 :(得分:1)

好帖子。我使用Netbeans使用以下RegEx字符串进行了升级: (第一行搜索字符串,第二行替换字符串)

public void test
@Test\n    public void test

@Override\n.*protected void onSetUp
@Before\n    protected void onSetUp

@Override\n.*protected void onTearDown
@After\n    protected void onTearDown

不要忘记标记正则表达式复选框!

答案 6 :(得分:0)

如果您要自动将 Junit3测试转换为Junit4 ,则可以使用Vogella的Codemodify工具,该工具可在以下地址使用:

TL; DR的安装和使用方式如下:

然后,您可以右键单击任何Junit3测试类,然后选择 Source> Convert to JUnit4 菜单项,以将您的类转换为JUnit4。

enter image description here

该工具将自动删除TestCase父对象,过时的导入,添加 @Before @After @Test >注释等