gpt4 book ai didi

java - 自动将测试从 JUnit 3 迁移到 JUnit 4 的最佳方法?

转载 作者:IT老高 更新时间:2023-10-28 20:37:19 25 4
gpt4 key购买 nike

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

最佳答案

在我看来,这不可能那么难。那么让我们试试吧:

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()方法一定有转换。谢谢,sua!

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

结论

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

关于java - 自动将测试从 JUnit 3 迁移到 JUnit 4 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/264680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com