50

我有一堆扩展 TestCase 的 JUnit 3 类,并希望将它们自动迁移为带有注释的 JUnit4 测试,例如@Before, @After,@Test等。
有任何工具可以在大批量运行中执行此操作吗?

4

7 回答 7

58

在我看来,这不可能那么难。所以让我们尝试一下:

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()方法的转换。谢谢,萨瓦!

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

结论

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

于 2009-03-24T13:19:43.750 回答
37

以下是我用来执行 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

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

于 2010-12-26T19:45:10.707 回答
5

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

https://github.com/FranciscoBorges/junit3ToJunit4

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

  • 调用TestCase.setUp()TestCase.tearDown()
  • TestCase(String)在子类构造函数中调用构造函数
  • 调用TestCase.assert*移至Assert.
  • 将包名称固定junit.frameworkorg.junit
  • ETC
于 2013-05-24T09:10:08.307 回答
3

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

于 2008-11-05T10:08:07.343 回答
3

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

  • 去年,在纳什维尔的 OOPSLA 上,有一篇关于 API 迁移的论文,但可惜他们的工具似乎没有公开可用。我将提供该论文的链接,(尽管我敢说它对您没有什么用,因为它涉及大量理论):“注解重构:推断旧版应用程序的升级转换”

  • 上面,我写了“没有可用的工具(还)”,因为我的学生 Lea Hänsenberger 目前正在研究从 JUnit 4 a 到 JExample 以及从 JUnit 3 到 JUnit 4 的自动 API 迁移。请在 Twitter 上关注 JExample在她发布第一个测试版时收到通知。

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

于 2009-03-23T09:26:18.423 回答
1

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

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

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

于 2010-06-28T06:09:49.563 回答
0

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

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

  • 如果您还没有使用 Eclipse,请获取它(它是开源的)
  • 从菜单:Help > Install new software ,然后将更新站点http://dl.bintray.com/vogellacompany/junit-4-tools/粘贴到Work With字段中
  • 完成安装,如果需要重新启动。

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

在此处输入图像描述

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

于 2019-12-05T22:09:50.930 回答