该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
% T! K5 U/ N) s# m/ w新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo
& ?$ W; `+ H4 c7 |* C- i+ d
6 y6 S1 `. Q! q: B P) hTestExample.java- package com.mkyong.test;
8 ^2 V9 s9 L; W: q& V - 4 x" G- S: ~" s( b
- import com.mkyong.test.core.Test;; M$ C" {2 Y8 f( U
- import com.mkyong.test.core.TesterInfo;
# W( j: e9 A- L J: L - import com.mkyong.test.core.TesterInfo.Priority;
) c% z; b: G1 M) A G: J - 2 J; y: b3 }7 I; k" n) {
- @TesterInfo(( h9 c# g2 B x
- priority = Priority.HIGH,
' t0 L( ?5 Z' C$ T+ x7 `4 j - createdBy = "mkyong.com",
2 F% h5 q. J" w$ T$ p - tags = {"sales","test" } @% B# ?* `# ^: @8 k+ ?# d2 F
- ). q" s. M& u0 X! N
- public class TestExample {
( \$ r" m1 \/ \, Y - ( q: N- |& t4 x( T2 h. \- h2 ?
- @Test
5 A2 J' z6 R, O" o- R - void testA() {
Y w2 n- b# O0 Q* G - if (true)1 C; s6 J: K/ `6 ` J1 k
- throw new RuntimeException("This test always failed");
5 ^& L+ [: o; l6 p - }
& N% Y6 K% g% n# Z& o - 2 d" U: k/ L$ Z( j# [
- @Test(enabled = false)
4 Y+ K4 L. S* K P2 X9 T: S - void testB() {
2 S+ u" D% ?# k+ E - if (false)% G. h7 K& Q! k5 q
- throw new RuntimeException("This test always passed");8 O6 e2 a5 i! h1 K3 p
- }3 M$ u4 Z0 E Y- Y( {
-
, P' ]. p, S2 w' X - @Test(enabled = true)
5 I' R. ]+ P* Q3 n- D* n( v: V5 p - void testC() {
& w& w2 n- o; R4 Q4 w% g - if (10 > 1) {) D' |7 Y1 w; @2 u$ U
- // do nothing, this test always passed.
5 W0 {; V1 P# B3 o% V. r - }* o, ~: r; V7 ~4 r
- }+ u: U& u* c$ g1 V- K I! z, e
-
3 S% O. z/ W6 `: O7 o* I$ Z - }
复制代码 * W$ X: \: I, x; h2 g- @ E: j
4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。
1 B( q7 a f9 G3 s9 g, S/ j' q A' n
RunTest.java" a! v- ^, l7 m% q) K. i) T/ ?
" ?; A7 v8 Q3 b' C& E2 G
- package com.mkyong.test;+ o$ Q* z) t. A$ }$ H' r5 {
- ' ?4 ]- c. [6 _
- import java.lang.annotation.Annotation;9 K2 p" J h5 O" K" I2 D
- import java.lang.reflect.Method;
1 ~( t v; k D! p - D0 n0 O. h3 A7 v! i: M" C
- import com.mkyong.test.core.Test;9 l& M6 H6 f# S; m U' C8 S6 Q* g' a
- import com.mkyong.test.core.TesterInfo;5 g/ h1 P4 L* G6 D% X
-
2 m7 g, N; w3 K' U - public class RunTest {' V9 E2 u" m# O" D
-
3 n" r, y4 Y* V - public static void main(String[] args) throws Exception {
3 f+ f( W0 A) r6 ^' {2 a# W -
0 R3 E, m/ B1 A1 f6 D# K9 S( c - System.out.println("Testing...");2 [. p8 o3 ~6 W
- 7 Q! b& V% U# V1 p. s
- int passed = 0, failed = 0, count = 0, ignore = 0;+ }" _9 ?3 i$ s8 ?$ F
-
$ G, E! L8 t0 T! j2 w S - Class<TestExample> obj = TestExample.class;
7 O( ?, k4 J& j( b' t3 w j -
" L7 g) Z9 O8 k; Z& r - // Process @TesterInfo [ m$ o* T' y9 @! K( R
- if (obj.isAnnotationPresent(TesterInfo.class)) {+ H2 ?: ~/ M* m% [ O4 O3 H6 ^
- $ `! [0 n$ P8 Z3 h( q
- Annotation annotation = obj.getAnnotation(TesterInfo.class);* Z; ]4 p8 j0 B6 V
- TesterInfo testerInfo = (TesterInfo) annotation;
/ j# H! a& F" q C3 O5 H- G -
: l; D# G. a# o6 z5 d - System.out.printf("%nPriority :%s", testerInfo.priority());
/ o' g" E' r' R. M; B - System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());7 V+ i# I9 \# f5 n
- System.out.printf("%nTags :");
- }* e& y8 f3 } - : M. u& B; [" Y4 m# F
- int tagLength = testerInfo.tags().length;
1 a! s8 _" \0 Y* X1 |) o - for (String tag : testerInfo.tags()) {
: s! V: ~2 s% O2 } - if (tagLength > 1) {4 g* b8 d- U5 V4 s! j* Z9 Y
- System.out.print(tag + ", ");5 z; h" N4 R, p6 G4 {; e. C
- } else {+ v/ Q8 x# v8 e4 Q$ c1 ^) c/ X
- System.out.print(tag);" e2 f! f9 d$ S- V* {
- }
/ c6 t/ ^% z8 M; [' \ - tagLength--;
) A8 _/ U0 g9 `3 j4 t% a - }
! W1 o% ?# u! y" X" O& J- s2 j -
( D8 x# T9 d5 t* {- U - System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
* D1 @6 b, S- i% G -
a8 e9 N. F/ ? - }
~( T8 S8 T* h2 S+ z/ z* r -
4 C0 `0 y/ H. H5 }7 {1 {" L - // Process @Test j3 t1 K6 j' v
- for (Method method : obj.getDeclaredMethods()) {8 |. g) E$ D% {3 Z5 L( D& s7 E9 L
-
9 a% }( K2 m: j! i! w! u - // if method is annotated with @Test+ A7 p. p8 g, E3 g# \
- if (method.isAnnotationPresent(Test.class)) {+ G# u: L4 e$ @7 j5 s7 e
-
9 _% B7 k7 v% O1 i" W8 x - Annotation annotation = method.getAnnotation(Test.class);
' h. w+ r, D' |5 P! f" ? - Test test = (Test) annotation;
1 h3 b6 e) g% m5 _+ m8 f# K - : c6 Z0 v- l, ]( l" ~3 y
- // if enabled = true (default)7 B& ?3 j6 W3 F; C
- if (test.enabled()) {
/ p* y [, o3 T- S - ) v; \' O. R& O) b9 Q. @5 J
- try {! q6 {: F/ V2 C
- method.invoke(obj.newInstance());' o) \, J" M7 x8 }$ k( f
- System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());7 v* a7 D1 x7 Q C w$ @: M$ j' {! j
- passed++;
3 {' z- L" f- h" a2 G1 f - } catch (Throwable ex) {7 ~$ H% y8 W3 N4 W6 q& Z6 Z( X0 r+ ?# @
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());0 j1 k6 b. d% C$ n$ b. d
- failed++;; j: L$ T& v3 R; x: G0 \0 U
- }
- q% u/ J4 _. ` l, U2 u - : g1 K7 L6 U M) ^% H: S: \: ]; _
- } else {3 G M& r" G3 ~$ I) C6 ~! K
- System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());# I. K, b/ f6 u5 U" Q7 i8 n( ]! }
- ignore++;
. m* z. ]4 ~- ^, o# l% ] - }
Z& Z+ G& d3 l9 i: \ - ! r5 p+ Y; {6 v6 [
- }
2 d1 v" ]3 _- e( E# n -
: _4 b7 v' ^9 T- o) X# o - }6 n5 M% c. g. S4 `
- System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);: y5 }2 W. v0 s' ]) a6 O
-
* ?0 r; A- }' J, R1 q6 m - }: y8 T7 e& s# ~
- }
复制代码
! P" P. m: l, j+ nOutput- Testing...
, Y" i0 h9 W5 h F7 x% @2 E' k - ) ]6 }) o' U* ^; a; m6 a+ M
- Priority :HIGH
% t1 U% c$ R H ^( E1 p - CreatedBy :mkyong.com
& I" s/ y) \! ]9 q - Tags :sales, <strong>test</strong>
4 l7 e8 ~; |1 y" ?1 h6 u0 y - LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>
K R# c( ]$ V3 d2 S -
& X6 w/ F9 c% e- y6 | - <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed
( }7 z& v% a5 v - <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed
- V$ X S6 d( n! c5 [+ x; q - <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored
: `9 x2 N$ u. r4 d9 t - # r2 r. f+ l& E
- 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>
复制代码 |
|