该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
/ Z8 S" }4 V2 ]3 Y) ?7 T0 I新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo m1 l7 G. a( a \ w
0 ^/ m! q0 P* r+ |; f& wTestExample.java- package com.mkyong.test;
+ T! O' G. H7 u# }8 U - / O- g$ O) v3 G& e
- import com.mkyong.test.core.Test;
6 p5 s; L4 j! d9 P9 E0 X: N - import com.mkyong.test.core.TesterInfo;' @ e. D- a0 }' X4 C. u
- import com.mkyong.test.core.TesterInfo.Priority;0 Y1 N; Q) S( I& B# o
-
# T: L" J3 C$ m# L6 t& q8 a+ D4 g' p - @TesterInfo(" X6 |- K$ V+ B( V
- priority = Priority.HIGH, $ D8 `" |$ v5 g
- createdBy = "mkyong.com",
( r9 u" g! Y' S; w. P - tags = {"sales","test" }
0 r6 Z7 U8 H8 O* h9 ~/ R" A( j( V - )" l& v5 J, ~2 X1 V
- public class TestExample {
! g4 f$ k8 j/ t$ S; U* Z4 K% n - / \* d/ g% u3 X8 i, ]
- @Test
- R* C) J( D/ H( v0 X0 }& \ - void testA() {7 g* f7 }% N) t' C) Q% }. _
- if (true)
7 u1 n. O9 K6 ? - throw new RuntimeException("This test always failed");
- h' Y! F& |2 H3 Y2 f7 M0 X! O# v) d - }) U7 Q3 a( t0 ?3 H# A; o
-
8 [9 g" V3 O# ` - @Test(enabled = false) ^" {- U( G9 M
- void testB() {# }) g( |2 b6 T8 |& Q
- if (false)3 C \3 A: g& \+ e. A; U7 S
- throw new RuntimeException("This test always passed");8 p! @- B" g r4 l' w2 \) _8 C
- }
2 J; V% Z A3 F3 q: i5 Q5 i/ R1 R$ C -
" m5 U+ V+ E ]. n1 q! Z - @Test(enabled = true)% N1 u4 p0 g- w
- void testC() {
& T1 i. s9 ^( a# X - if (10 > 1) {
* c- d3 ~3 L; h; `% R - // do nothing, this test always passed.
7 r; a( e; t8 L0 i% h. j0 q+ ~ - }
! Y. ?3 ^% Z8 v H - }
, n4 [3 a# l# C6 r/ {; I7 t8 }8 G - & r% s8 m# U- C( ?/ a/ \: i
- }
复制代码 ; h3 e( g4 z5 b7 @
4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。
' q& }/ R# k; {3 d, k8 w+ O
5 T( J5 v' f0 X* o8 H$ {) iRunTest.java. E% V6 }. T& ~& x
9 B9 l7 r: z/ R; y7 ^1 T- package com.mkyong.test;
$ Q y8 d$ \ P3 [5 R Y -
3 Y' e0 o4 C4 v* I8 Z A: F - import java.lang.annotation.Annotation;+ `7 Q! ^6 J3 M' }; H
- import java.lang.reflect.Method;
$ S# ^* Q) n( M- z6 _' y -
) \( }8 `! A5 Y - import com.mkyong.test.core.Test;
( [6 j; d: {( t& I' f- H - import com.mkyong.test.core.TesterInfo;+ f7 ]" q9 E! C: q
- 9 F% r3 {* U/ ~9 O5 X4 O8 v
- public class RunTest {( L1 L) E$ f% s* t" Z l. t( h
-
5 \ q2 t7 J2 e$ e - public static void main(String[] args) throws Exception {+ z/ p& q# h: X' D' a, L: r
-
0 k/ \( i) Z' S" E9 ?! j+ q - System.out.println("Testing..."); H& g; v9 p& K. D
- % V l {, b2 A6 f! s; t5 O
- int passed = 0, failed = 0, count = 0, ignore = 0;" ^- ~7 t: h& \* D' o' I' t: ]
-
! ^$ Y T; e. A6 V" x( ^2 Y - Class<TestExample> obj = TestExample.class;* Z P. V& k- a% s
-
; }8 B/ r# [. k# } - // Process @TesterInfo
$ y# c, L# ?6 c2 G* K - if (obj.isAnnotationPresent(TesterInfo.class)) {0 \, @; ?2 m, h" m# d8 z/ `+ y. ]# `# E
- , C: I; b. ~/ q$ Z4 N
- Annotation annotation = obj.getAnnotation(TesterInfo.class);
! ?" W' f! C: {) L! s4 H5 i: M - TesterInfo testerInfo = (TesterInfo) annotation;+ T3 @/ P c2 V5 B' o2 ]/ z
- . O0 O) z; X8 R v2 c
- System.out.printf("%nPriority :%s", testerInfo.priority());1 R I% s: p2 \" p
- System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());! C7 c/ G: C2 A H& B3 F3 S
- System.out.printf("%nTags :");! @3 ]3 \- X9 X" C/ V [" V
- 4 y' p; O7 m0 P% E/ i1 H1 Y& r
- int tagLength = testerInfo.tags().length;3 D7 U* E2 x( e- J- k, u P
- for (String tag : testerInfo.tags()) {
; m- |+ K- z V S0 I - if (tagLength > 1) {' v% d' @0 _/ i; M- M/ u6 [
- System.out.print(tag + ", ");0 z8 C6 ^/ ?, w c5 Z
- } else {: X2 F4 Z1 o/ F E
- System.out.print(tag);
$ O( m9 d( @# d - }
& S% c0 V9 C: R2 h6 j - tagLength--;
4 B9 q; I: `$ d0 P& Z - }6 I# L3 u( g8 d# n1 A. E2 B
-
" X: u3 a7 {7 v% V' S - System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
# m, c9 p. y* R& T' V1 N -
* H8 F1 v% B; y8 y/ Z- J+ L2 C- a - }3 F2 ^( F4 ~. L# ]1 Q
-
3 ~) {; ]. k6 ~) I; F; L7 ]' M+ g) y - // Process @Test
, Z7 d' ]+ }$ z3 H - for (Method method : obj.getDeclaredMethods()) {: \3 ^* o- g# x* Z! y
-
/ P8 `/ Z5 M5 ^7 N2 s' g/ _% Y0 { - // if method is annotated with @Test
9 [9 X4 I8 t# D9 z4 S3 F - if (method.isAnnotationPresent(Test.class)) {
# F \, l+ `0 I8 I9 M+ L/ e - 8 ?6 \ b; B$ \8 g
- Annotation annotation = method.getAnnotation(Test.class);" j2 q* L% S9 C" F3 x
- Test test = (Test) annotation;# S8 \( A" U7 T6 `6 z; X
-
7 V4 o+ {/ q* V+ L - // if enabled = true (default)1 `( m0 C; u: v/ i5 i7 _0 G% J
- if (test.enabled()) {2 L4 c# v# I) B" o. A
- ! I, v. x! F7 _+ O$ v
- try {, \ N! a1 v- t$ ^$ c7 c- \
- method.invoke(obj.newInstance());
. G* H# N y1 {) V" ] - System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());3 ^) o% O- _5 J( ~& {
- passed++;% ]' ~( Q- E! ^" m3 x. F: E
- } catch (Throwable ex) {( v+ L% v* ~; V+ M: v: n2 R
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());% G' O3 E. w! W$ Z- t
- failed++;9 _0 L; @- C! c6 |) j$ C! t. ~
- }
! Y4 s$ \- N4 m$ Y -
5 a( m2 N; Z1 ]/ a& Z$ X - } else {6 k* z3 q6 [0 ?, i
- System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());; Q$ D/ l3 M0 u H
- ignore++;
1 l( m" `- w/ r. O1 V W! g* e! C2 B - }/ m( ?9 R8 g _1 {6 `! F
-
+ o0 Z/ E& Y# p ]( p% e5 A: O3 C - }
6 u3 p1 T( Q* J -
+ \0 d' x4 Y7 F. s3 k2 P3 ^ - }. R/ O9 P. v3 k) @: X
- System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);8 X( |8 S; D! j0 W" f
-
1 w! e$ Q6 Z$ T4 b4 {/ ], f - }
& t- p" S/ D5 L - }
复制代码 . p: L, _* b& A
Output- Testing...0 q6 x" E" C* W' N
-
! i; y6 P5 p! q! Q. i, Y$ G$ A - Priority :HIGH, U. Q0 K P5 o; C1 w5 ^. r3 u
- CreatedBy :mkyong.com
7 Z+ W, C6 U0 j; k; @- Z - Tags :sales, <strong>test</strong>5 o/ }) M9 S! B, {3 d0 c6 t, [8 x
- LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>
/ Y0 S2 X4 l) `) c2 n3 | O0 S- w - + k9 N! C& T7 T' A" L$ L8 |1 m
- <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed
2 Q* I" K! l/ K% Y+ Q& O - <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed " ^; G, W& V" U/ p
- <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored
/ V2 I- c5 K i; X3 G! ^4 t k -
9 D' q [' E; J8 m - 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>
复制代码 |
|