Junit @Before
和 @Test
注解在java中有什么用?如何将它们与 netbeans 一起使用?
最佳答案
你能更精确一点吗?
你需要了解什么是@Before
和@Test
注解吗?
@Test
注释是一个注释(从 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);
...
}
}
关于java - 什么是 JUnit @Before 和 @Test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/531371/