该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
' s& }6 g8 p1 u新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo. D7 p+ _; c- O' T+ D
K$ ?; x3 K0 B4 \4 n: a. m
TestExample.java- package com.mkyong.test;
" P7 {6 H3 b0 V3 p - : j5 T2 L' n' s
- import com.mkyong.test.core.Test;6 V+ H- S# H/ G/ g$ v& Y' {. k- e
- import com.mkyong.test.core.TesterInfo;
# z* u K* g M+ y: G4 k1 L) g - import com.mkyong.test.core.TesterInfo.Priority;1 Z- E9 H% _3 z( M
-
8 L+ w7 D8 i3 U - @TesterInfo(- Z. P1 c( I1 e" P9 D N3 D7 P
- priority = Priority.HIGH, + K0 [' |5 W |; B7 D6 k W# \
- createdBy = "mkyong.com",
7 L# |2 h/ E: W; j - tags = {"sales","test" }
9 g& b" g# _) B! C. v6 Y! U2 S - )8 P; h+ g9 i1 P8 @' c* }* ]0 G. t7 }' N
- public class TestExample {
$ V7 |+ c# `2 q - 4 ~5 x1 O. i& x5 R/ L; v+ C$ K
- @Test
6 e+ u* s6 b7 }" [. J/ M - void testA() {2 c# y# ?% c# x( }
- if (true)4 k" \9 b5 O7 s, M) T: B
- throw new RuntimeException("This test always failed");
6 L) b; Q1 b0 T- d% ^ - }3 J7 c& v( C2 L7 `& c/ i
- + x! \$ [" L6 Y+ _
- @Test(enabled = false), y. q2 ~: t6 |1 E! K, N6 _: c
- void testB() {
7 t. a, v8 ]. B4 `6 \) c - if (false)
1 q _- `5 V. K& J - throw new RuntimeException("This test always passed");; h( D6 N$ c# N1 B4 ~" {: _& B
- }
/ `. {( ^2 Q, q8 z -
7 ]0 k) W8 ?, P& m; [9 x - @Test(enabled = true)' c+ N0 M$ s' ^% R# h+ \
- void testC() {% E b4 M* k, e; M$ \
- if (10 > 1) {8 q0 R* _; `- S3 }9 _5 N$ u
- // do nothing, this test always passed.
' l3 q* l2 u1 ] - }9 B( i, \' |. E2 t H, [
- }" p( }* V- C0 @3 \- _+ Q
- 6 I$ F% d& \. w$ D) Z1 J5 M" A$ {
- }
复制代码
' _6 s. A8 s7 X: K9 u4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。9 `; B# d- d0 q: O
q- v g' R) D8 `% Y# L+ hRunTest.java: @* x) V, F l. o+ A0 z. V
. h$ I, x, ^6 h N
- package com.mkyong.test;3 v6 L7 r( l l7 _0 T C; [
-
?$ k9 B9 }9 t+ W% n6 i% a) C - import java.lang.annotation.Annotation;
& ?, A+ R: Z% j) g# t& \ - import java.lang.reflect.Method;3 Q2 V9 N- V0 X; ?8 L
- 4 T7 n/ O3 \5 K+ L+ W& D$ m) S) Z
- import com.mkyong.test.core.Test;
* M1 q1 y$ T" P% z4 U$ | - import com.mkyong.test.core.TesterInfo;
k5 w2 u0 r' m/ J* k, K1 I# l% K: `" M -
: e3 y* |, L$ Z$ ?7 c - public class RunTest {3 f f/ y5 }# e; w9 D6 O+ _
- I" ^' l1 Y& S. I+ T, [; o- N* o
- public static void main(String[] args) throws Exception {
/ g) E. s4 o' C1 }% }! c. X -
; r" i) U/ ^8 D$ @0 \# b - System.out.println("Testing...");8 C- Q; a, C0 M4 Q" `1 p2 ]: w
- & A8 R- Y# y$ ~3 ?
- int passed = 0, failed = 0, count = 0, ignore = 0;3 J" W5 {" g3 E9 }% e, Y6 d) K
- / f. y0 o0 G* K5 L* ^6 ?
- Class<TestExample> obj = TestExample.class;
5 ~7 g# k6 e. J3 G1 a - ; @" f7 I9 x8 S$ r" Z
- // Process @TesterInfo+ Q V' V* T& ?9 z: |* A9 ~
- if (obj.isAnnotationPresent(TesterInfo.class)) {3 X5 W" _' j0 f' h# ^' b3 t# {
- % C k) C. e- x" _
- Annotation annotation = obj.getAnnotation(TesterInfo.class);
) K; y0 E/ T! ] - TesterInfo testerInfo = (TesterInfo) annotation;/ n9 H' X* J+ X; b% t, ~$ w( V
-
! y/ _6 c. q ^& B1 K - System.out.printf("%nPriority :%s", testerInfo.priority());5 u# w# C, l7 }8 i- _* D# S
- System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());+ Y3 P, H U) K8 S
- System.out.printf("%nTags :");
5 a4 N: \6 g6 k( g/ s: \ -
- F. s( Y ]) a - int tagLength = testerInfo.tags().length;
7 R5 X* ], N" [& O2 e/ `0 \: I - for (String tag : testerInfo.tags()) {0 ?3 F2 g/ k7 y
- if (tagLength > 1) {! O1 f' V. E' ]& a
- System.out.print(tag + ", ");
5 L8 o; g" Y- ]* C; Y8 G- [ - } else {
2 o! N, s7 v { - System.out.print(tag);) F! X" Q/ I- U# V$ o9 s
- }. h" h& s& E2 B( G. c! X! }
- tagLength--;
9 B8 b: W4 ]! C$ F - }; {: @# z% ~ p+ O
-
' L, j1 Y3 X# {5 W* J - System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
% h; J$ o! F7 a -
7 \: I) S( ~: i( | - }/ |8 i3 E. Y# ]3 L, @
-
+ r$ R. I; L. M! g6 C - // Process @Test
" C$ L" `- J( U" t! M- W/ m1 X0 ] - for (Method method : obj.getDeclaredMethods()) {2 Z, x8 E. v% e/ i
- " h' e, g8 K& S8 a; p
- // if method is annotated with @Test
; n7 f+ z, y3 l+ R' i. _ - if (method.isAnnotationPresent(Test.class)) {) F) G. [5 `5 A# Q# x4 i/ p [2 p
-
+ t8 U4 H; s' j - Annotation annotation = method.getAnnotation(Test.class);
9 w; E' I+ z( |" K8 d - Test test = (Test) annotation;6 c/ z- V" Z/ L
-
% h" ]$ A. u* X - // if enabled = true (default): N9 Z+ A9 [0 }* t# Z0 e* _- v- `5 [
- if (test.enabled()) {8 s% ]' `: n7 T: r* h+ R5 t# \( n
- 3 p+ G5 E$ _( O- C/ U
- try {
. S1 ?; E$ X# C, |, \* F: i4 W - method.invoke(obj.newInstance());9 p. V' J9 A8 r
- System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());, T$ Z- G* d1 O
- passed++;9 U& [' o; U s5 W1 u$ r5 M) l; J2 z& q
- } catch (Throwable ex) {- B |4 F2 p* r) T1 e
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());
2 }& I! C/ \- l; {) N/ W - failed++;
; B! ^& Z% N! p5 a; v9 Y% d - }. Q: F- v* o2 D+ H' u1 N
- 4 Z9 J! q6 j6 z$ n" o# A% u
- } else {
9 g/ G/ L; C, k2 R - System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());: d$ q4 M5 ]) v; p
- ignore++;
! F# V" m% \+ N( ^; G; c& K6 C - }
5 N5 d5 ~1 }+ X9 K6 A; ? - & Y5 d* B1 m+ o& {5 s* U
- }+ d4 X( Q/ i& h- e8 R) E4 Z
-
5 _9 T; Q5 C; C; k1 T- v/ } - }$ B( O0 G4 z4 F' U
- System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);
( k. k8 N! g/ o7 l! P+ z; s -
* ?5 }9 h$ w5 k. v7 m* P" I - }( y, g$ q& `; C
- }
复制代码 / [* [- o ?% S7 W9 |- R' D
Output- Testing..." b+ P% y" `) v3 j
-
$ s, ?* ?0 G8 @ c" V" f4 g - Priority :HIGH- D4 u! K$ q, H* T0 P& V% `- h
- CreatedBy :mkyong.com" h2 j& v5 R! A6 b
- Tags :sales, <strong>test</strong>+ s2 p/ ^6 s! m/ \) a& P* a& h
- LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>/ \+ n3 ~$ b2 s! S) ]/ ]
- 9 E) v0 T/ `7 y z/ J
- <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed
' z" q- ~4 B" _/ O, I/ G9 x - <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed 2 d% a+ M3 w& g. K3 ]
- <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored, j7 C( E# c; ?; ] F# q
-
0 R$ O( i+ Z( D: F" d - 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>
复制代码 |
|