自动填充 JUnit 设置和拆卸
Automatically populate JUnit setup and teardown
对于我为我的应用程序所做的大部分测试,我有一个通用的设置和拆卸。
当我创建一个新的 JUnit 测试用例时 class 我可以选择让 eclipse 自动创建 setUp() 和 tearDown() 方法。
有没有办法更改默认的 setUp 和 TearDown 方法以具有一些自定义代码行?
因此,当我创建一个新测试 class 并选中 setUp 方法时,我得到:
@Before
public void setUp() throws Exception
{
UnitTestSetup.standardSetup();
}
导入应该不是问题,因为它会在保存时自动导入所需的包。
为什么不创建一个 class 来执行代码来进行设置,另一个 class 来进行拆卸并使用那些 classes 来封装所需的逻辑分别进行设置和拆除。然后,您可以随时随地调用此逻辑。
像这样:
企业class:
package com.foo.bar.businesslogic.woozinator;
public class Woozinator {
public void doSomething() {
System.out.println("Doing something.");
System.out.println("Done.");
}
}
测试class:
package com.foo.bar.businesslogic.woozinator;
import org.junit.Test;
import com.foo.bar.businesslogic.testutil.WidgitUserTestSetupUtil;
import com.foo.bar.businesslogic.testutil.WidgitUserTestTearDownUtil;
public class WoozinatorTest {
@Test
public void shouldPassTest() {
WidgitUserTestSetupUtil.setUp();
// do test stuff here
WidgitUserTestTearDownUtil.tearDown();
}
}
设置 class:
package com.foo.bar.businesslogic.testutil;
public class WidgitUserTestSetupUtil {
public static void setUp() {
System.out.println("Setting up widget environment for widget user test");
// do stuff here
System.out.println("Done setting up environment");
}
}
拆解class:
package com.foo.bar.businesslogic.testutil;
public class WidgitUserTestTearDownUtil {
public static void tearDown() {
System.out.println("Doing woozinator tear down.");
// undo stuff here
System.out.println("Done with woozinator tear down.");
}
}
如果您希望更改 Eclipse 新 JUnit 测试用例向导的输出,则不重写插件的源代码是不可能的。生成的代码是hard-coded into the wizard.
buffer.append("void "); //$NON-NLS-1$
buffer.append(methodName);
buffer.append("() throws "); //$NON-NLS-1$
buffer.append(imports.addImport("java.lang.Exception")); //$NON-NLS-1$
buffer.append(" {}"); //$NON-NLS-1$
buffer.append(delimiter);
JUnit 支持继承。将公共 @Before
和 @After
应用于测试用例的一种方法是将它们放在公共父 class 中,并在测试用例中继承:
public class Common {
@Before
public void setUp() {
...
}
@After
public void tearDown() {
...
}
}
public class MyTest extends Common {
@Test
public void test() {
// Common.setUp() will have been run before this test
// Common.tearDown() will run after the test
}
}
如果您真的认为它值得,您可以编写自己的注释处理器。然后让它处理所有具有 @Test
注释的东西,或者可能有您自己的 @VerySpecialTestWithGeneratedSetupAndTeardown
注释并将其添加到您要处理的 classes/methods。
这将是一大堆代码,因此您需要进行大量测试来证明这一点,但这里有一个关于编写注释处理器的很好的演练:http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
您可以编写扩展 TestWatcher 的 JUnit 规则并将您的代码移动到方法 starting
和 finished
.
public class YourRule extends TestWatcher {
@Override
protected void starting(Description description) {
UnitTestSetup.standardSetup();
//more of your code
}
@Override
protected void finished(Description description) {
//your code
}
}
在每个测试中使用此规则:
public class YourTest {
@Rule
public final YourRule yourRule = new YourRule();
...
}
对于我为我的应用程序所做的大部分测试,我有一个通用的设置和拆卸。
当我创建一个新的 JUnit 测试用例时 class 我可以选择让 eclipse 自动创建 setUp() 和 tearDown() 方法。
有没有办法更改默认的 setUp 和 TearDown 方法以具有一些自定义代码行?
因此,当我创建一个新测试 class 并选中 setUp 方法时,我得到:
@Before
public void setUp() throws Exception
{
UnitTestSetup.standardSetup();
}
导入应该不是问题,因为它会在保存时自动导入所需的包。
为什么不创建一个 class 来执行代码来进行设置,另一个 class 来进行拆卸并使用那些 classes 来封装所需的逻辑分别进行设置和拆除。然后,您可以随时随地调用此逻辑。
像这样:
企业class:
package com.foo.bar.businesslogic.woozinator;
public class Woozinator {
public void doSomething() {
System.out.println("Doing something.");
System.out.println("Done.");
}
}
测试class:
package com.foo.bar.businesslogic.woozinator;
import org.junit.Test;
import com.foo.bar.businesslogic.testutil.WidgitUserTestSetupUtil;
import com.foo.bar.businesslogic.testutil.WidgitUserTestTearDownUtil;
public class WoozinatorTest {
@Test
public void shouldPassTest() {
WidgitUserTestSetupUtil.setUp();
// do test stuff here
WidgitUserTestTearDownUtil.tearDown();
}
}
设置 class:
package com.foo.bar.businesslogic.testutil;
public class WidgitUserTestSetupUtil {
public static void setUp() {
System.out.println("Setting up widget environment for widget user test");
// do stuff here
System.out.println("Done setting up environment");
}
}
拆解class:
package com.foo.bar.businesslogic.testutil;
public class WidgitUserTestTearDownUtil {
public static void tearDown() {
System.out.println("Doing woozinator tear down.");
// undo stuff here
System.out.println("Done with woozinator tear down.");
}
}
如果您希望更改 Eclipse 新 JUnit 测试用例向导的输出,则不重写插件的源代码是不可能的。生成的代码是hard-coded into the wizard.
buffer.append("void "); //$NON-NLS-1$
buffer.append(methodName);
buffer.append("() throws "); //$NON-NLS-1$
buffer.append(imports.addImport("java.lang.Exception")); //$NON-NLS-1$
buffer.append(" {}"); //$NON-NLS-1$
buffer.append(delimiter);
JUnit 支持继承。将公共 @Before
和 @After
应用于测试用例的一种方法是将它们放在公共父 class 中,并在测试用例中继承:
public class Common {
@Before
public void setUp() {
...
}
@After
public void tearDown() {
...
}
}
public class MyTest extends Common {
@Test
public void test() {
// Common.setUp() will have been run before this test
// Common.tearDown() will run after the test
}
}
如果您真的认为它值得,您可以编写自己的注释处理器。然后让它处理所有具有 @Test
注释的东西,或者可能有您自己的 @VerySpecialTestWithGeneratedSetupAndTeardown
注释并将其添加到您要处理的 classes/methods。
这将是一大堆代码,因此您需要进行大量测试来证明这一点,但这里有一个关于编写注释处理器的很好的演练:http://hannesdorfmann.com/annotation-processing/annotationprocessing101/
您可以编写扩展 TestWatcher 的 JUnit 规则并将您的代码移动到方法 starting
和 finished
.
public class YourRule extends TestWatcher {
@Override
protected void starting(Description description) {
UnitTestSetup.standardSetup();
//more of your code
}
@Override
protected void finished(Description description) {
//your code
}
}
在每个测试中使用此规则:
public class YourTest {
@Rule
public final YourRule yourRule = new YourRule();
...
}