该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example3 e3 u2 a1 ~0 d6 `) _# ?
新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo- k9 ~% Q3 [' \9 ?
9 R+ j# X; H8 D) ]TestExample.java- package com.mkyong.test;
9 A& b7 c1 n1 ~4 |. ?* j1 C -
2 L& P$ J* `$ I2 H( y - import com.mkyong.test.core.Test;- T" ~7 |; }( z8 B' U! ]- T5 m
- import com.mkyong.test.core.TesterInfo;( Y4 a o H* b
- import com.mkyong.test.core.TesterInfo.Priority;
8 P( Y l' \ @- A! V8 K -
) v/ ~/ |+ e" F+ `. J - @TesterInfo(
5 i, X& P- X1 N% I. u - priority = Priority.HIGH,
# O# l+ E5 ]1 K8 ^$ G5 J - createdBy = "mkyong.com", $ k- X8 E) _1 ~; {, {6 c9 I* s
- tags = {"sales","test" }
+ H5 w3 G" E8 Y% Q - )( P# S+ Z. `1 C$ @) O
- public class TestExample {
3 r- t2 l: e: l) j0 k0 m5 J. s - 5 k8 u+ Z! o% n- ?
- @Test: @$ j. M' s% p' \4 r
- void testA() {
3 _+ M) R/ i+ H - if (true)
" m5 O, _3 M# o7 q$ [- |. {. o* L0 d - throw new RuntimeException("This test always failed");
7 s& j' r5 y. K b - }
7 G$ }# S* t" s) q$ b - 9 r5 K: L0 z6 B6 F' H
- @Test(enabled = false)% X9 b# ]( m% a8 {; `4 a
- void testB() {" N9 E; K# D' G" C
- if (false) d, d1 [; b4 Q" ]6 N; n
- throw new RuntimeException("This test always passed");
* v+ n: a7 d6 h3 z# r+ j( x - }
! x" W$ H. ?3 k8 x+ l0 t! } - ' D( e+ b; }: _- N0 `
- @Test(enabled = true)! E! E+ L" t6 d' A! D7 M
- void testC() {- Z' K* k& W/ k" d5 X
- if (10 > 1) {
; {6 p0 G w1 Y( m6 c - // do nothing, this test always passed.
7 D8 S0 u( J' ]! I1 Q" D0 D. w - }
0 a6 ?* ^% H# x( B/ C0 k- g7 w+ x - }% j8 y3 x( V- ?+ G
- ! k; @# p* i' Q [! `
- }
复制代码
$ N( J4 t/ d7 z5 R- y4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。
# J( X, _# M$ O( B7 G" t: R8 q- A* O; L& i6 ~& a' u
RunTest.java6 P- d0 y" m8 }4 a
: P! T5 g% L! o: c3 k( ~. w
- package com.mkyong.test;2 ^3 h; H0 O& S! N+ w( ]8 U7 l
- ( y$ ]2 j0 \9 [5 [2 |( d! ?( ~
- import java.lang.annotation.Annotation;3 q) o+ U& D) P) a% l: e3 T" m
- import java.lang.reflect.Method;
4 J1 V8 `4 R. m, x: |) e7 X - ) m3 V6 W! d, H+ c x
- import com.mkyong.test.core.Test;
- t' _1 y4 j: D! ^( }( r G. [/ S - import com.mkyong.test.core.TesterInfo;: E! T; s2 V$ i. J
- # {7 S3 F8 U) K. p6 ^: k$ H; @0 ~
- public class RunTest {
' k* b+ r& I& m8 _$ S+ a0 l - 4 P' t. l1 p- w, e" J: A4 }* o
- public static void main(String[] args) throws Exception {
9 y: R3 ?1 b/ @, u" @ - 9 u) S* f1 C3 w& q$ p' G* Q
- System.out.println("Testing...");) f: Y6 Q+ V2 C+ N" f7 y$ }! M
- , |: n d5 `8 C8 y
- int passed = 0, failed = 0, count = 0, ignore = 0;
3 G: N& @$ S7 Y& G( R: ] -
% P0 s" Q2 q. s1 C9 K - Class<TestExample> obj = TestExample.class;
4 Y) f0 W; a) P' T n+ P" z( Q -
& h' p% ~; R" L r8 Z9 c" D - // Process @TesterInfo
2 C+ p% F! S3 |5 t3 Z1 [ - if (obj.isAnnotationPresent(TesterInfo.class)) {" D+ o2 i' N9 D+ {
-
; ?/ B0 D- @. f2 T" e' i, N - Annotation annotation = obj.getAnnotation(TesterInfo.class);
! r% X2 t$ v$ _" | Q - TesterInfo testerInfo = (TesterInfo) annotation;4 {1 z+ ~ N& ]9 a' Z: M: Z
-
. m. M& a3 L7 A5 V* d" H - System.out.printf("%nPriority :%s", testerInfo.priority());' K# r5 d; `7 I1 F! d7 C* O0 F
- System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
$ U4 D# O( S( z% V j4 C - System.out.printf("%nTags :");
" X% n, n$ U, j$ }8 g$ M) a -
$ H) w: `+ u( D8 p1 V - int tagLength = testerInfo.tags().length;$ G) r3 k3 {& O" g& k& x; b; A6 l
- for (String tag : testerInfo.tags()) {
- ]1 R j( K. u- E7 X! H - if (tagLength > 1) {! L- A6 k! |* n- I- v2 U
- System.out.print(tag + ", ");$ H5 w. M8 b' w; ~/ X( W
- } else {
% q( Y1 Q) l9 k" B: i' P: a* X - System.out.print(tag);
/ t( U. `* q/ d% P0 T3 k - }; K& d4 t( u5 _$ u7 s' r4 ?
- tagLength--;$ j" O" w1 ]2 Q' Q8 h6 `
- }8 M& U- @( o& m: l" B
- 8 H6 h) O+ m2 i) N2 Y4 ~, E* F
- System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
" P; Z: d0 ~$ r - 7 M0 x* `/ C$ e, V9 @* F4 c; X
- }$ |) _4 E3 w2 l/ m! S7 _$ S F
-
" `0 G; \0 }- Y& C8 x6 ` - // Process @Test
0 H8 I; d* s4 _4 L - for (Method method : obj.getDeclaredMethods()) {
: Z% o$ C( [% k V - ( m5 x! J* t7 f1 A8 n, K3 C
- // if method is annotated with @Test: P, b* j T( F0 V5 W& q
- if (method.isAnnotationPresent(Test.class)) {' m0 j" l8 A9 ]/ J8 J1 _3 |
- ( s* z& D c1 I0 g' P
- Annotation annotation = method.getAnnotation(Test.class); {: M5 R# z# Y1 s" T( `! d
- Test test = (Test) annotation;- J3 x! Q0 n% w g. i* ?" U: D
-
# ]* a0 p2 u: W2 s4 P - // if enabled = true (default)
; |$ z. {# D) v& R* L8 |9 `" K1 E - if (test.enabled()) {
' x& Y% O1 H7 V7 B4 T+ L, a -
% l8 a) [3 D* e8 e - try { s# q* P) C! Q1 _- {# l' |
- method.invoke(obj.newInstance());" O6 f, A2 g, e7 R/ ?. q5 x% E* O
- System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName()); U" d8 [# b* d! a- ^
- passed++;7 x, {( T9 Y% P# a. E- Z/ k
- } catch (Throwable ex) {$ c% \' e* Q7 z2 ]) N# o
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());
G9 Y2 G" {* a1 I5 W - failed++;. p( b2 f6 H- [" I/ j# _$ G
- }
& X# H' G( s9 d/ R4 ^$ N -
& t2 e! V% U: z7 b - } else {
" N/ n" B+ [+ s - System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());
! Z+ u# e- X4 e; y; W - ignore++;
8 i/ `3 y4 ?& r - }2 H o4 S5 U: h+ V6 R" S
-
# X) t ^; T/ I - } X1 N1 p4 K- U% g
- 8 Y( Z8 f5 ?/ E4 t. e2 W
- }, W( Q' k, X6 y/ r0 t
- System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore); G+ ^2 x% L% d
-
3 k |* Y. t' u) Z! L - }
/ _7 y1 F4 c1 w) e& x, Y- r - }
复制代码 % J n8 z5 Z: j/ D- L8 o, N8 ]# i
Output- Testing...! m) I- y/ x) @' |* l, y* p
-
% q" K# o" r, ~) _4 U3 h# v5 ?& F, [ - Priority :HIGH5 K S) E6 e2 t
- CreatedBy :mkyong.com
- k! m. P! Y9 F+ v+ ^# q - Tags :sales, <strong>test</strong>
~+ f0 p8 W7 V& s - LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>: o: v& x* x- J+ L9 h6 c% O
-
. p. D w+ K$ n, ?( r# I - <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed # A: x" s, Q5 T- e3 l/ o; f" q8 A: J
- <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed ( I! J. U$ Z) O
- <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored# U0 v7 r, D" A# S, U5 ^. S
-
) a e! [3 S: R& X+ p. p: | - 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>
复制代码 |
|