在JUnit中为每个测试用例可以使用不同的@Before @After吗? [英] Is it possible to use different @Before @After for each test case in JUnit?

查看:313
本文介绍了在JUnit中为每个测试用例可以使用不同的@Before @After吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的 Java& JUnit ,并遇到不同的 。我在网上搜了很多,但我无法得到答案。

I am new to Java & JUnit and came across different Fixtures. I searched a lot on net but I couldn't get the answer.

是否可以使用不同的 @Before @After JUnit 中的不同测试用例?
例如:我有以下TC,那么可以使用不同的 @Before 进行测试和不同的 @Before for test1

Is it possible to use different @Before @After for different test case in JUnit? For eg: I have the following TC then is it possible to use different @Before for test & different @Before for test1

 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);
}

}

任何人都可以引导我? p>

Can anyone guide me?

推荐答案

如果你想要不同的之前 / code>,将每个测试放在正确的公共类中

If you want different Before and After, put each test in its proper public class


标记为@Before的方法将在执行
每个班级的考试。

Method that is marked with @Before will be executed before executing every test in the class.

这篇关于在JUnit中为每个测试用例可以使用不同的@Before @After吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆