JUnit注解解释

JUnit里面有很多注解,下面解释下每个注解的意义

org.junit.BeforeClass

必须为静态方法,只会执行1次,

  [1] mytest.CalculatorTest.beforeClass (CalculatorTest.java:18)
  [2] sun.reflect.NativeMethodAccessorImpl.invoke0 (native method)
  [3] sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
  [4] sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
  [5] java.lang.reflect.Method.invoke (Method.java:498)
  [6] org.junit.runners.model.FrameworkMethod$1.runReflectiveCall (FrameworkMethod.java:50)
  [7] org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java:12)
  [8] org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java:52)
  [9] org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:24)
  [10] org.junit.runners.ParentRunner.run (ParentRunner.java:369)
  [11] org.junit.runners.Suite.runChild (Suite.java:128)
  [12] org.junit.runners.Suite.runChild (Suite.java:1)
  [13] org.junit.runners.ParentRunner$3.run (ParentRunner.java:296)
  [14] org.junit.runners.ParentRunner$1.schedule (ParentRunner.java:70)
  [15] org.junit.runners.ParentRunner.runChildren (ParentRunner.java:294)
  [16] org.junit.runners.ParentRunner.access$0 (ParentRunner.java:288)
  [17] org.junit.runners.ParentRunner$2.evaluate (ParentRunner.java:271)
  [18] org.junit.runners.ParentRunner.run (ParentRunner.java:369)
  [19] org.junit.runner.JUnitCore.run (JUnitCore.java:163)
  [20] org.junit.runner.JUnitCore.run (JUnitCore.java:133)
  [21] org.junit.runner.JUnitCore.runMain (JUnitCore.java:88)
  [22] org.junit.runner.JUnitCore.main (JUnitCore.java:38)

org.junit.Before

在每个Test方法之前执行,如果有多个,有排序()

如果类指定了排序类

 Comparator<Method> comparator = getSorter(clazz.getAnnotation(FixMethodOrder.class));

否则使用默认排序方法,系统有2个

/**
     * DEFAULT sort order
     */
    public static final Comparator<Method> DEFAULT = new Comparator<Method>() {
        public int compare(Method m1, Method m2) {
            int i1 = m1.getName().hashCode();
            int i2 = m2.getName().hashCode();
            if (i1 != i2) {
                return i1 < i2 ? -1 : 1;
            }
            return NAME_ASCENDING.compare(m1, m2);
        }
    };

    /**
     * Method name ascending lexicographic sort order, with {@link Method#toString()} as a tiebreaker
     */
    public static final Comparator<Method> NAME_ASCENDING = new Comparator<Method>() {
        public int compare(Method m1, Method m2) {
            final int comparison = m1.getName().compareTo(m2.getName());
            if (comparison != 0) {
                return comparison;
            }
            return m1.toString().compareTo(m2.toString());
        }
    };

具体哪个,未确认,读者自己debug下。

 

org.junit.Test

标记需要监控的方法

 

 

org.junit.After

Test方法之后执行

 

org.junit.BeforeClass

必须为静态方法,只会执行1次

 

 

org.junit.Ignore

标记的方法不会被执行,只针对@Test方法有效

 

最后,奉上测试源码

package mytest;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class CalculatorTest {
	public CalculatorTest() {
		System.out.println("CalculatorTest generated...--->" + this);
	}

	@BeforeClass
	public static void beforeClass() {
		//new Exception().printStackTrace();
		System.out.println("@BeforeClass of  invoked");
	}

	@Before
	public void before() {
		System.out.println("before of " + this + " invoked");
	}

	@Test
	public void evaluatesExpression() {
		Calculator calculator = new Calculator();
		int sum = calculator.evaluate("1+2+3");
		assertEquals(6, sum);
		System.out.println("sum is ---> " + sum);
		System.out.println("evaluatesExpression of " + this + " invoked");
	}

	@Test
	public void evaluatesExpression1() {
		Calculator calculator = new Calculator();
		int sum = calculator.evaluate("1+2+3");
		assertEquals(6, sum);
		System.out.println("sum is ---> " + sum);
		System.out.println("evaluatesExpression1 of " + this + " invoked");
	}

	@Test
	@Ignore
	public void evaluatesExpressionIgnored() {
		Calculator calculator = new Calculator();
		int sum = calculator.evaluate("1+2+3");
		assertEquals(6, sum);
		System.out.println("sum is ---> " + sum);
		System.out.println("evaluatesExpression1 of " + this + " invoked");
	}

	@After
	public void after() {
		System.out.println("after of " + this + " invoked");
	}
	
	@AfterClass
	public static void afterClass() {
		//new Exception().printStackTrace();
		System.out.println("@AfterClass of  invoked");
	}
}
package mytest;

public class Calculator {
	public int evaluate(String expression) {
		int sum = 0;
		for (String summand : expression.split("\\+"))
			sum += Integer.valueOf(summand);
		return sum;
	}
}

测试结果如下:

method--->public void mytest.CalculatorTest.before()
method--->public static void mytest.CalculatorTest.beforeClass()
method--->public void mytest.CalculatorTest.evaluatesExpressionIgnored()
method--->public void mytest.CalculatorTest.after()
method--->public void mytest.CalculatorTest.evaluatesExpression()
method--->public void mytest.CalculatorTest.evaluatesExpression1()
method--->public static void mytest.CalculatorTest.afterClass()
method--->public java.lang.String java.lang.Object.toString()
method--->public boolean java.lang.Object.equals(java.lang.Object)
method--->public final native void java.lang.Object.notify()
method--->protected void java.lang.Object.finalize() throws java.lang.Throwable
method--->public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
method--->public final void java.lang.Object.wait() throws java.lang.InterruptedException
method--->public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
method--->protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
method--->public native int java.lang.Object.hashCode()
method--->private static native void java.lang.Object.registerNatives()
method--->public final native void java.lang.Object.notifyAll()
method--->public final native java.lang.Class java.lang.Object.getClass()
getChildren *** is invoked
filteredChildren --- > [public void mytest.CalculatorTest.evaluatesExpressionIgnored(), public void mytest.CalculatorTest.evaluatesExpression(), public void mytest.CalculatorTest.evaluatesExpression1()]
[public static void mytest.CalculatorTest.beforeClass()]
@BeforeClass of  invoked

*******************************************
runChild invoked...method is --->public void mytest.CalculatorTest.evaluatesExpressionIgnored()

*******************************************
runChild invoked...method is --->public void mytest.CalculatorTest.evaluatesExpression()
methodBlock--->public void mytest.CalculatorTest.evaluatesExpression()
CalculatorTest generated...--->mytest.CalculatorTest@2f92e0f4
[public void mytest.CalculatorTest.before()]
before of mytest.CalculatorTest@2f92e0f4 invoked
sum is ---> 6
evaluatesExpression of mytest.CalculatorTest@2f92e0f4 invoked
after of mytest.CalculatorTest@2f92e0f4 invoked

*******************************************
runChild invoked...method is --->public void mytest.CalculatorTest.evaluatesExpression1()
methodBlock--->public void mytest.CalculatorTest.evaluatesExpression1()
CalculatorTest generated...--->mytest.CalculatorTest@28a418fc
[public void mytest.CalculatorTest.before()]
before of mytest.CalculatorTest@28a418fc invoked
sum is ---> 6
evaluatesExpression1 of mytest.CalculatorTest@28a418fc invoked
after of mytest.CalculatorTest@28a418fc invoked
@AfterClass of  invoked

有兴趣的可以看看网友的https://my.oschina.net/jackieyeah/blog/183277

转载于:https://my.oschina.net/qiangzigege/blog/830118

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值