解读Junit的@Test注解,避免initializationerror

青旅半醒 2022-06-13 01:14 317阅读 0赞
![Open Declaration][]org.junit.Test

@Target(value={ METHOD}) - - - target注解:有效目标—只作用于方法
@Retention(value=RUNTIME) - - - Retention注解:有效范围—运行时

The Test annotation tells JUnit that thepublic void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

(test注解告诉Junit为要做测试的public权限的方法创建一个测试实例。为了运行这个方法,Junit首先会创建一个该类实例,用于调用被@Test注解了的方法。遇到任何异常抛出时,Junit都会执行失败。如果没有异常抛出,Junit测试必然会成功)

A simple test looks like this: (简单实例如下:)

  1. public class Example {
  2. @Test
  3. public void method() {
  4. org.junit.Assert.assertTrue( new ArrayList().isEmpty() );
  5. }
  6. }

The Test annotation supports two optional parameters. The first,expected, declares that a test method should throw an exception. If it doesn’t throw an exception or if it throws a different exception than the one declared, the test fails.

(test注解可支持两个可选参数。第一个参数为:excepted——声明被测试方法应该抛出的异常。如果没有抛出异常或抛出的异常不是声明的异常,则会测试失败。)

For example, the following test succeeds: (比如,下面的成功测试:)

  1. @Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() {
  2. new ArrayList<Object>().get(1);
  3. }

If the exception’s message or one of its properties should be verified, theExpectedException rule can be used. Further information about exception testing can be found at theJUnit Wiki.

(如果要验证异常信息或某个属性,可是使用ExpectedException的规则(即在这种情况下推荐使用Exception参数)。若要了解exception测试的详细信息可以去JUnit Wiki 中查看。)

``

The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).

(第二个可选参数:timeout—如果单元测试的实际超过了规定的时间,则会显示测试失败。单位:毫秒)

The following test fails: (如下,测试失败情况:)

  1. @Test(timeout=100) public void infinity() {
  2. while(true);
  3. }

Warning: while timeout is useful to catch and terminate infinite loops, it shouldnot be considered deterministic. The following test may or may not fail depending on how the operating system schedules threads:

(警告:尽管timeout对于捕获和终止无限循环体非常有用,但应该考虑其不确定性。下面的测试可能会或可能不会失败,这个要根据运行系统的线程决定。)

  1. @Test(timeout=100) public void sleep100() {
  2. Thread.sleep(100);
  3. }

T**HREAD SAFETY WARNING: Test methods with a timeout parameter are run in a thread other than the thread which runs the fixture’s @Before and @After methods. This may yield different behavior for code that is not thread safe when compared to the same test method without a timeout parameter.Consider using the org.junit.rules.Timeout rule instead**, which ensures a test method is run on the same thread as the fixture’s @Before and @After methods.

Since:

4.0

(线程安全性警告:@Test方法的参数timeout只作用于一个线程上,除了运行时固定会执行的@Before和@After 方法的线程。。。。。)

-——————————————————————————————————————————————————————————————————————————————————————

@Test注解:只能作用于public权限的方法

[Open Declaration]:

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读