1
+ package test ;
2
+
3
+ import java .io .Closeable ;
4
+ import java .io .IOException ;
5
+
6
+ import org .junit .After ;
7
+ import org .junit .AfterClass ;
8
+ import org .junit .Before ;
9
+ import org .junit .BeforeClass ;
10
+ import org .junit .Test ;
11
+
12
+ public class TestFixturesExample {
13
+
14
+ // 進行函式的 Mock
15
+ static class ExpensiveManagedResource implements Closeable {
16
+ @ Override
17
+ public void close () throws IOException {}
18
+ }
19
+
20
+ static class ManagedResource implements Closeable {
21
+ @ Override
22
+ public void close () throws IOException {}
23
+ }
24
+
25
+ private ManagedResource myManagedResource ;
26
+ private static ExpensiveManagedResource myExpensiveManagedResource ;
27
+
28
+
29
+ @ BeforeClass
30
+ public static void setUpClass () {
31
+ System .out .println ("@BeforeClass setUpClass" );
32
+ myExpensiveManagedResource = new ExpensiveManagedResource ();
33
+ }
34
+
35
+ @ Before
36
+ public void setUp () {
37
+ System .out .println ("@Before setUp" );
38
+ this .myManagedResource = new ManagedResource ();
39
+ }
40
+
41
+
42
+ @ Test
43
+ public void test1 () {
44
+ System .out .println ("@Test test1()" );
45
+ }
46
+
47
+ @ Test
48
+ public void test2 () {
49
+ System .out .println ("@Test test2()" );
50
+ }
51
+
52
+ @ After
53
+ public void tearDown () throws IOException {
54
+ System .out .println ("@After tearDown" );
55
+
56
+ this .myManagedResource .close ();
57
+ this .myManagedResource = null ;
58
+ }
59
+
60
+ @ AfterClass
61
+ public static void tearDownClass () throws IOException {
62
+ System .out .println ("@AfterClass tearDownClass" );
63
+ myExpensiveManagedResource .close ();
64
+ myExpensiveManagedResource = null ;
65
+ }
66
+
67
+ }
0 commit comments