该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
% D( \% e" _1 }* c* s1 ]新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo
" N( `% a0 K* Q+ `/ l5 c A! | ]6 P2 j& b* W2 I# x/ K7 d/ O
TestExample.java- package com.mkyong.test;" j) ^& T3 }8 _; N% K
-
8 ^% o8 n! a# k: S* ~7 d W- P1 Y - import com.mkyong.test.core.Test;
- G- m d# w2 F+ C - import com.mkyong.test.core.TesterInfo;: E, w; h* U5 T+ c+ R
- import com.mkyong.test.core.TesterInfo.Priority;; w' g% I, |% Q5 i1 ]: g
-
7 [* ~8 Y( M. p: s! t- Y - @TesterInfo(7 ^+ q6 h& Y# N W7 }3 O
- priority = Priority.HIGH, % e; s1 S+ R( M6 ?. M. A2 F/ o) m
- createdBy = "mkyong.com", 9 @; ?3 n- Y0 b. x* g9 c
- tags = {"sales","test" }
' t& L0 g7 o5 c! K4 ?0 q9 r q - )
+ N; Q" X7 z* e - public class TestExample {# @" V: t' }7 ?& N9 D) `# O
-
; m3 q r. q/ i - @Test
+ w- W5 H/ k* X6 D - void testA() {
. p: s$ M o' m! Q N. P9 b6 q& m - if (true)
G( l1 c l6 g - throw new RuntimeException("This test always failed");) ?2 y: x/ T( I5 \
- }
9 V8 P) T, m, U' U -
0 L# s" n! b C" P. D/ u; B - @Test(enabled = false)
* x7 ^! Y( D, v) Q: @ - void testB() {
3 A3 G+ p0 M+ A6 f% C: W# u& \$ ? - if (false)9 t4 r' t8 _, ^0 I+ f
- throw new RuntimeException("This test always passed");7 D; q5 e' ?7 H$ Z% ?! C
- }2 E" g4 ~* f( q
- ! }( P) k; F9 |0 g9 V0 h
- @Test(enabled = true)1 ^' N5 _" G+ |
- void testC() {
4 k$ i, o( [) B" \ - if (10 > 1) {$ y7 J( R3 q- n) X. }
- // do nothing, this test always passed.
; R1 Q0 A+ ~: \- k* ]- c - }
# r0 q5 o# y' ?( U - }! H0 \8 L k" T3 F; D$ y+ ?
-
3 F2 T+ Y, o1 U* t! ]( o - }
复制代码 + }$ R3 g: P' Y0 [" g
4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。
/ W/ D: V" Q2 V! ]. V) Z/ C& S7 C0 j6 S! l% N- C- p4 V
RunTest.java/ [5 g( ], U$ X% c
/ _' W# \% }& g
- package com.mkyong.test;
9 q( g: i2 ^" {3 ]/ T$ B - 7 u" E0 G/ A1 |
- import java.lang.annotation.Annotation;5 X' A) w2 d4 F
- import java.lang.reflect.Method;
* G" S: r K. y9 r -
6 c! o$ W% f3 o. R/ y - import com.mkyong.test.core.Test;
|) a5 Q/ d! o/ a% B8 `8 g& S - import com.mkyong.test.core.TesterInfo;
# a; g/ v5 H6 G1 ~0 K0 M4 S; z" o - / C# o" X B- z$ }
- public class RunTest {
( f4 }5 q1 }$ |8 l6 H -
8 i' q% S. R+ b* W) w3 o - public static void main(String[] args) throws Exception {! B3 @" v3 _* C; Y
- % F/ Z4 [( Q: Y- @
- System.out.println("Testing...");
4 q# Z6 k$ w( S1 p - U" k1 @* W+ G \' F: Q
- int passed = 0, failed = 0, count = 0, ignore = 0;/ z9 q4 N& l- I7 O( U
-
2 [: K8 v4 Y9 q, C2 I - Class<TestExample> obj = TestExample.class;% C; N" R0 U* q7 g6 v6 _
-
0 H1 J/ W; V6 u& { Z - // Process @TesterInfo
" k. g2 J _. `! P. G4 }' g6 N - if (obj.isAnnotationPresent(TesterInfo.class)) {
( b# E p, G* P9 T# n: Q -
6 l, W# u" Y! |) L, h - Annotation annotation = obj.getAnnotation(TesterInfo.class);
! o+ Z. q8 Q( u0 z4 e& P# C) G2 } - TesterInfo testerInfo = (TesterInfo) annotation;
+ U2 p+ ]8 l' z! e( y! Z \3 A7 B Z6 g -
2 g- x* R: c+ q( q( l5 Q - System.out.printf("%nPriority :%s", testerInfo.priority());
- X, d# `7 h& y2 w7 U - System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());2 L, g& K( B/ N0 j
- System.out.printf("%nTags :");- Y3 \% m4 f' p2 c9 h5 `
- p9 P8 I3 `) M, I! A! C
- int tagLength = testerInfo.tags().length;
& i- o; s5 z, W, D - for (String tag : testerInfo.tags()) {) x5 u" f# K8 d/ H0 B; g+ F2 f: W
- if (tagLength > 1) {
. Y5 ?, {) C1 g# Y - System.out.print(tag + ", ");
- @) u, _! Z! n0 ~2 V* X - } else {* U" i6 i9 Y, Q3 k
- System.out.print(tag);
& X$ ?, J: x& I* P' j - }. t; |( `+ g+ m
- tagLength--;1 C" V( C6 G6 k" B, q" Y
- }
7 D3 n/ i% P* p8 ?8 F, |! `" ? -
+ v, M/ _6 G3 R* e( }4 r' D/ {8 h/ q - System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());+ G4 Z F( _; a! `8 V3 Q
-
" p5 T1 s: X4 k/ Y - }3 Y- V' r2 ]3 ^5 b2 w3 \
- ) y+ H9 f# g/ J* v1 V# T; D" O
- // Process @Test" Y; n; r/ C4 H* O2 u4 V& ~
- for (Method method : obj.getDeclaredMethods()) {6 z8 G6 H1 \! ^8 ]- a( [
-
( U0 p9 J" P! y0 r9 [7 _ - // if method is annotated with @Test
. K1 e* Y( q/ ` - if (method.isAnnotationPresent(Test.class)) {+ l# J' F: T5 L! G8 f
-
. L1 X T, I6 Q4 [+ P; [ - Annotation annotation = method.getAnnotation(Test.class);
9 X; f# v" n1 W: D& \4 W - Test test = (Test) annotation;5 @% \/ ~$ e/ I; F. W# T5 P
- ' l, |2 K3 u {4 w8 P
- // if enabled = true (default)
; v6 T# F, h" P. r' c4 H - if (test.enabled()) {$ d7 w0 L5 Q' v4 x+ R8 s. b
-
; ~ c) Y4 t$ _+ M4 J5 D3 }7 e6 ] - try {
6 G. J# g" N( C$ n5 u8 ] - method.invoke(obj.newInstance());8 r0 q" Q0 z+ s' n9 `+ p
- System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());
( ~* w g8 p- S8 T3 `" b+ n% h - passed++;4 \7 [4 H8 I" C3 _
- } catch (Throwable ex) { z: R g6 v' H1 F" D
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());7 `, w1 J. e6 e7 q. b9 B1 k. N
- failed++;0 C3 Z3 f+ z ~+ m- j% l
- }
5 i' G" k1 `) ~' j/ m& L -
. c7 M2 m2 s2 ~9 u6 V; G - } else {
1 E' g' e# g& m - System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());# z8 ?3 C6 \, D$ n
- ignore++;+ j5 g0 o3 E3 S2 H5 w
- }
- _8 A+ }4 \- `2 ?# \ - 3 n. B( t; m2 i8 \ s
- }
3 `" C$ _2 V0 D/ k1 z' P0 [ -
7 ~; b6 M; V$ y- s3 z - }
3 l8 I0 S5 q2 o n& k - System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);# Q2 r, S' o" ^
-
( c- q- [- y$ ]" \ x. p - }
# w* g5 m1 O) G8 e3 Z: q - }
复制代码 : t$ P5 ?% Z* r. J$ R$ \) M
Output- Testing...
. S) L M8 t* i7 i& Y - ! a- u* @0 S( T
- Priority :HIGH7 f. q7 m# u ^+ S
- CreatedBy :mkyong.com
$ J( q8 r- [( o - Tags :sales, <strong>test</strong>& @, M: Q9 X2 V& H
- LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>
! T/ a# h# F- j -
( T( f0 g& S- x% t- o - <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed ) s, ]+ Z9 a( o! F- @( H
- <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed 3 }+ N( \" V( _: E9 F1 u: \% G
- <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored
7 i: B. T% V8 Q -
9 T# C. U, F5 e( N - Result : Total : <span style="color:#000000">3</span>, Passed: <span style="color:#000000">1</span>, Failed <span style="color:#000000">1</span>, Ignore <span style="color:#000000">1</span>
复制代码 |
|