View Javadoc
1   package org.junit.tests.junit3compatibility;
2   
3   import junit.framework.AssertionFailedError;
4   import junit.framework.JUnit4TestAdapter;
5   import junit.framework.TestCase;
6   import junit.framework.TestFailure;
7   import junit.framework.TestListener;
8   import junit.framework.TestResult;
9   import org.junit.After;
10  import org.junit.AfterClass;
11  import org.junit.Before;
12  import org.junit.BeforeClass;
13  import org.junit.Test;
14  import org.junit.runner.Description;
15  import org.junit.runner.RunWith;
16  import org.junit.runner.Runner;
17  import org.junit.runner.notification.RunNotifier;
18  
19  public class ForwardCompatibilityTest extends TestCase {
20      static String fLog;
21  
22      static public class NewTest {
23          @Before
24          public void before() {
25              fLog += "before ";
26          }
27  
28          @After
29          public void after() {
30              fLog += "after ";
31          }
32  
33          @Test
34          public void test() {
35              fLog += "test ";
36          }
37      }
38  
39      public void testCompatibility() {
40          fLog = "";
41          TestResult result = new TestResult();
42          junit.framework.Test adapter = new JUnit4TestAdapter(NewTest.class);
43          adapter.run(result);
44          assertEquals("before test after ", fLog);
45      }
46  
47      public void testToString() {
48          JUnit4TestAdapter adapter = new JUnit4TestAdapter(NewTest.class);
49          junit.framework.Test test = adapter.getTests().get(0);
50          assertEquals(String.format("test(%s)", NewTest.class.getName()), test.toString());
51      }
52  
53      public void testUseGlobalCache() {
54          JUnit4TestAdapter adapter1 = new JUnit4TestAdapter(NewTest.class);
55          JUnit4TestAdapter adapter2 = new JUnit4TestAdapter(NewTest.class);
56          assertSame(adapter1.getTests().get(0), adapter2.getTests().get(0));
57      }
58  
59      static Exception exception = new Exception();
60  
61      public static class ErrorTest {
62          @Test
63          public void error() throws Exception {
64              throw exception;
65          }
66      }
67  
68      public void testException() {
69          TestResult result = new TestResult();
70          junit.framework.Test adapter = new JUnit4TestAdapter(ErrorTest.class);
71          adapter.run(result);
72          assertEquals(exception, result.errors().nextElement().thrownException());
73      }
74  
75      public void testNotifyResult() {
76          JUnit4TestAdapter adapter = new JUnit4TestAdapter(ErrorTest.class);
77          TestResult result = new TestResult();
78          final StringBuffer log = new StringBuffer();
79          result.addListener(new TestListener() {
80  
81              public void startTest(junit.framework.Test test) {
82                  log.append(" start ").append(test);
83              }
84  
85              public void endTest(junit.framework.Test test) {
86                  log.append(" end ").append(test);
87              }
88  
89              public void addFailure(junit.framework.Test test, AssertionFailedError t) {
90                  log.append(" failure ").append(test);
91              }
92  
93              public void addError(junit.framework.Test test, Throwable e) {
94                  log.append(" error " + test);
95              }
96          });
97          adapter.run(result);
98          String testName = String.format("error(%s)", ErrorTest.class.getName());
99          assertEquals(String.format(" start %s error %s end %s", testName, testName, testName), log.toString());
100     }
101 
102 
103     public static class NoExceptionTest {
104         @Test(expected = Exception.class)
105         public void succeed() throws Exception {
106         }
107     }
108 
109     public void testNoException() {
110         TestResult result = new TestResult();
111         junit.framework.Test adapter = new JUnit4TestAdapter(NoExceptionTest.class);
112         adapter.run(result);
113         assertFalse(result.wasSuccessful());
114     }
115 
116     public static class ExpectedTest {
117         @Test(expected = Exception.class)
118         public void expected() throws Exception {
119             throw new Exception();
120         }
121     }
122 
123     public void testExpected() {
124         TestResult result = new TestResult();
125         junit.framework.Test adapter = new JUnit4TestAdapter(ExpectedTest.class);
126         adapter.run(result);
127         assertTrue(result.wasSuccessful());
128     }
129 
130     public static class UnExpectedExceptionTest {
131         @Test(expected = Exception.class)
132         public void expected() throws Exception {
133             throw new Error();
134         }
135     }
136 
137     static String log;
138 
139     public static class BeforeClassTest {
140         @BeforeClass
141         public static void beforeClass() {
142             log += "before class ";
143         }
144 
145         @Before
146         public void before() {
147             log += "before ";
148         }
149 
150         @Test
151         public void one() {
152             log += "test ";
153         }
154 
155         @Test
156         public void two() {
157             log += "test ";
158         }
159 
160         @After
161         public void after() {
162             log += "after ";
163         }
164 
165         @AfterClass
166         public static void afterClass() {
167             log += "after class ";
168         }
169     }
170 
171     public void testBeforeAndAfterClass() {
172         log = "";
173         TestResult result = new TestResult();
174         junit.framework.Test adapter = new JUnit4TestAdapter(BeforeClassTest.class);
175         adapter.run(result);
176         assertEquals("before class before test after before test after after class ", log);
177     }
178 
179     public static class ExceptionInBeforeTest {
180         @Before
181         public void error() {
182             throw new Error();
183         }
184 
185         @Test
186         public void nothing() {
187         }
188     }
189 
190     public void testExceptionInBefore() {
191         TestResult result = new TestResult();
192         junit.framework.Test adapter = new JUnit4TestAdapter(ExceptionInBeforeTest.class);
193         adapter.run(result);
194         assertEquals(1, result.errorCount());
195     }
196 
197     public static class InvalidMethodTest {
198         @BeforeClass
199         public void shouldBeStatic() {
200         }
201 
202         @Test
203         public void aTest() {
204         }
205     }
206 
207     public void testInvalidMethod() {
208         TestResult result = new TestResult();
209         junit.framework.Test adapter = new JUnit4TestAdapter(InvalidMethodTest.class);
210         adapter.run(result);
211         assertEquals(1, result.errorCount());
212         TestFailure failure = result.errors().nextElement();
213         assertTrue(failure.exceptionMessage().contains("Method shouldBeStatic() should be static"));
214     }
215 
216     private static boolean wasRun = false;
217 
218     public static class MarkerRunner extends Runner {
219         public MarkerRunner(Class<?> klass) {
220         }
221 
222         @Override
223         public void run(RunNotifier notifier) {
224             wasRun = true;
225         }
226 
227         @Override
228         public int testCount() {
229             return 0;
230         }
231 
232         @Override
233         public Description getDescription() {
234             return Description.EMPTY;
235         }
236     }
237 
238     @RunWith(MarkerRunner.class)
239     public static class NoTests {
240     }
241 
242     public void testRunWithClass() {
243         wasRun = false;
244         TestResult result = new TestResult();
245         junit.framework.Test adapter = new JUnit4TestAdapter(NoTests.class);
246         adapter.run(result);
247         assertTrue(wasRun);
248     }
249 
250     public void testToStringSuite() {
251         junit.framework.Test adapter = new JUnit4TestAdapter(NoTests.class);
252         assertEquals(NoTests.class.getName(), adapter.toString());
253     }
254 }