该用户从未签到
|
沙发
发表于 2014-11-18 22:25:14
|只看该作者
3. Unit Test Example0 z( k% G! g7 v4 r) x. q
新建一个简单的单元测试实例,并且通过新建的标注来标注:@Test and @TesterInfo* x" S- G- V4 K6 ~. O! s# k8 F& u: Y
# ~' |* `2 {9 K; F& a' h. i( ]/ f: pTestExample.java- package com.mkyong.test;
7 Z" r3 ~4 z6 a; E9 U: Q) C - % e$ G$ M/ T! h1 ]
- import com.mkyong.test.core.Test;
" k2 D. l' E; v- y, P4 I - import com.mkyong.test.core.TesterInfo;
( J* E$ p: X. j3 G0 z - import com.mkyong.test.core.TesterInfo.Priority;9 `, x8 R) E/ W" f4 }
-
3 V R3 h# G2 m4 ] - @TesterInfo(/ m9 M, S6 _+ M+ z
- priority = Priority.HIGH, ; } w% S, J7 S7 e% s" ]
- createdBy = "mkyong.com",
+ l8 d! f6 b& w1 k; i, }, ^, z6 v - tags = {"sales","test" }
4 Y+ x4 b* s: C. e - )7 c. I! o( B+ }3 A5 B& S. K) K
- public class TestExample {4 }4 V6 K# Z- x" N3 o* H: R
- " y$ a0 O" W' ?8 m) v. _
- @Test
8 N9 p( G( e; i4 v# E8 ?7 |+ l - void testA() {3 e9 h+ }9 u, s4 i7 h0 f- V
- if (true)9 t5 R4 [; `. }/ Y
- throw new RuntimeException("This test always failed");
3 ~ R2 ?7 S, I - }
% D: |* U- w7 I ~ -
( N1 y0 ]3 S9 d a% i' ~8 Q1 b - @Test(enabled = false)
4 T0 K9 Q0 C; s% r - void testB() {/ r! J" W9 V- u# R" m+ C
- if (false)$ y9 r C7 J( J. o7 n
- throw new RuntimeException("This test always passed");
9 i- A5 f9 o4 Q0 G0 f1 I/ K - }( M, O, e) T5 H% S4 I: |- `
-
% Z- t. `$ l8 e - @Test(enabled = true) |( R" ?) ` C3 S
- void testC() {* p1 @8 h- S/ @" [2 \% M' g; \9 B
- if (10 > 1) {% t; O0 P5 {: o5 m! `- ~/ v$ _
- // do nothing, this test always passed.
1 G' O9 B0 o. ]& E0 |# I - }% j/ s# N8 v! v; q* q) S& P
- }
6 E& a; B- ?/ R' e - - k0 `- p5 B+ M7 j4 }5 ]# |
- }
复制代码 . B! d6 w. t4 o1 s
4. Java reflection – Read the Annotation下面的例子演示如何通过java反射来读取和处理自定义标注。+ @7 a5 U% G, M; |4 [8 [) k
5 E( b, p5 ?3 D2 m- h
RunTest.java
" S* G2 G) |# p% q# b7 \- U: D1 R# @$ X* u1 J& L
- package com.mkyong.test;) J! u4 _$ K+ L" ^/ N
- 5 t" z# j* K) M/ W- i7 o9 d
- import java.lang.annotation.Annotation;! ^4 x D3 q$ V" b! F
- import java.lang.reflect.Method;
! D0 D. V4 Y# i) p/ g, D0 J -
* z+ v% l: [; R - import com.mkyong.test.core.Test;
( t& k. R/ N& e& k9 S w - import com.mkyong.test.core.TesterInfo;/ |% k1 n! {2 e! h m# t" Z
-
& v9 {+ Y1 p# H0 c - public class RunTest {3 i" J) g" w* [
-
2 x; l7 ~6 M/ }8 U5 k& U - public static void main(String[] args) throws Exception {! h, F4 @6 m9 B5 [: c8 K, K `* V
-
8 ^/ u5 ^& v, O) x4 z7 C - System.out.println("Testing...");
8 a$ ?9 \% ` c9 A) y. D -
5 D. I: N6 d0 m - int passed = 0, failed = 0, count = 0, ignore = 0;
4 s0 y9 h8 |8 L% l/ L -
& F# R* a1 }6 r5 D2 W' ` - Class<TestExample> obj = TestExample.class;
7 Y2 @* |# w2 T* J4 Z" G) S8 v -
% n. ?7 l, P9 Y& P8 {; g - // Process @TesterInfo
7 N2 q- @! \, s. o$ W - if (obj.isAnnotationPresent(TesterInfo.class)) {
4 E2 u) M9 m# {2 t# ]; ?( d - + Y! H8 x- t0 |( b
- Annotation annotation = obj.getAnnotation(TesterInfo.class);* B* j* W+ D, X7 H* A+ F V. A- M
- TesterInfo testerInfo = (TesterInfo) annotation;
6 v b! B& `8 d- G% J/ P- O -
, d5 N* J+ ?8 ?3 m - System.out.printf("%nPriority :%s", testerInfo.priority());
) f% E8 j% Q" f9 M- Z- ] - System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
& y* \5 X% x- M) J, @& \- [$ X3 P4 z - System.out.printf("%nTags :");- K& p: C# W$ r5 ~( S5 @) L
-
9 w+ n$ e- o; Z2 l - int tagLength = testerInfo.tags().length;
* T* S E: S4 w" K' F - for (String tag : testerInfo.tags()) {
2 s. O! |; [: Z' W$ Y' O9 ~/ F - if (tagLength > 1) {1 y9 |! K! [! V7 ^2 Z2 P
- System.out.print(tag + ", ");
% E$ V" ]1 v3 z5 ?0 o7 X7 G8 K- s5 B - } else {4 J) z/ {5 Y0 V- J7 n' G# b
- System.out.print(tag);/ m3 G1 d# z* g8 N L6 U8 |
- }( w8 [7 e5 d# ]# w
- tagLength--; V3 a7 y( K! H# G `
- }
% |6 M! M+ k1 Y: X) e# H - 2 c! t% m4 T* P; g+ }$ R: @& A$ G
- System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
* w7 C1 r0 O& I+ y- k' {0 x -
8 E' ]6 W" p0 K9 y- ~9 w9 G - } U) d- l. b' R
-
2 F% G* i; x+ f - // Process @Test
* t. P" C% H: N: ? V: F - for (Method method : obj.getDeclaredMethods()) {
; y% \+ H5 g+ X6 f8 j -
' C4 {& X7 A% Y; A1 W: h - // if method is annotated with @Test
* {# Z0 C5 i, s% B) c1 z0 h - if (method.isAnnotationPresent(Test.class)) {/ m2 V5 X/ p+ O
- 7 {. I+ \4 F/ a% [2 f$ L
- Annotation annotation = method.getAnnotation(Test.class);
$ ^% Z8 O& {% @ - Test test = (Test) annotation;
1 r3 ]. r3 o8 V% K! A - 5 N6 }+ E& ~7 b* ]# N
- // if enabled = true (default)
, q2 ]2 v& S, S0 N - if (test.enabled()) {
$ V9 M: h, W( }, T7 U2 h3 i -
- s! e1 D% [' b, Y/ W2 E! l# e: L - try {; T' c5 R& S6 z' P2 g
- method.invoke(obj.newInstance());$ n: n3 J, {" q& K8 H
- System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());
0 n* h9 M8 z; N$ E1 { - passed++;& f5 i$ G, Y% ^; o7 _5 m2 u, ]# U
- } catch (Throwable ex) {* j2 ~: \. z: W+ E& K V9 x+ \
- System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());( a& M% i3 h% N
- failed++;
0 V- [9 m3 `/ W# b - }
# F) g! M! _: N9 n -
9 ~" Q. p8 B! f" v, t! d3 e" y - } else {
7 n4 k7 p9 `4 ?3 ]& A - System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());! H2 u6 N$ R5 n( i
- ignore++;
~* A3 V# p- I" S& E - }
: ^0 U% A( l0 C9 x* V3 R, V - & x3 e" k$ b8 ?8 [5 Z6 a
- }
' S7 ]. K" t/ t8 V" O+ K* n9 d - , }( }. V( q$ j: |+ b% G1 N3 N
- }
4 E6 _; |* Q* ~% K3 L$ e; d - System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);
0 N: o% o: {, M9 C6 S' O/ { - : p- @& o# L* f: c; d: Z1 c
- }) ?8 }! E. E+ m# C
- }
复制代码
* `+ U" L% f9 h$ n) cOutput- Testing...7 V5 G9 ` O/ x6 X& ~0 N
-
" {6 Z6 P) T* G' Y- Y+ c! R - Priority :HIGH' x2 e2 \5 w$ G% B0 `
- CreatedBy :mkyong.com. G, M1 \: f" ]5 R: _
- Tags :sales, <strong>test</strong>1 s$ v% G0 ^. v) q# D& f0 y
- LastModified :03<strong>/</strong>01<strong>/</strong><span style="color:#000000">2014</span>' L' o: }& h% {
- - k- c+ i% P' d: ^
- <span style="color:#000000">1</span> - Test <span style="color:#ff0000">'testA'</span> - failed: java.lang.RuntimeException: This <strong>test</strong> always failed
( v* }: {6 Y6 ]9 j3 O* _: E: f7 I; k - <span style="color:#000000">2</span> - Test <span style="color:#ff0000">'testC'</span> - passed + P& I9 Y2 d3 w; ?* U# ~
- <span style="color:#000000">3</span> - Test <span style="color:#ff0000">'testB'</span> - ignored
% g: {8 K* ~# |) I. ]6 Q -
+ @% Q2 ~8 `- _" x" g" _ - 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>
复制代码 |
|