1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package vogar.target.junit4; 18 19 import org.junit.Test; 20 import org.junit.runner.RunWith; 21 import org.junit.runners.JUnit4; 22 import vogar.target.AbstractTestRunnerTest; 23 import vogar.target.TestRunner; 24 import vogar.target.TestRunnerProperties; 25 26 /** 27 * Tests for using TestRunner to run JUnit 4 classes. 28 */ 29 @RunWith(JUnit4.class) 30 public class TestRunnerJUnit4Test extends AbstractTestRunnerTest { 31 32 @TestRunnerProperties(testClass = ChangeDefaultLocaleTest.class) 33 @Test testRunner_ChangeDefaultLocaleTest()34 public void testRunner_ChangeDefaultLocaleTest() throws Exception { 35 TestRunner runner = testRunnerRule.createTestRunner(); 36 runner.run(); 37 38 // Note, that this doesn't complete normally. That's correct behavior as that will trigger 39 // the vogar process to restart the VM and run the tests from after this one. 40 expectedResults() 41 .success("testDefault_Locale_CANADA") 42 .success("testDefault_Locale_CHINA") 43 .completedNormally(); 44 } 45 46 @TestRunnerProperties(testClass = SimpleTest2.class) 47 @Test testRunner_SimpleTest2()48 public void testRunner_SimpleTest2() throws Exception { 49 TestRunner runner = testRunnerRule.createTestRunner(); 50 runner.run(); 51 52 expectedResults() 53 .success("Simple3") 54 .success("simple1") 55 .success("simple2") 56 .completedNormally(); 57 } 58 59 @TestRunnerProperties(testClass = SimpleTest2.class) 60 @Test testRunner_SkipPast()61 public void testRunner_SkipPast() throws Exception { 62 Class<?> testClass = testRunnerRule.testClass(); 63 String failingTestName = testClass.getName() + "#simple1"; 64 TestRunner runner = testRunnerRule.createTestRunner("--skipPast", failingTestName); 65 66 runner.run(); 67 68 expectedResults() 69 .success("simple2") 70 .completedNormally(); 71 } 72 73 @TestRunnerProperties(testClass = SimpleTest2.class) 74 @Test testRunner_SkipPastAll()75 public void testRunner_SkipPastAll() throws Exception { 76 Class<?> testClass = testRunnerRule.testClass(); 77 String failingTestName = testClass.getName() + "#other"; 78 TestRunner runner = testRunnerRule.createTestRunner("--skipPast", failingTestName); 79 80 runner.run(); 81 expectedResults().completedNormally(); 82 } 83 84 @TestRunnerProperties(testClass = SimpleTest2.class) 85 @Test testRunner_SimpleTest2_OneMethod()86 public void testRunner_SimpleTest2_OneMethod() throws Exception { 87 String[] args = {"simple2"}; 88 TestRunner runner = testRunnerRule.createTestRunner(args); 89 runner.run(); 90 91 expectedResults() 92 .success("simple2") 93 .completedNormally(); 94 } 95 96 @TestRunnerProperties(testClass = SimpleTest2.class) 97 @Test testRunner_SimpleTest2_TwoMethod()98 public void testRunner_SimpleTest2_TwoMethod() throws Exception { 99 String[] args = {"simple1", "Simple3"}; 100 TestRunner runner = testRunnerRule.createTestRunner(args); 101 runner.run(); 102 103 expectedResults() 104 .success("Simple3") 105 .success("simple1") 106 .completedNormally(); 107 } 108 109 @TestRunnerProperties(testClass = SimpleTest2.class) 110 @Test testRunner_SimpleTest2_WrongMethod()111 public void testRunner_SimpleTest2_WrongMethod() throws Exception { 112 String args = "testSimple5"; 113 TestRunner runner = testRunnerRule.createTestRunner(args); 114 runner.run(); 115 116 expectedResults() 117 .failure("testSimple5", 118 "junit.framework.AssertionFailedError: Method \"" + args 119 + "\" not found\n") 120 .completedNormally(); 121 } 122 123 @TestRunnerProperties(testClass = ExtendedSimpleTest2.class) 124 @Test testRunner_ExtendedSimple2()125 public void testRunner_ExtendedSimple2() throws Exception { 126 TestRunner runner = testRunnerRule.createTestRunner(); 127 runner.run(); 128 129 expectedResults() 130 .success("Simple3") 131 .success("simple1") 132 .success("simple2") 133 .success("simple4") 134 .completedNormally(); 135 } 136 137 @TestRunnerProperties( 138 testClass = ExtendedSimpleTest2.class, 139 testClassOrPackage = "vogar.target.junit4.ExtendedSimpleTest2#simple2") 140 @Test testRunner_ExtendedSimple2_QualifiedAndMethodNames()141 public void testRunner_ExtendedSimple2_QualifiedAndMethodNames() throws Exception { 142 String[] args = {"simple1", "simple4"}; 143 TestRunner runner = testRunnerRule.createTestRunner(args); 144 runner.run(); 145 146 expectedResults() 147 .success("simple1") 148 .success("simple2") 149 .success("simple4") 150 .completedNormally(); 151 } 152 153 @TestRunnerProperties(testClass = SimpleTest.class) 154 @Test testRunner_SimpleTest()155 public void testRunner_SimpleTest() throws Exception { 156 TestRunner runner = testRunnerRule.createTestRunner(); 157 runner.run(); 158 159 expectedResults() 160 .forTestClass(SimpleTest3.class) 161 .success("simple") 162 .completedNormally(); 163 } 164 165 @TestRunnerProperties(testClass = FailTest.class) 166 @Test testRunner_FailTest()167 public void testRunner_FailTest() throws Exception { 168 TestRunner runner = testRunnerRule.createTestRunner(); 169 runner.run(); 170 171 expectedResults() 172 .failure("failure", 173 "java.lang.AssertionError: failed.\n") 174 .success("success") 175 .success("throwAnotherExpectedException") 176 .failure("throwException", "java.lang.RuntimeException: exception\n") 177 .success("throwExpectedException") 178 .completedNormally(); 179 } 180 181 @TestRunnerProperties(testClass = FailConstructorTest.class) 182 @Test testRunner_FailConstructorTest()183 public void testRunner_FailConstructorTest() throws Exception { 184 TestRunner runner = testRunnerRule.createTestRunner(); 185 runner.run(); 186 187 expectedResults() 188 .failure("testSuccess", "" 189 + "java.lang.IllegalStateException: Constructor failed\n") 190 .completedNormally(); 191 } 192 193 @TestRunnerProperties(testClass = SuiteTest.class) 194 @Test testRunner_SuiteTest()195 public void testRunner_SuiteTest() throws Exception { 196 TestRunner runner = testRunnerRule.createTestRunner(); 197 runner.run(); 198 199 // The order is different from previous version of Vogar as that sorted a flattened list 200 // but JUnit has it organized as a hierarchy and sorts each level so classes which are on 201 // a separate level, like SimpleTest2 and SimpleTest3 are not sorted relative to each 202 // other. 203 expectedResults() 204 .forTestClass(SimpleTest3.class) 205 .success("simple") 206 .forTestClass(SimpleTest2.class) 207 .success("Simple3") 208 .success("simple1") 209 .success("simple2") 210 .completedNormally(); 211 } 212 213 @TestRunnerProperties(testClass = LongTest.class, timeout = 1) 214 @Test testRunner_LongTest_WithTimeout()215 public void testRunner_LongTest_WithTimeout() throws Exception { 216 TestRunner runner = testRunnerRule.createTestRunner(); 217 runner.run(); 218 219 // Note, that this doesn't complete normally. That's correct behavior as that will trigger 220 // the vogar process to restart the VM and run the tests from after this one. 221 expectedResults() 222 .failure("test", "java.util.concurrent.TimeoutException\n") 223 .aborted(); 224 } 225 226 @TestRunnerProperties(testClass = LongTest2.class) 227 @Test testRunner_LongTest2_WithoutTimeout()228 public void testRunner_LongTest2_WithoutTimeout() throws Exception { 229 TestRunner runner = testRunnerRule.createTestRunner(); 230 runner.run(); 231 232 expectedResults() 233 .success("test1") 234 .success("test2") 235 .success("test3") 236 .success("test4") 237 .success("test5") 238 .success("test6") 239 .success("test7") 240 .success("test8") 241 .completedNormally(); 242 } 243 244 @TestRunnerProperties(testClass = TestMethodWithParameterTest.class) 245 @Test testRunner_TestMethodWithParameterTest()246 public void testRunner_TestMethodWithParameterTest() throws Exception { 247 TestRunner runner = testRunnerRule.createTestRunner(); 248 runner.run(); 249 250 String methodName = "parameterized"; 251 expectedResults() 252 .failure(methodName, 253 "java.lang.Exception: Method " + methodName 254 + " should have no parameters\n") 255 .completedNormally(); 256 } 257 258 @TestRunnerProperties(testClass = TestMethodWithParameterTest.class) 259 @Test testRunner_TestMethodWithParameterTest_Requested()260 public void testRunner_TestMethodWithParameterTest_Requested() throws Exception { 261 String methodName = "parameterized"; 262 TestRunner runner = testRunnerRule.createTestRunner(methodName); 263 runner.run(); 264 265 // Ignores tests with no parameters. 266 expectedResults() 267 .failure(methodName, 268 "java.lang.Exception: Method " + methodName 269 + " should have no parameters\n") 270 .completedNormally(); 271 } 272 273 @TestRunnerProperties(testClass = NoMethodTest.class) 274 @Test testRunner_NoMethodTest()275 public void testRunner_NoMethodTest() throws Exception { 276 TestRunner runner = testRunnerRule.createTestRunner(); 277 runner.run(); 278 279 // Ignores tests with no parameters. 280 expectedResults() 281 .unsupported() 282 .completedNormally(); 283 } 284 285 @TestRunnerProperties(testClass = AnnotatedTestMethodsTest.class) 286 @Test testRunner_AnnotatedTestMethodsTest()287 public void testRunner_AnnotatedTestMethodsTest() throws Exception { 288 TestRunner runner = testRunnerRule.createTestRunner(); 289 runner.run(); 290 291 expectedResults() 292 .success("test1", "Before\nTest 1\nAfter\n") 293 .completedNormally(); 294 } 295 296 @TestRunnerProperties(testClass = AnnotatedMethodsTest.class) 297 @Test testRunner_AnnotatedMethodsTest()298 public void testRunner_AnnotatedMethodsTest() throws Exception { 299 TestRunner runner = testRunnerRule.createTestRunner(); 300 runner.run(); 301 302 expectedResults() 303 .text("Before Class\n") 304 .success("test1", "Before\nTest 1\nAfter\n") 305 .success("test2", "Before\nTest 2\nAfter\n") 306 .text("After Class\n") 307 .completedNormally(); 308 } 309 310 @TestRunnerProperties(testClass = LazyTestCreationTest.class) 311 @Test testRunner_LazyTestCreationTest()312 public void testRunner_LazyTestCreationTest() throws Exception { 313 TestRunner runner = testRunnerRule.createTestRunner(); 314 runner.run(); 315 316 expectedResults() 317 .text("Creating\n") 318 .success("test1") 319 .text("Creating\n") 320 .success("test2") 321 .completedNormally(); 322 } 323 324 // ========================================================================================= 325 // Place all JUnit4 specific test methods after this one. 326 // ========================================================================================= 327 328 @TestRunnerProperties(testClass = HasIgnoredTest.class) 329 @Test testRunner_HasIgnoredTest()330 public void testRunner_HasIgnoredTest() throws Exception { 331 TestRunner runner = testRunnerRule.createTestRunner(); 332 runner.run(); 333 334 expectedResults() 335 .success("working") 336 .completedNormally(); 337 } 338 339 @TestRunnerProperties(testClass = MockitoFieldTest.class) 340 @Test testRunner_MockitoFieldTest()341 public void testRunner_MockitoFieldTest() throws Exception { 342 TestRunner runner = testRunnerRule.createTestRunner(); 343 runner.run(); 344 345 expectedResults() 346 .success("test") 347 .completedNormally(); 348 } 349 350 @TestRunnerProperties(testClass = AssumeTest.class) 351 @Test testRunner_AssumeTest()352 public void testRunner_AssumeTest() throws Exception { 353 TestRunner runner = testRunnerRule.createTestRunner(); 354 runner.run(); 355 356 expectedResults() 357 .success("assumesCorrectly", "Assumption was correct\n") 358 .success("assumesIncorrectly") 359 .completedNormally(); 360 } 361 } 362