该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
; `9 M* T& p5 q/ Y: H新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo/ ~1 t a h7 t) k& l, z6 B
* q7 T3 J3 H2 rTestExample.java- package com.mkyong.test;
" C5 }8 n) B+ z4 {9 [4 C Z -
) x: H% L$ D1 m- A$ A - import com.mkyong.test.core.Test;
& k4 D0 n7 a0 d& s6 y& H - import com.mkyong.test.core.TesterInfo;8 C, I ~, E2 i# u5 \- N- J$ {
- import com.mkyong.test.core.TesterInfo.Priority;
4 ~# R5 j8 t: U' Z - / q- R" j3 |" g7 p, ]2 z
- @TesterInfo(
2 E" [" q9 D' t9 ? - priority = Priority.HIGH, " Y* u. s$ h/ ^& u" \0 d
- createdBy = "mkyong.com",
& E0 F; W/ A! t. g+ o - tags = {"sales","test" }
& W/ w7 p! W1 Y4 k' o - )" ^* e6 n, K9 t% C" G n
- public class TestExample { D( b3 L% u3 P8 L
-
: J" ~. R! K7 ~, y( x - @Test4 g$ R: K: s. _2 `# g1 }$ y
- void testA() {
q6 N3 E0 m) R' e; x X - if (true)
4 i0 D4 T& }* M1 V/ Q% u6 g - throw new RuntimeException("This test always failed");5 l- v. L1 ?& {; y
- }& W9 @8 J+ g7 V7 Q" e; I; S4 W% N
- # U" x0 Q" b. O7 ~* ]
- @Test(enabled = false)# H( ^3 k9 G6 G. W. M% n+ V3 \! q
- void testB() {
& h& I. Y( ]4 Q6 k3 \$ E - if (false)
) A* s; Y2 ~9 F$ |/ y - throw new RuntimeException("This test always passed");' T- m- q; s8 y1 X1 {9 ^
- }4 P* A5 B' {+ j
- & B: r; D- l' i" }
- @Test(enabled = true)
6 O& c: D* \/ y2 K% J P% `8 d - void testC() {
: U: c0 _# S: \# R, ` - if (10 > 1) {
9 C* A' h) n+ j" j' d - // do nothing, this test always passed.7 i% [- B1 q7 _0 h8 X5 I2 v5 J
- }3 Y3 e0 k1 Q& U
- }
. I2 D7 c3 g7 a: T6 u2 i6 i/ { -
) v/ y% y/ J I' a% h% H& o' z4 j - }
复制代码 1 m, w; d3 c& k( L/ J. [
4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。 }5 |6 r7 g* n+ |! L
/ M+ I( O9 \. v+ \. m+ k" r9 E
RunTest.java5 z) Q# D, v$ B" V, Z+ [
' y* }3 b+ l6 e2 M2 L- package com.mkyong.test;8 V L- }. }. }' D9 Q4 X1 Q
- % V& c& @0 @5 j) B
- import java.lang.annotation.Annotation;
4 `1 R Q! u4 v* h& {- A - import java.lang.reflect.Method;8 H$ r% P9 }& o& N6 \' d
-
+ k, w/ A9 Y7 l+ C C - import com.mkyong.test.core.Test;
. m* n: ~: X- c - import com.mkyong.test.core.TesterInfo;2 y1 Z7 R% z6 j! Z% e! S) b
- ]8 [: V) N! u
- public class RunTest {/ k/ {& b- g' P& V% c
-
9 \0 ^: b( J& }# J/ Z2 z/ i - public static void main(String[] args) throws Exception {, x& [* x7 a9 P5 d
-
# w5 {) j1 v, s& G$ m* ] - System.out.println("Testing...");
9 K1 j2 k/ O, M% ?9 t! K - ' T* a4 ` s* u$ T7 V
- int passed = 0, failed = 0, count = 0, ignore = 0;8 s# y* s& }: ^+ E3 ]
-
! @& w: ]5 W8 }( B - Class<TestExample> obj = TestExample.class;
: M" y3 W2 {3 I: w* L6 ~0 r -
& F" Q5 T6 q7 c! \ - // Process @TesterInfo3 o1 [: x$ a, C- W; D8 V7 r5 W9 @
- if (obj.isAnnotationPresent(TesterInfo.class)) {! {, `! ]+ A6 [2 `+ a- {5 H% J, c
-
1 O" \9 R: Z# X0 U - Annotation annotation = obj.getAnnotation(TesterInfo.class);' E- R! A5 H a7 X' y8 k7 Q
- TesterInfo testerInfo = (TesterInfo) annotation;
5 R9 r% {1 f0 w+ K6 s* s' j- X, S m -
& j& u/ k6 H8 w+ Q5 |5 k) D - System.out.printf("%nPriority :%s", testerInfo.priority());
! K, l m! O) Q- \* r6 A0 ? - System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
/ E, [4 n) d. E! R5 ` - System.out.printf("%nTags :");
3 N0 _. T9 ^3 v5 l* {) i - & w' Q+ c7 i! ^
- int tagLength = testerInfo.tags().length;" P+ Z" b, M6 r/ V
- for (String tag : testerInfo.tags()) {
' E. [: R# Z' o3 H - if (tagLength > 1) {5 I4 a- {# x9 t4 @
- System.out.print(tag + ", ");
4 S5 r: @- ]# Z7 R$ t - } else {
' C6 g, }$ W7 _' a - System.out.print(tag);1 E# T* n" [4 X, s* C5 a6 @
- }
3 w/ V# i5 D" b - tagLength--;
+ a; [( G1 r5 q; \& V - }! k7 P- f" |) L2 y8 _6 }
- & q* Q: t: O2 y2 m/ z
- System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
6 r/ y, n+ u5 O- k, W+ ` -
5 o0 m, q. C+ c+ H; t6 O - }9 I8 y1 n# o5 e+ c
- - I+ x: h" b& |0 X
- // Process @Test
5 G, Z& p+ o" z' y - for (Method method : obj.getDeclaredMethods()) {
1 F. M* ~* [2 d8 \2 _% G - 3 ? T; i9 g/ R, N# V9 ]
- // if method is annotated with @Test' \% }: U& t+ \' B! [6 t% j
- if (method.isAnnotationPresent(Test.class)) {
$ @, s& G7 b1 c9 e' J -
7 A2 `$ s& ]: u: h/ ~0 H - Annotation annotation = method.getAnnotation(Test.class);# x3 |* G' i( |% {8 h3 }
- Test test = (Test) annotation;! [0 V- T' P+ P, u1 h' W5 m
-
4 l8 ^6 ]- B- F1 `7 `8 h) E1 h2 M# V - // if enabled = true (default)
9 y) X+ F/ l B; I Y - if (test.enabled()) {2 K: `8 L5 M; }& _
-
) N/ _( f8 V3 B) Q" }" ? - try {
* ]) P" A9 j' l' Z2 H% H: Y% K - method.invoke(obj.newInstance());3 F5 C* C# ~4 }8 t
- System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());# h2 Y5 I9 L6 o8 P9 l9 W
- passed++;! @7 y; A3 M+ ?% A0 v0 U: h
- } catch (Throwable ex) {
6 q7 @; M; h4 y) h - System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());
% M) u7 }5 C4 i - failed++;; D; D; l5 o7 W$ W: s6 }+ C
- }
3 X2 t: _, M# u2 o. g$ `# J -
: U4 P* k6 c6 I! I5 p$ V - } else {
# \$ d" `: J4 w! g4 z- } - System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());
( g. \5 I, d/ h' S# J: z - ignore++;
7 r: T" A6 L0 U - }
: D/ E7 g9 P- w -
; S+ Q q2 N/ [7 M' N& | - }4 m- P8 |5 u; z- P' n5 Y
-
% t" j2 o* Z: I+ z: U) g - }
4 T C- b1 P3 |9 @2 s( q - System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);- g& D& [: k1 r" N5 {: g, u
- / t6 j- b3 f( F
- }5 [, F# G: P9 o' x' {% v1 U
- }
复制代码
4 N! ]; v1 s$ i; l0 y7 w$ [Output- Testing...
0 c6 H" g/ W$ ^: f4 R) B - 2 v+ o2 ?2 H* _1 K4 z) |1 c' f* {- Q8 }
- Priority :HIGH2 _+ G# x8 _/ S2 v* ]+ L* g: r
- CreatedBy :mkyong.com
( h, v7 e* n0 M4 ? M - Tags :sales, <strong>test</strong>
9 {: L) r3 \$ C' l - LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>
6 T1 G5 Z% i% t6 t2 E+ M6 `7 E -
/ N8 Q3 k7 u- Q: @ - <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed
- \. m5 H. T3 }4 J/ j) i - <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed
6 ^! F X# K: a/ T - <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored
+ D6 {- S; }! e2 f6 y' ^ -
) W( X1 o2 n. ~: o5 R6 a2 K# h" \ - 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>
复制代码 |
|