java中的Junit@Before
和@Test
注释有什么用?如何将它们与 netbeans 一起使用?
shiva
问问题
85423 次
2 回答
55
你能更精确一点吗?您需要了解什么是@Before
和@Test
注释吗?
@Test
annotation 是一个注释(从 JUnit 4 开始),指示附加的方法是一个单元测试。这允许您使用任何方法名称进行测试。例如:
@Test
public void doSomeTestOnAMethod() {
// Your test goes here.
...
}
@Before
注释表明附加的方法将在类中的任何测试之前运行。它主要用于设置测试所需的一些对象:
(编辑添加进口):
import static org.junit.Assert.*; // Allows you to use directly assert methods, such as assertTrue(...), assertNull(...)
import org.junit.Test; // for @Test
import org.junit.Before; // for @Before
public class MyTest {
private AnyObject anyObject;
@Before
public void initObjects() {
anyObject = new AnyObject();
}
@Test
public void aTestUsingAnyObject() {
// Here, anyObject is not null...
assertNotNull(anyObject);
...
}
}
于 2009-02-10T07:48:25.807 回答
22
如果我理解正确,您想知道注释的
@Before
含义。注释将方法标记为在每个测试执行之前执行。在那里您可以实施旧setup()
程序。注释将
@Test
以下方法标记为 JUnit 测试。测试运行程序将识别每个带有注释的方法@Test
并执行它。例子:import org.junit.*; public class IntroductionTests { @Test public void testSum() { Assert.assertEquals(8, 6 + 2); } }
How can i use it with Netbeans?
在 Netbeans 中,包含用于 JUnit 测试的测试运行程序。您可以在执行对话框中选择它。
于 2009-02-10T07:45:51.570 回答