1 package org.junit.tests.manipulation; 2 3 import static org.hamcrest.CoreMatchers.is; 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertThat; 6 7 import java.util.Arrays; 8 import java.util.List; 9 10 import junit.framework.JUnit4TestAdapter; 11 import org.junit.BeforeClass; 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 import org.junit.runner.RunWith; 18 import org.junit.runner.Runner; 19 import org.junit.runner.manipulation.Filter; 20 import org.junit.runner.manipulation.Filterable; 21 import org.junit.runner.manipulation.NoTestsRemainException; 22 import org.junit.runners.Parameterized; 23 import org.junit.runners.Parameterized.Parameters; 24 import org.junit.runners.Suite; 25 import org.junit.runners.Suite.SuiteClasses; 26 27 public class SingleMethodTest { 28 public static int count; 29 30 static public class OneTimeSetup { 31 @BeforeClass 32 public static void once() { 33 count++; 34 } 35 36 @Test 37 public void one() { 38 } 39 40 @Test 41 public void two() { 42 } 43 } 44 45 @Test 46 public void oneTimeSetup() throws Exception { 47 count = 0; 48 Runner runner = Request.method(OneTimeSetup.class, "one").getRunner(); 49 Result result = new JUnitCore().run(runner); 50 51 assertEquals(1, count); 52 assertEquals(1, result.getRunCount()); 53 } 54 55 @RunWith(Parameterized.class) 56 static public class ParameterizedOneTimeSetup { 57 @Parameters 58 public static List<Object[]> params() { 59 return Arrays.asList(new Object[]{1}, new Object[]{2}); 60 } 61 62 public ParameterizedOneTimeSetup(int x) { 63 } 64 65 @Test 66 public void one() { 67 } 68 } 69 70 @Test 71 public void parameterizedFilterToSingleMethod() throws Exception { 72 count = 0; 73 Runner runner = Request.method(ParameterizedOneTimeSetup.class, 74 "one[0]").getRunner(); 75 Result result = new JUnitCore().run(runner); 76 77 assertEquals(1, result.getRunCount()); 78 } 79 80 @RunWith(Parameterized.class) 81 static public class ParameterizedOneTimeBeforeClass { 82 @Parameters 83 public static List<Object[]> params() { 84 return Arrays.asList(new Object[]{1}, new Object[]{2}); 85 } 86 87 public ParameterizedOneTimeBeforeClass(int x) { 88 } 89 90 @BeforeClass 91 public static void once() { 92 count++; 93 } 94 95 @Test 96 public void one() { 97 } 98 } 99 100 101 @Test 102 public void parameterizedBeforeClass() throws Exception { 103 count = 0; 104 JUnitCore.runClasses(ParameterizedOneTimeBeforeClass.class); 105 assertEquals(1, count); 106 } 107 108 @Test 109 public void filteringAffectsPlan() throws Exception { 110 Runner runner = Request.method(OneTimeSetup.class, "one").getRunner(); 111 assertEquals(1, runner.testCount()); 112 } 113 114 @Test 115 public void nonexistentMethodCreatesFailure() throws Exception { 116 assertEquals(1, new JUnitCore().run( 117 Request.method(OneTimeSetup.class, "thisMethodDontExist")) 118 .getFailureCount()); 119 } 120 121 @Test(expected = NoTestsRemainException.class) 122 public void filteringAwayEverythingThrowsException() throws NoTestsRemainException { 123 Filterable runner = (Filterable) Request.aClass(OneTimeSetup.class).getRunner(); 124 runner.filter(new Filter() { 125 @Override 126 public boolean shouldRun(Description description) { 127 return false; 128 } 129 130 @Override 131 public String describe() { 132 return null; 133 } 134 }); 135 } 136 137 public static class TestOne { 138 @Test 139 public void a() { 140 } 141 142 @Test 143 public void b() { 144 } 145 } 146 147 public static class TestTwo { 148 @Test 149 public void a() { 150 } 151 152 @Test 153 public void b() { 154 } 155 } 156 157 @RunWith(Suite.class) 158 @SuiteClasses({TestOne.class, TestTwo.class}) 159 public static class OneTwoSuite { 160 } 161 162 @Test 163 public void eliminateUnnecessaryTreeBranches() throws Exception { 164 Runner runner = Request.aClass(OneTwoSuite.class).filterWith( 165 Description.createTestDescription(TestOne.class, "a")) 166 .getRunner(); 167 Description description = runner.getDescription(); 168 assertEquals(1, description.getChildren().size()); 169 } 170 171 public static class HasSuiteMethod { 172 @Test 173 public void a() { 174 } 175 176 @Test 177 public void b() { 178 } 179 180 public static junit.framework.Test suite() { 181 return new JUnit4TestAdapter(HasSuiteMethod.class); 182 } 183 } 184 185 @Test 186 public void classesWithSuiteMethodsAreFiltered() { 187 int testCount = Request.method(HasSuiteMethod.class, "a").getRunner().getDescription().testCount(); 188 assertThat(testCount, is(1)); 189 } 190 }