Java JUnit:@Before 仅用于某些测试方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1548462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit: @Before only for some test methods?
提问by Nick Heiner
I have some common set up code that I've factored out to a method marked with @Before
. However, it is not necessary for all this code to run for every single test. Is there a way to mark it so the @Before
method only runs before certain tests?
我有一些常见的设置代码,我已经将它们分解为标有@Before
. 但是,没有必要为每个测试运行所有这些代码。有没有办法标记它以便该@Before
方法仅在某些测试之前运行?
采纳答案by Kirschstein
Just move out the tests that don't need the setup code into a separate test class. If you have some other code common to the tests that would be helpful to keep, move that out into a helper class.
只需将不需要设置代码的测试移到单独的测试类中即可。如果您有一些其他有助于保留的测试通用代码,请将其移到辅助类中。
回答by duffymo
回答by vtor
It is possible to achieve also via Assumefrom JUnit. And then you can check the method name for which you want to process @Before
.
也可以通过JUnit 的Assume实现。然后您可以检查要处理的方法名称@Before
。
public class MyTest {
@Rule
public TestName testName = new TestName();
@Before
public void setUp() {
assumeTrue(testName.getMethodName().equals("myMethodName"));
// setup follows
}
}
Check the topicfor more insights about @Rule
.
查看该主题以获取有关 的更多见解@Rule
。
回答by Pocha
Not sure about @Before, but I recently came up with a strategy for @After block to run selectively. The implementation was straight forward. I have some flags set to default values as part of the test class. They are reset to default values in @Before class. In the class I need to do things specific to a flag, I set those flags & in @After I check for flag values to do the respective jobs.
不确定@Before,但我最近想出了一个策略,让@After 块有选择地运行。实施是直截了当的。作为测试类的一部分,我将一些标志设置为默认值。它们在 @Before 类中被重置为默认值。在我需要做特定于某个标志的事情的课程中,我在 @After 我检查标志值以执行相应的工作时设置了这些标志。
回答by Rohit Singh
@Nested + @ForEach
@Nested + @ForEach
Totally agree with the point of moving the related code to an inner class. So here what I have done.
完全同意将相关代码移动到内部类的观点。所以在这里我做了什么。
- Create an inner class inside your test class
- Annotate the inner class with @Nested
- Move all the test methods you want to use in the inner class
- Write the init code inside the inner class and annotate it with @ForEach
- 在测试类中创建一个内部类
- 使用 @Nested 注释内部类
- 在内部类中移动所有要使用的测试方法
- 在内部类中编写init代码并用@ForEach注解
Here is the code:
这是代码:
class Testing {
@Test
public void testextmethod1() {
System.out.println("test ext method 1");
}
@Nested
class TestNest{
@BeforeEach
public void init() {
System.out.println("Init");
}
@Test
public void testmethod1() {
System.out.println("This is method 1");
}
@Test
public void testmethod2() {
System.out.println("This is method 2");
}
@Test
public void testmethod3() {
System.out.println("This is method 3");
}
}
@Test
public void testextmethod2() {
System.out.println("test ext method 2");
}
}
Here is the output
这是输出
test ext method 1
test ext method 2
Init
This is method 1
Init
This is method 2
Init
This is method 3
Note: I am not sure if this is supported in Junit4. I am doing this in JUnit5
注意:我不确定这是否在 Junit4 中受支持。我在 JUnit5 中这样做