Java 对于JUnit中的每个测试用例,是否可以使用不同的@Before@After?
我不熟悉Java 对于JUnit中的每个测试用例,是否可以使用不同的@Before@After?,java,eclipse,unit-testing,junit,Java,Eclipse,Unit Testing,Junit,我不熟悉Java&JUnit,遇到了不同的装置。我在网上搜索了很多,但没有找到答案 对于JUnit中的不同测试用例,是否可以使用不同的@Before@After? 例如:我有以下TC,那么是否可以在测试前使用不同的@Before和在测试1前使用不同的@Before呢 import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; import org.ju
Java&JUnit
,遇到了不同的装置。我在网上搜索了很多,但没有找到答案
对于JUnit中的不同测试用例,是否可以使用不同的@Before@After
?
例如:我有以下TC,那么是否可以在测试前使用不同的@Before
和在测试1前使用不同的@Before
呢
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class testJUnit {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("Before Class");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("Tear Down After Class");
}
@Before
public void setUp() throws Exception {
System.out.println("Setup");
}
@After
public void tearDown() throws Exception {
System.out.println("Tear Down");
}
@Test
public void test() {
int a = 1;
assertEquals("Testing...", 1, a);
}
@Ignore
@Test
public void test1() {
int a = 156;
assertEquals("Testing...", 156, a);
}
}
有人能指导我吗?只要写一个你在测试中调用的私有方法。如果你想要不同的
在之前和之后,把每个测试放在它适当的公共类中
标记为@Before的方法将在执行之前执行
课堂上的每一次考试
如果测试不共享相同的初始化/上下文,那么它们可能不应该在同一个类中…@LaurentG OK。谢谢顺便说一句,你知道JUnit在哪里调用了main
,因为我在它的代码中找不到。它是在junit包下完成的吗?junit测试没有main
。它们直接从开发环境中调用——比如右键单击类或包并选择“运行测试”,或者在菜单中的某个地方;取决于特定的IDE。您提供的代码足够了,不需要更多的方法。您使用什么:Eclipse、NetBeans等等?@ViliamBúr我使用的是Eclipse。再次感谢:)您也可以使用超类中的After/Before方法。因此,如果您希望每个After/Before有更多的类,只需将它们放入一个(抽象)超类中即可。顾名思义,super-Before将在子类的注释方法之后运行,super-After将在子类的注释方法之后运行。