View Javadoc
1   package org.junit.tests.junit3compatibility;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertTrue;
6   import static org.junit.Assert.fail;
7   
8   import junit.framework.JUnit4TestAdapter;
9   import junit.framework.TestCase;
10  import junit.framework.TestSuite;
11  import org.junit.Ignore;
12  import org.junit.Test;
13  import org.junit.runner.Description;
14  import org.junit.runner.JUnitCore;
15  import org.junit.runner.Request;
16  import org.junit.runner.Result;
17  
18  public class SuiteMethodTest {
19      public static boolean wasRun;
20  
21      static public class OldTest extends TestCase {
22          public OldTest(String name) {
23              super(name);
24          }
25  
26          public static junit.framework.Test suite() {
27              TestSuite suite = new TestSuite();
28              suite.addTest(new OldTest("notObviouslyATest"));
29              return suite;
30          }
31  
32          public void notObviouslyATest() {
33              wasRun = true;
34          }
35      }
36  
37      @Test
38      public void makeSureSuiteIsCalled() {
39          wasRun = false;
40          JUnitCore.runClasses(OldTest.class);
41          assertTrue(wasRun);
42      }
43  
44      static public class NewTest {
45          @Test
46          public void sample() {
47              wasRun = true;
48          }
49  
50          public static junit.framework.Test suite() {
51              return new JUnit4TestAdapter(NewTest.class);
52          }
53      }
54  
55      @Test
56      public void makeSureSuiteWorksWithJUnit4Classes() {
57          wasRun = false;
58          JUnitCore.runClasses(NewTest.class);
59          assertTrue(wasRun);
60      }
61  
62  
63      public static class CompatibilityTest {
64          @Ignore
65          @Test
66          public void ignored() {
67          }
68  
69          public static junit.framework.Test suite() {
70              return new JUnit4TestAdapter(CompatibilityTest.class);
71          }
72      }
73  
74      // when executing as JUnit 3, ignored tests are stripped out before execution
75      @Test
76      public void descriptionAndRunNotificationsAreConsistent() {
77          Result result = JUnitCore.runClasses(CompatibilityTest.class);
78          assertEquals(0, result.getIgnoreCount());
79  
80          Description description = Request.aClass(CompatibilityTest.class).getRunner().getDescription();
81          assertEquals(0, description.getChildren().size());
82      }
83  
84      static public class NewTestSuiteFails {
85          @Test
86          public void sample() {
87              wasRun = true;
88          }
89  
90          public static junit.framework.Test suite() {
91              fail("called with JUnit 4 runner");
92              return null;
93          }
94      }
95  
96      @Test
97      public void suiteIsUsedWithJUnit4Classes() {
98          wasRun = false;
99          Result result = JUnitCore.runClasses(NewTestSuiteFails.class);
100         assertEquals(1, result.getFailureCount());
101         assertFalse(wasRun);
102     }
103 
104     static public class NewTestSuiteNotUsed {
105         private static boolean wasIgnoredRun;
106 
107         @Test
108         public void sample() {
109             wasRun = true;
110         }
111 
112         @Ignore
113         @Test
114         public void ignore() {
115             wasIgnoredRun = true;
116         }
117 
118         public static junit.framework.Test suite() {
119             return new JUnit4TestAdapter(NewTestSuiteNotUsed.class);
120         }
121     }
122 
123     @Test
124     public void makeSureSuiteNotUsedWithJUnit4Classes2() {
125         wasRun = false;
126         NewTestSuiteNotUsed.wasIgnoredRun = false;
127         Result res = JUnitCore.runClasses(NewTestSuiteNotUsed.class);
128         assertTrue(wasRun);
129         assertFalse(NewTestSuiteNotUsed.wasIgnoredRun);
130         assertEquals(0, res.getFailureCount());
131         assertEquals(1, res.getRunCount());
132         assertEquals(0, res.getIgnoreCount());
133     }
134 }