什么是JUnit @Before和@Test [英] What are JUnit @Before and @Test
问题描述
在java中使用Junit @Before
和 @Test
注释有什么用?如何在netbeans中使用它们?
What is the use of a Junit @Before
and @Test
annotations in java? How can I use them with netbeans?
推荐答案
你能更准确吗?
你需要了解什么是 @Before
和 @Test
注释?
Can you be more precise?
Do you need to understand what are @Before
and @Test
annotation?
@Test
注释是一个注释(自JUnit 4开始),表示附加的方法是单元测试。这允许您使用任何方法名称进行测试。例如:
@Test
annotation is an annotation (since JUnit 4) that indicates the attached method is an unit test. That allows you to use any method name to have a test. For example:
@Test
public void doSomeTestOnAMethod() {
// Your test goes here.
...
}
@Before
注释表示附加的方法将在 类中的任何测试之前运行。它主要用于设置测试所需的一些对象:
The @Before
annotation indicates that the attached method will be run before any test in the class. It is mainly used to setup some objects needed by your tests:
(编辑为添加导入):
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);
...
}
}
这篇关于什么是JUnit @Before和@Test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!