Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于JUnit中的每个测试用例,是否可以使用不同的@Before@After?_Java_Eclipse_Unit Testing_Junit - Fatal编程技术网

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将在子类的注释方法之后运行。