Closed. This question needs to be more focused。它当前不接受答案。












想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。

3年前关闭。



Improve this question




Java中Junit的@Before@Test批注的用途是什么?如何将它们与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/

10-11 04:47
查看更多