该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example
1 ]* V% R' k7 `' q- ^新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo
! N* g8 Y6 H# e; c8 o* @8 `- B' E& y& H
TestExample.java- package com.mkyong.test;
% z `8 \$ J: _ - # ^ r5 ]& l& F: g0 t `# D
- import com.mkyong.test.core.Test;
5 v0 _, W! H1 X+ t$ B& x) x - import com.mkyong.test.core.TesterInfo;
" O& c. d2 m8 P! Q) I - import com.mkyong.test.core.TesterInfo.Priority;( G/ E8 U3 s; ?/ m+ P% N3 p
-
1 ]' z% W% i6 j( V& g9 x6 Z! Y' x- p - @TesterInfo(+ `7 D0 y8 \2 \- ^: q5 U4 d+ v
- priority = Priority.HIGH, - {+ l+ D" l+ y( ^. o% u
- createdBy = "mkyong.com",
- a8 l( h- u4 u - tags = {"sales","test" }: G. ^6 t+ M, C, ]/ D) ^
- )9 Y1 H. } ^# _7 r
- public class TestExample {# l2 H W0 k$ ]4 k
- / ]( E7 p" W" _2 {, R( g
- @Test
: g2 A3 c# k( x& N - void testA() {
; L* |: N. Y) H' `9 ~" g - if (true)8 g$ J3 Q! @ Z' `
- throw new RuntimeException("This test always failed");& w# }" X% H, }! O0 D
- }" v' I: |$ d) y) W- V3 b' t! Y
-
: ~, k4 m9 @" v3 L% ~1 r3 p - @Test(enabled = false)
* D9 m, ?* ` k3 M4 O! W - void testB() {* O4 j) p5 O) j5 ^, w% j
- if (false)& }* e% N' n* X; `
- throw new RuntimeException("This test always passed");
9 _ q6 v" g( p) N- v - }* \ ?- R7 A& Z- `7 l
-
% X w% ~# \& I4 [$ \- ? x7 N - @Test(enabled = true)( H) c& d7 _+ w, O) i
- void testC() {, O1 q1 \! W" B
- if (10 > 1) {- w5 Y" w u. _# b7 z9 \
- // do nothing, this test always passed.# b; S% p) G' h9 X7 l
- }
4 e1 Z% r+ o1 {1 F- Z - }+ W1 o g! L3 [# L
-
$ [- l5 E) s F( s- o! T* W, Q' x: m A - }
复制代码
- l2 z/ T0 H* \4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。& b# C2 r( d6 ?, I2 I
5 Z$ d. y4 d7 X/ [/ [+ u. y* p
RunTest.java( I; p, X5 g5 M$ O6 c1 N
% D, r* t6 A% f- ], q8 O' K
- package com.mkyong.test;
- @9 E1 n. x* W8 G - + v1 o9 X g. ^7 x
- import java.lang.annotation.Annotation;* M/ O5 `6 D5 X& {. g8 {
- import java.lang.reflect.Method;
; m, q$ v0 N9 Y - ' x$ ^: C$ g7 I6 l2 _4 [2 z
- import com.mkyong.test.core.Test;" U% g/ n5 ~5 F- h* A
- import com.mkyong.test.core.TesterInfo;
1 y/ @" f& s- b( ~: [ - . B, {& o7 q' P. r7 u
- public class RunTest {1 k) m; [: ~: f4 E6 d
- " ]7 N0 {1 f4 v+ F
- public static void main(String[] args) throws Exception { H6 Y2 Y4 B- S& F7 a6 r9 Y' I
- : c7 ~+ [0 h) x
- System.out.println("Testing...");
" K; G ~- L) n* B1 ~5 O, T x -
3 J1 I8 w0 K0 J - int passed = 0, failed = 0, count = 0, ignore = 0;
( s8 s$ G: H4 e% v: D -
2 ]8 p; o: y4 ^# n9 [ - Class<TestExample> obj = TestExample.class;; B% C3 }8 x1 f( z! g3 P$ o5 V$ t+ L
- + A' u) V4 X/ @9 t2 _6 D5 F. Y2 @; G
- // Process @TesterInfo3 j: ?# Q! H! P# i2 k8 m1 h
- if (obj.isAnnotationPresent(TesterInfo.class)) {6 N0 Z0 [( R: M! _
- ~# [2 V, Y, d
- Annotation annotation = obj.getAnnotation(TesterInfo.class);
- k6 {& \1 I* T' f2 Y% \0 z - TesterInfo testerInfo = (TesterInfo) annotation;" K* \5 I3 G' h9 O& ^* _7 g
- 4 Z$ H0 j; P4 t. q* P
- System.out.printf("%nPriority :%s", testerInfo.priority());% t3 R3 f* [2 H5 s, \% k6 N }
- System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
{. M7 X/ n' C# M - System.out.printf("%nTags :");0 E$ H: \- N8 ~- E t. n; g
-
. x- {$ `. }' c - int tagLength = testerInfo.tags().length;
+ {; x, ?2 V$ l# t) A) x4 I, o: U/ k - for (String tag : testerInfo.tags()) {
9 T1 o8 a1 W+ e, |; r! C( ]$ s1 _ - if (tagLength > 1) {$ P, b2 i, l& H) u. G" @
- System.out.print(tag + ", ");! t% y# { v, n
- } else {
' I/ t1 @3 r6 i/ Y; k. U$ X - System.out.print(tag); v3 K- m& n0 O$ X5 I
- }
8 z0 r, { o6 i& ` - tagLength--;
, V$ [: z& c1 x- n9 o! A2 {: E - }
; Y+ F( G2 f5 W- n3 G - & \8 x9 i) f. [, X- Q$ z. y
- System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());. U, G6 n$ n4 E
-
2 B; a' }: ~8 e8 Q* Q - }
' A( k. o6 j( C4 |! R3 `5 T -
/ D) w7 B4 q) G: B - // Process @Test# I4 L9 @/ U/ `6 A, v6 p! g7 S
- for (Method method : obj.getDeclaredMethods()) {: p) s% x# P% W) v3 k
- 5 D) I( t/ Y) i
- // if method is annotated with @Test4 ~2 M& A0 J( y R0 ]) h
- if (method.isAnnotationPresent(Test.class)) {) X4 e; V* g5 I, V `6 W
- 4 c- |; d7 l# E6 s" A* _
- Annotation annotation = method.getAnnotation(Test.class);+ j# b- u1 b1 C( d+ ~$ _
- Test test = (Test) annotation;2 o& ]- r$ a- q& q, I& D+ V
- % b' D2 l* H2 R% A. e+ j
- // if enabled = true (default)
; s! v7 [# f" O+ r" e0 A8 y( v Y - if (test.enabled()) {
! z* R- b! _+ m1 X, i0 e! c' c& W - ( K+ t* K' _/ ]9 ^4 X% n- x$ \
- try {
# F# `5 o9 B' J8 p3 g - method.invoke(obj.newInstance());
2 O( U$ p( f6 }: X$ C. @ - System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());
/ }7 n* s+ _( A9 | - passed++;
8 o" z; c8 \$ d- l7 L8 \& g - } catch (Throwable ex) {9 t% ^8 t! L: Z+ f1 |
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());1 T5 t$ A8 d# Z6 L0 I h
- failed++;- i+ Z' ~7 ~0 i8 u0 H/ ^
- }' e) a+ |8 S n1 i
- $ _% o# d! s6 N5 \" s( _+ u
- } else {4 a7 m! Z9 z: Q: J8 a
- System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());4 I" k$ ~ s# |
- ignore++;
( G4 a& t& I- V9 y* J: H- s$ n/ u6 x- N - }
" o( b5 O$ \ |$ E$ L -
9 }/ c+ n# G% V$ `) j5 E4 x - }4 x2 N5 m3 O) u
- / r0 I; b1 C+ G5 I% U
- }! G) m1 n! o5 T2 w4 w z) Q4 E2 P: @
- System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);
% n% W R0 [4 E& b -
% L; I+ T1 @; ?9 _ - }
4 g, E! w+ k/ b4 w0 z, | - }
复制代码
2 ] @6 E6 Q @9 w* K0 C9 e$ _9 JOutput- Testing.../ k& F6 V) {5 t4 m" L
- ' h# ~% C7 J* o& I/ E( q
- Priority :HIGH
5 q$ b& e% ]; w c - CreatedBy :mkyong.com
; N7 N' v: t4 z* X: ? - Tags :sales, <strong>test</strong>
1 U' K* _* h$ `9 ^& n - LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>
& [& C2 Q" A% U! ~+ d/ {" h9 K - 0 ~( s, V7 J5 ^6 V* `* e
- <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed # J( W! ~+ {5 W" t) [6 j; m
- <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed 0 U6 ]6 v6 X$ U" U- |
- <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored! N* _" f$ u5 H# K: H
- - d5 G# T- D; { k8 a9 j/ @
- 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>
复制代码 |
|