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

我有一堆扩展了TestCase的JUnit 3类,并希望自动将它们迁移到JUnit4testing中,使用@Before@Before@Before等注释。
任何工具在那里做大批量运行?

在我看来,这不是那么难。 那么让我们试试看:

0.import

您需要导入三个注释:

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

在你做出接下来的几个改变之后,你将不需要import junit.framework.TestCase;

1.注释test*方法

所有以public void test开头的方法都必须以@Test注释开头。 正则expression式的这个任务很简单。

2.注释SetUp和TearDown方法

Eclipse生成以下setUp()方法:

 @Override protected void setUp() throws Exception { } 

必须replace为:

 @Before public void setUp() throws Exception { } 

相同的tearDown()

 @Override protected void tearDown() throws Exception { } 

取而代之

 @After public void tearDown() throws Exception { } 

3.摆脱extends TestCase

删除每个文件的string恰好有一个出现

 " extends TestCase" 

4.删除主要方法?

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

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

根据Saua的评论,必须有suite()方法的转换。 谢谢,沙乌!

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

结论

我认为,通过一套正则expression式,它很容易做到,即使它会杀死我的大脑;)

下面是我用来执行furtelwart的build议的实际正则expression式:

 // 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 

注意:按照上面所示的顺序执行这些操作非常重要。

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

https://github.com/FranciscoBorges/junit3ToJunit4

它处理更多的angular落案件比在上面的答案中列举的案件。 如:

  • 调用TestCase.setUp()TestCase.tearDown()
  • 调用子类构造函数中的TestCase(String)构造函数
  • 调用TestCase.assert*方法移动到Assert
  • 修复软件包名称junit.frameworkorg.junit
  • 等等

我不知道有一个工具可以做到这一点 – 我希望Eclipse能够在短时间内提供一些插件 – 但是如果你只想要一个简单的源代码树,就可以开发一个Java类,做一个基本的转换。 我不得不写一些类似的东西来为遗留应用程序自动生成框架testing用例,所以我已经有了相当数量的支持代码。 欢迎您使用它。

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

  • 去年,在纳什维尔的OOPSLA上,有一篇关于API迁移的文章,但是他们的工具似乎并不公开。 我将提供这篇论文的链接(尽pipe我敢说它对你来说没什么用处,因为它相当理论): “注释重构:推断旧版应用程序的升级转换” 。

  • 在上面,我写了“没有可用的工具(还)”,因为我的学生LeaHänsenberger目前正在从JUnit 4 a到JExample,而不是从onyl,从JUnit 3到JUnit 4进行auotmated API迁移。请按照Twitter上的JExample当她发布第一个testing版时得到通知。

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

好贴。 我使用Netbeans进行升级,并使用以下RegExstring:(第一行searchstring,第二个replacestring)

 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 

不要忘记标记正则expression式checkbox!