我是Java & JUnit
的新手,碰到不同的赛程。我在网上搜索了很多,但我无法得到答案。是否可以在JUnit中为每个测试用例使用不同的@Before @After?
是否有可能在JUnit
中针对不同的测试用例使用不同的@Before @After
? 对于例如:我有以下的TC则有可能要用于测试1
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
不同@Before
任何人都可以指导我?
也许你的测试不应该在同一个类,如果他们不共享相同的初始化/上下文... – LaurentG
@LaurentG行。谢谢!顺便说一句,你知道'main'为JUnit调用的地方,因为我无法在其中找到代码。它是在junit包下完成的吗? – Amrit
JUnit测试没有'main'。它们直接从开发环境中调用 - 就像右键单击一个类或包并选择“运行测试”或菜单中的某处;取决于具体的IDE。你提供的代码是足够的,它不需要更多的方法。你用什么:Eclipse,NetBeans,...? –