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, profile = true) 60 @Test testRunner_SimpleTest2_Profile()61 public void testRunner_SimpleTest2_Profile() throws Exception { 62 TestRunner runner = testRunnerRule.createTestRunner(); 63 runner.run(); 64 65 expectedResults() 66 .ensureProfilingWasRequested() 67 .success("Simple3") 68 .success("simple1") 69 .success("simple2") 70 .completedNormally(); 71 } 72 73 @TestRunnerProperties(testClass = SimpleTest2.class) 74 @Test testRunner_SkipPast()75 public void testRunner_SkipPast() throws Exception { 76 Class<?> testClass = testRunnerRule.testClass(); 77 String failingTestName = testClass.getName() + "#simple1"; 78 TestRunner runner = testRunnerRule.createTestRunner("--skipPast", failingTestName); 79 80 runner.run(); 81 82 expectedResults() 83 .success("simple2") 84 .completedNormally(); 85 } 86 87 @TestRunnerProperties(testClass = SimpleTest2.class) 88 @Test testRunner_SkipPastAll()89 public void testRunner_SkipPastAll() throws Exception { 90 Class<?> testClass = testRunnerRule.testClass(); 91 String failingTestName = testClass.getName() + "#other"; 92 TestRunner runner = testRunnerRule.createTestRunner("--skipPast", failingTestName); 93 94 runner.run(); 95 expectedResults().completedNormally(); 96 } 97 98 @TestRunnerProperties(testClass = SimpleTest2.class) 99 @Test testRunner_SimpleTest2_OneMethod()100 public void testRunner_SimpleTest2_OneMethod() throws Exception { 101 String[] args = {"simple2"}; 102 TestRunner runner = testRunnerRule.createTestRunner(args); 103 runner.run(); 104 105 expectedResults() 106 .success("simple2") 107 .completedNormally(); 108 } 109 110 @TestRunnerProperties(testClass = SimpleTest2.class) 111 @Test testRunner_SimpleTest2_TwoMethod()112 public void testRunner_SimpleTest2_TwoMethod() throws Exception { 113 String[] args = {"simple1", "Simple3"}; 114 TestRunner runner = testRunnerRule.createTestRunner(args); 115 runner.run(); 116 117 expectedResults() 118 .success("Simple3") 119 .success("simple1") 120 .completedNormally(); 121 } 122 123 @TestRunnerProperties(testClass = SimpleTest2.class) 124 @Test testRunner_SimpleTest2_WrongMethod()125 public void testRunner_SimpleTest2_WrongMethod() throws Exception { 126 String args = "testSimple5"; 127 TestRunner runner = testRunnerRule.createTestRunner(args); 128 runner.run(); 129 130 expectedResults() 131 .failure("testSimple5", 132 "junit.framework.AssertionFailedError: Method \"" + args 133 + "\" not found\n") 134 .completedNormally(); 135 } 136 137 @TestRunnerProperties(testClass = ExtendedSimpleTest2.class) 138 @Test testRunner_ExtendedSimple2()139 public void testRunner_ExtendedSimple2() throws Exception { 140 TestRunner runner = testRunnerRule.createTestRunner(); 141 runner.run(); 142 143 expectedResults() 144 .success("Simple3") 145 .success("simple1") 146 .success("simple2") 147 .success("simple4") 148 .completedNormally(); 149 } 150 151 @TestRunnerProperties( 152 testClass = ExtendedSimpleTest2.class, 153 testClassOrPackage = "vogar.target.junit4.ExtendedSimpleTest2#simple2") 154 @Test testRunner_ExtendedSimple2_QualifiedAndMethodNames()155 public void testRunner_ExtendedSimple2_QualifiedAndMethodNames() throws Exception { 156 String[] args = {"simple1", "simple4"}; 157 TestRunner runner = testRunnerRule.createTestRunner(args); 158 runner.run(); 159 160 expectedResults() 161 .success("simple1") 162 .success("simple2") 163 .success("simple4") 164 .completedNormally(); 165 } 166 167 @TestRunnerProperties(testClass = SimpleTest.class) 168 @Test testRunner_SimpleTest()169 public void testRunner_SimpleTest() throws Exception { 170 TestRunner runner = testRunnerRule.createTestRunner(); 171 runner.run(); 172 173 expectedResults() 174 .forTestClass(SimpleTest3.class) 175 .success("simple") 176 .completedNormally(); 177 } 178 179 @TestRunnerProperties(testClass = FailTest.class) 180 @Test testRunner_FailTest()181 public void testRunner_FailTest() throws Exception { 182 TestRunner runner = testRunnerRule.createTestRunner(); 183 runner.run(); 184 185 expectedResults() 186 .failure("failure", 187 "java.lang.AssertionError: failed.\n") 188 .success("success") 189 .success("throwAnotherExpectedException") 190 .failure("throwException", "java.lang.RuntimeException: exception\n") 191 .success("throwExpectedException") 192 .completedNormally(); 193 } 194 195 @TestRunnerProperties(testClass = FailConstructorTest.class) 196 @Test testRunner_FailConstructorTest()197 public void testRunner_FailConstructorTest() throws Exception { 198 TestRunner runner = testRunnerRule.createTestRunner(); 199 runner.run(); 200 201 expectedResults() 202 .failure("testSuccess", "" 203 + "java.lang.IllegalStateException: Constructor failed\n") 204 .completedNormally(); 205 } 206 207 @TestRunnerProperties(testClass = SuiteTest.class) 208 @Test testRunner_SuiteTest()209 public void testRunner_SuiteTest() throws Exception { 210 TestRunner runner = testRunnerRule.createTestRunner(); 211 runner.run(); 212 213 // The order is different from previous version of Vogar as that sorted a flattened list 214 // but JUnit has it organized as a hierarchy and sorts each level so classes which are on 215 // a separate level, like SimpleTest2 and SimpleTest3 are not sorted relative to each 216 // other. 217 expectedResults() 218 .forTestClass(SimpleTest3.class) 219 .success("simple") 220 .forTestClass(SimpleTest2.class) 221 .success("Simple3") 222 .success("simple1") 223 .success("simple2") 224 .completedNormally(); 225 } 226 227 @TestRunnerProperties(testClass = LongTest.class, timeout = 1) 228 @Test testRunner_LongTest_WithTimeout()229 public void testRunner_LongTest_WithTimeout() throws Exception { 230 TestRunner runner = testRunnerRule.createTestRunner(); 231 runner.run(); 232 233 // Note, that this doesn't complete normally. That's correct behavior as that will trigger 234 // the vogar process to restart the VM and run the tests from after this one. 235 expectedResults() 236 .failure("test", "java.util.concurrent.TimeoutException\n") 237 .aborted(); 238 } 239 240 @TestRunnerProperties(testClass = LongTest2.class) 241 @Test testRunner_LongTest2_WithoutTimeout()242 public void testRunner_LongTest2_WithoutTimeout() throws Exception { 243 TestRunner runner = testRunnerRule.createTestRunner(); 244 runner.run(); 245 246 expectedResults() 247 .success("test1") 248 .success("test2") 249 .success("test3") 250 .success("test4") 251 .success("test5") 252 .success("test6") 253 .success("test7") 254 .success("test8") 255 .completedNormally(); 256 } 257 258 @TestRunnerProperties(testClass = TestMethodWithParameterTest.class) 259 @Test testRunner_TestMethodWithParameterTest()260 public void testRunner_TestMethodWithParameterTest() throws Exception { 261 TestRunner runner = testRunnerRule.createTestRunner(); 262 runner.run(); 263 264 String methodName = "parameterized"; 265 expectedResults() 266 .failure(methodName, 267 "java.lang.Exception: Method " + methodName 268 + " should have no parameters\n") 269 .completedNormally(); 270 } 271 272 @TestRunnerProperties(testClass = TestMethodWithParameterTest.class) 273 @Test testRunner_TestMethodWithParameterTest_Requested()274 public void testRunner_TestMethodWithParameterTest_Requested() throws Exception { 275 String methodName = "parameterized"; 276 TestRunner runner = testRunnerRule.createTestRunner(methodName); 277 runner.run(); 278 279 // Ignores tests with no parameters. 280 expectedResults() 281 .failure(methodName, 282 "java.lang.Exception: Method " + methodName 283 + " should have no parameters\n") 284 .completedNormally(); 285 } 286 287 @TestRunnerProperties(testClass = NoMethodTest.class) 288 @Test testRunner_NoMethodTest()289 public void testRunner_NoMethodTest() throws Exception { 290 TestRunner runner = testRunnerRule.createTestRunner(); 291 runner.run(); 292 293 // Ignores tests with no parameters. 294 expectedResults() 295 .unsupported() 296 .completedNormally(); 297 } 298 299 @TestRunnerProperties(testClass = AnnotatedTestMethodsTest.class) 300 @Test testRunner_AnnotatedTestMethodsTest()301 public void testRunner_AnnotatedTestMethodsTest() throws Exception { 302 TestRunner runner = testRunnerRule.createTestRunner(); 303 runner.run(); 304 305 expectedResults() 306 .success("test1", "Before\nTest 1\nAfter\n") 307 .completedNormally(); 308 } 309 310 @TestRunnerProperties(testClass = AnnotatedMethodsTest.class) 311 @Test testRunner_AnnotatedMethodsTest()312 public void testRunner_AnnotatedMethodsTest() throws Exception { 313 TestRunner runner = testRunnerRule.createTestRunner(); 314 runner.run(); 315 316 expectedResults() 317 .text("Before Class\n") 318 .success("test1", "Before\nTest 1\nAfter\n") 319 .success("test2", "Before\nTest 2\nAfter\n") 320 .text("After Class\n") 321 .completedNormally(); 322 } 323 324 @TestRunnerProperties(testClass = LazyTestCreationTest.class) 325 @Test testRunner_LazyTestCreationTest()326 public void testRunner_LazyTestCreationTest() throws Exception { 327 TestRunner runner = testRunnerRule.createTestRunner(); 328 runner.run(); 329 330 expectedResults() 331 .text("Creating\n") 332 .success("test1") 333 .text("Creating\n") 334 .success("test2") 335 .completedNormally(); 336 } 337 338 // ========================================================================================= 339 // Place all JUnit4 specific test methods after this one. 340 // ========================================================================================= 341 342 @TestRunnerProperties(testClass = HasIgnoredTest.class) 343 @Test testRunner_HasIgnoredTest()344 public void testRunner_HasIgnoredTest() throws Exception { 345 TestRunner runner = testRunnerRule.createTestRunner(); 346 runner.run(); 347 348 expectedResults() 349 .success("working") 350 .completedNormally(); 351 } 352 353 @TestRunnerProperties(testClass = MockitoFieldTest.class) 354 @Test testRunner_MockitoFieldTest()355 public void testRunner_MockitoFieldTest() throws Exception { 356 TestRunner runner = testRunnerRule.createTestRunner(); 357 runner.run(); 358 359 expectedResults() 360 .success("test") 361 .completedNormally(); 362 } 363 364 @TestRunnerProperties(testClass = AssumeTest.class) 365 @Test testRunner_AssumeTest()366 public void testRunner_AssumeTest() throws Exception { 367 TestRunner runner = testRunnerRule.createTestRunner(); 368 runner.run(); 369 370 expectedResults() 371 .success("assumesCorrectly", "Assumption was correct\n") 372 .success("assumesIncorrectly") 373 .completedNormally(); 374 } 375 } 376