什么是 JUnit @Before 和 @Test [英] What are JUnit @Before and @Test

查看:21
本文介绍了什么是 JUnit @Before 和 @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屋!

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