该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
; ^9 G; E2 p& ~/ E) T新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo/ d7 Z9 L8 l- ?) V4 D% L; x
$ p, b, A* @$ KTestExample.java- package com.mkyong.test;0 S% o7 k% p' m* D6 _
- $ @* d; _' R6 X: C
- import com.mkyong.test.core.Test; E6 h- x* k& s& C3 e0 s) s1 g
- import com.mkyong.test.core.TesterInfo;) h6 k' j/ F @' {' v+ Q# ~; w1 M2 W
- import com.mkyong.test.core.TesterInfo.Priority; S8 } {/ e: z8 ~9 l' M" w& K; A
- % e9 ?% t: k, M2 w4 a& s5 Z
- @TesterInfo(
3 y. x6 g+ B/ E0 i q6 P: R$ K - priority = Priority.HIGH, 2 U8 z7 h# q' u3 L8 |" T- x
- createdBy = "mkyong.com", % }4 @2 i3 C% }' W. w1 j, {! A
- tags = {"sales","test" }
1 d" i3 d' ~. X. w, R/ s, U - )0 L! g* l+ S0 v, F$ h" s: G' O5 f0 t
- public class TestExample {" [- h* C" _, D$ }; `
- 3 L) n+ E4 p; r4 L
- @Test
6 H _& d6 D' T8 o - void testA() {0 _0 j. i' G8 U R. J! {/ b
- if (true)
% H1 J8 q6 o8 T: R# w - throw new RuntimeException("This test always failed");
9 e6 C, t! Y' V2 x0 d! j* D - }& v s5 V0 P6 H2 k3 f- t( I: }! F
- . X! |0 x3 E1 O( E- r. A, B% D7 r
- @Test(enabled = false)
0 j! ~0 s2 m* k7 G9 m( M1 I - void testB() {+ [% ?% Z0 v4 ^8 m3 G
- if (false)8 r6 m( G3 Y" ]( p X7 c& \
- throw new RuntimeException("This test always passed");" R7 ^9 e$ R9 [/ D
- }2 F2 r: B F6 |: ?# Q8 r
-
. g$ I7 t2 C t/ z" ~ - @Test(enabled = true)+ D/ f) @: @1 R9 s9 A5 u+ M7 b
- void testC() {$ i9 z: n/ o( U0 L
- if (10 > 1) {
5 s( r' Q( x& `, U - // do nothing, this test always passed.0 a$ t* L: Y9 ]6 M0 @
- }
( t6 I1 j8 J) i" d7 q2 S - }
/ E/ D, x% C! y, O1 s* D9 U -
- @2 J G4 A# C* X/ J6 Q# i - }
复制代码
" K7 t& R# ^& D3 Y2 B9 ~. R: Y4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。
6 H% D" `* }4 j- j& A8 Q
. o# |9 n! B3 y' _/ A$ g, ]2 wRunTest.java7 g4 U2 {& X0 J" _( o& e M
5 D5 ?# k2 ^& k
- package com.mkyong.test;
$ r: p; r7 k9 z& ?2 C' d2 e" ` - $ y; l2 k9 O7 S5 ~
- import java.lang.annotation.Annotation;
9 f* e; v! d9 H3 C2 k - import java.lang.reflect.Method;& }% E% P% e& K* M, [8 j9 M
-
7 Y* d1 K4 k% e7 d$ k - import com.mkyong.test.core.Test;, |, H& o: i) d, b
- import com.mkyong.test.core.TesterInfo;. b6 J8 [ b; f7 T( X
-
, ?; h3 z/ H1 B# k - public class RunTest {
@4 y* p& T! E! b. h+ w -
; ~. c# H8 _7 D4 Z. Q3 o" H - public static void main(String[] args) throws Exception {' W' o( _3 P2 G+ s! w w
-
9 m7 J& A& ^. g - System.out.println("Testing..."); h4 g$ p5 ^8 g& X) l
-
# ~, e+ x& s2 H$ ?# r - int passed = 0, failed = 0, count = 0, ignore = 0;# m+ ]* @7 }+ O0 ?* ~3 K
-
0 K1 i9 l5 F+ F' g; X- z - Class<TestExample> obj = TestExample.class;
0 r5 p4 D% ]$ X- o! r. O2 J. a -
' r! d- i2 P' K - // Process @TesterInfo
# s, d; ^9 g+ ?1 | - if (obj.isAnnotationPresent(TesterInfo.class)) {
: {( F* O+ r+ m& ~* j! V1 W% Y -
/ s5 E! N& ]+ J' D; z* a) ^7 _ - Annotation annotation = obj.getAnnotation(TesterInfo.class);
- C1 e- S4 |* o/ L' g* U" w - TesterInfo testerInfo = (TesterInfo) annotation;- X4 c3 y3 X- r1 A5 X p) c
-
& @6 p- u% O: i, y, _) O) d - System.out.printf("%nPriority :%s", testerInfo.priority());$ u8 ^% Q8 y* z# G: I% k
- System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
. m+ n& y' z% l8 C; J$ O3 k - System.out.printf("%nTags :");* _& w5 G& U! Z( u
- 1 `* `7 `: k8 H: O3 |4 }. p
- int tagLength = testerInfo.tags().length;& e9 K) O2 E; T2 k3 m$ P! j* v$ B
- for (String tag : testerInfo.tags()) {' [% J' |, R0 v, J3 f
- if (tagLength > 1) {# I* S4 k ?; T" S
- System.out.print(tag + ", ");
+ V$ ^# F% a, @! c9 c - } else {7 y( C3 }+ }8 @# U
- System.out.print(tag);) n+ S" S: ?8 o- L k7 g/ R
- }
2 N2 u4 Y5 H9 ~& P1 } h9 s- E - tagLength--;6 c- m% L0 ^4 L- U
- } C3 M9 \# E" k; k
- ) f. c; y1 R2 V9 s" P! g& m( Z
- System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
/ `& X5 B# e( O0 G0 P2 R - $ @; ~5 p+ J! F8 p, p! w6 D
- }' M, E5 Y0 \& _; Z. M! V
- $ {6 D) \7 ~# _, d3 V/ P: t
- // Process @Test
4 D- ]7 Q" y6 L1 t2 i2 i4 m) o - for (Method method : obj.getDeclaredMethods()) {5 l( r4 e! p# e6 E' @. {
- ! R3 x. z+ ^/ Q
- // if method is annotated with @Test, O+ ^1 {+ o; J C) H: L
- if (method.isAnnotationPresent(Test.class)) {
' c) R! M5 |5 ~5 V1 H, r -
9 V7 x1 k N( l- h4 e" g - Annotation annotation = method.getAnnotation(Test.class);5 H. y0 ^0 h5 s: k: w
- Test test = (Test) annotation;# y6 T$ o- h( N. G9 `+ \" Q
- ! q7 b7 i' T2 F1 W; Z9 j5 E
- // if enabled = true (default): V' H h/ Q7 N. u/ e
- if (test.enabled()) {( l/ S! R v+ B0 a. y
- # u3 v) D2 x0 o( s
- try {
% J8 Z8 n" z) E$ X - method.invoke(obj.newInstance());
% ?5 c' P4 x0 B4 ]; W+ x2 t; K+ m+ _6 j7 N - System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());
+ G. L* s/ Y: w/ F8 O - passed++;7 x( @' |3 [7 q" {4 _0 V
- } catch (Throwable ex) {
2 ?) H5 j0 Q- Z0 e3 d. @ - System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());
1 f9 V8 [9 ]$ i3 p8 }6 h. ] - failed++;) F6 Z' A% k- h3 y/ ]
- }9 W$ J6 n3 _$ G! v- p9 u3 }. K+ [
- 5 x4 w2 R2 p2 P8 r' S9 m! E+ r9 h" X4 ?
- } else {
- p, i- d% e! t: Y2 j - System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());
6 e( |. S& a1 T6 \0 q' ] - ignore++;
3 P$ s j, w6 i5 S, J; l" G - }
4 d% X7 q; @( k$ I9 b" c -
' u3 @& I6 z, E9 T - }: p2 Q8 A8 Y; R$ q% e
- & {% l$ K( }' Y$ l+ ~9 s; c, ?
- }% R3 [/ ?. b$ v0 K) e1 U
- System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);
0 k3 f7 Z* \) D* d# r. f Z - $ `. V4 c+ c3 Q1 d
- }
) S5 e- s6 W# v2 h1 a - }
复制代码
& i/ ^1 _1 `( |1 Y) P$ i. YOutput- Testing...
9 |, ^ f6 C J0 o - # t( @; s) `1 t( o4 S1 n8 ]# L" q
- Priority :HIGH
$ G3 { _: H" d, \$ c; b - CreatedBy :mkyong.com0 n- I7 c8 x" i
- Tags :sales, <strong>test</strong>
- l5 Z, j; V2 D1 r - LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>5 I C2 D+ k4 M
- # [) R+ {4 W- u
- <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed
0 `# t: O7 G1 y - <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed
$ f" q9 K3 |7 F3 ]! K - <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored- ]. `6 u" h" z9 T
- 4 o% T6 f* W) v" C; 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>
复制代码 |
|