...18import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;19import com.google.errorprone.CompilationTestHelper;20import org.junit.Test;21import org.junit.runner.RunWith;22import org.junit.runners.JUnit4;23/** @author eaftan@google.com (Eddie Aftandilian) */24@RunWith(JUnit4.class)25public class JUnit4TestNotRunTest {26 private final CompilationTestHelper compilationHelper =27 CompilationTestHelper.newInstance(JUnit4TestNotRun.class, getClass());28 private final BugCheckerRefactoringTestHelper refactoringHelper =29 BugCheckerRefactoringTestHelper.newInstance(new JUnit4TestNotRun(), getClass());30 @Test31 public void testPositiveCase1() {32 compilationHelper.addSourceFile("JUnit4TestNotRunPositiveCase1.java").doTest();33 }34 @Test35 public void testPositiveCase2() {36 compilationHelper.addSourceFile("JUnit4TestNotRunPositiveCase2.java").doTest();37 }38 @Test39 public void containsVerifyAsIdentifier_shouldBeTest() {40 compilationHelper41 .addSourceLines(42 "Test.java",43 "import static org.mockito.Mockito.verify;",44 "import org.junit.runner.RunWith;",45 "import org.junit.runners.JUnit4;",46 "@RunWith(JUnit4.class)",47 "public class Test {",48 " // BUG: Diagnostic contains: @Test",49 " public void shouldDoSomething() {",50 " verify(null);",51 " }",52 "}")53 .doTest();54 }55 @Test56 public void containsQualifiedVerify_shouldBeTest() {57 compilationHelper58 .addSourceLines(59 "Test.java",60 "import org.junit.runner.RunWith;",61 "import org.junit.runners.JUnit4;",62 "import org.mockito.Mockito;",63 "@RunWith(JUnit4.class)",64 "public class Test {",65 " // BUG: Diagnostic contains: @Test",66 " public void shouldDoSomething() {",67 " Mockito.verify(null);",68 " }",69 "}")70 .doTest();71 }72 @Test73 public void containsAssertAsIdentifier_shouldBeTest() {74 compilationHelper75 .addSourceLines(76 "Test.java",77 "import org.junit.runner.RunWith;",78 "import org.junit.runners.JUnit4;",79 "import static com.google.common.truth.Truth.assertThat;",80 "import java.util.Collections;",81 "@RunWith(JUnit4.class)",82 "public class Test {",83 " // BUG: Diagnostic contains: @Test",84 " public void shouldDoSomething() {",85 " assertThat(2).isEqualTo(2);",86 " }",87 " // BUG: Diagnostic contains: @Test",88 " public void shouldDoTwoThings() {",89 " Collections.sort(Collections.<Integer>emptyList());",90 " assertThat(3).isEqualTo(3);",91 " }",92 "}")93 .doTest();94 }95 @Test96 public void containsQualifiedAssert_shouldBeTest() {97 compilationHelper98 .addSourceLines(99 "Test.java",100 "import org.junit.runner.RunWith;",101 "import org.junit.runners.JUnit4;",102 "import com.google.common.truth.Truth;",103 "@RunWith(JUnit4.class)",104 "public class Test {",105 " // BUG: Diagnostic contains: @Test",106 " public void shouldDoSomething() {",107 " Truth.assertThat(1).isEqualTo(1);",108 " }",109 "}")110 .doTest();111 }112 @Test113 public void containsCheckAsIdentifier_shouldBeTest() {114 compilationHelper115 .addSourceLines(116 "Test.java",117 "import static com.google.common.base.Preconditions.checkState;",118 "import org.junit.runner.RunWith;",119 "import org.junit.runners.JUnit4;",120 "@RunWith(JUnit4.class)",121 "public class Test {",122 " // BUG: Diagnostic contains: @Test",123 " public void shouldDoSomething() {",124 " checkState(false);",125 " }",126 "}")127 .doTest();128 }129 @Test130 public void containsQualifiedCheck_shouldBeTest() {131 compilationHelper132 .addSourceLines(133 "Test.java",134 "import com.google.common.base.Preconditions;",135 "import org.junit.runner.RunWith;",136 "import org.junit.runners.JUnit4;",137 "@RunWith(JUnit4.class)",138 "public class Test {",139 " // BUG: Diagnostic contains: @Test",140 " public void shouldDoSomething() {",141 " Preconditions.checkState(false);",142 " }",143 "}")144 .doTest();145 }146 @Test147 public void containsFailAsIdentifier_shouldBeTest() {148 compilationHelper149 .addSourceLines(150 "Test.java",151 "import static org.junit.Assert.fail;",152 "import org.junit.runner.RunWith;",153 "import org.junit.runners.JUnit4;",154 "@RunWith(JUnit4.class)",155 "public class Test {",156 " // BUG: Diagnostic contains: @Test",157 " public void shouldDoSomething() {",158 " fail();",159 " }",160 "}")161 .doTest();162 }163 @Test164 public void containsQualifiedFail_shouldBeTest() {165 compilationHelper166 .addSourceLines(167 "Test.java",168 "import org.junit.Assert;",169 "import org.junit.runner.RunWith;",170 "import org.junit.runners.JUnit4;",171 "@RunWith(JUnit4.class)",172 "public class Test {",173 " // BUG: Diagnostic contains: @Test",174 " public void shouldDoSomething() {",175 " Assert.fail();",176 " }",177 "}")178 .doTest();179 }180 @Test181 public void containsExpectAsIdentifier_shouldBeTest() {182 compilationHelper183 .addSourceLines(184 "Test.java",185 "import static org.junit.Assert.assertThrows;",186 "import org.junit.runner.RunWith;",187 "import org.junit.runners.JUnit4;",188 "@RunWith(JUnit4.class)",189 "public class Test {",190 " // BUG: Diagnostic contains: @Test",191 " public void shouldDoSomething() {",192 " assertThrows(null, null);",193 " }",194 "}")195 .doTest();196 }197 @Test198 public void containsQualifiedExpect_shouldBeTest() {199 compilationHelper200 .addSourceLines(201 "Test.java",202 "import org.junit.Assert;",203 "import org.junit.runner.RunWith;",204 "import org.junit.runners.JUnit4;",205 "@RunWith(JUnit4.class)",206 "public class Test {",207 " // BUG: Diagnostic contains: @Test",208 " public void shouldDoSomething() {",209 " Assert.assertThrows(null, null);",210 " }",211 "}")212 .doTest();213 }214 @Test215 public void noTestKeyword_notATest() {216 compilationHelper217 .addSourceLines(218 "Test.java",219 "import org.junit.runner.RunWith;",220 "import org.junit.runners.JUnit4;",221 "import java.util.Collections;",222 "@RunWith(JUnit4.class)",223 "public class Test {",224 " public void shouldDoSomething() {",225 " Collections.sort(Collections.<Integer>emptyList());",226 " }",227 "}")228 .doTest();229 }230 @Test231 public void staticMethodWithTestKeyword_notATest() {232 compilationHelper233 .addSourceLines(234 "Test.java",235 "import org.junit.runner.RunWith;",236 "import org.junit.runners.JUnit4;",237 "import java.util.Collections;",238 "@RunWith(JUnit4.class)",239 "public class Test {",240 " private static void assertDoesSomething() {}",241 " public static void shouldDoSomething() {",242 " assertDoesSomething();",243 " }",244 "}")245 .doTest();246 }247 @Test248 public void hasOtherAnnotation_notATest() {249 compilationHelper250 .addSourceLines(251 "Test.java",252 "import org.junit.runner.RunWith;",253 "import org.junit.runners.JUnit4;",254 "@RunWith(JUnit4.class)",255 "public class Test {",256 " @Deprecated",257 " public void shouldDoSomething() {",258 " verify();",259 " }",260 " private void verify() {}",261 "}")262 .doTest();263 }264 @Test265 public void hasOtherAnnotationAndNamedTest_shouldBeTest() {266 compilationHelper267 .addSourceLines(268 "Test.java",269 "import org.junit.runner.RunWith;",270 "import org.junit.runners.JUnit4;",271 "import java.util.Collections;",272 "@RunWith(JUnit4.class)",273 "public class Test {",274 " @Deprecated",275 " // BUG: Diagnostic contains: @Test",276 " public void testShouldDoSomething() {",277 " Collections.sort(Collections.<Integer>emptyList());",278 " }",279 " private void verify() {}",280 "}")281 .doTest();282 }283 @Test284 public void shouldNotDetectMethodsOnAbstractClass() {285 compilationHelper286 .addSourceLines(287 "Test.java",288 "import org.junit.runner.RunWith;",289 "import org.junit.runners.JUnit4;",290 "@RunWith(JUnit4.class)",291 "public abstract class Test {",292 " public void testDoSomething() {}",293 "}")294 .doTest();295 }296 @Test297 public void testFix() {298 refactoringHelper299 .addInputLines(300 "in/TestStuff.java",301 "import org.junit.runner.RunWith;",302 "import org.junit.runners.JUnit4;",303 "@RunWith(JUnit4.class)",304 "public class TestStuff {",305 " public void testDoSomething() {}",306 "}")307 .addOutputLines(308 "out/TestStuff.java",309 "import org.junit.Test;",310 "import org.junit.runner.RunWith;",311 "import org.junit.runners.JUnit4;",312 "@RunWith(JUnit4.class)",313 "public class TestStuff {",314 " @Test",315 " public void testDoSomething() {}",316 "}")317 .setFixChooser(FixChoosers.FIRST)318 .doTest();319 }320 @Test321 public void ignoreFix() {322 refactoringHelper323 .addInputLines(324 "in/TestStuff.java",325 "import org.junit.runner.RunWith;",326 "import org.junit.runners.JUnit4;",327 "@RunWith(JUnit4.class)",328 "public class TestStuff {",329 " public void testDoSomething() {}",330 "}")331 .addOutputLines(332 "out/TestStuff.java",333 "import org.junit.Ignore;",334 "import org.junit.Test;",335 "import org.junit.runner.RunWith;",336 "import org.junit.runners.JUnit4;",337 "@RunWith(JUnit4.class)",338 "public class TestStuff {",339 " @Test @Ignore public void testDoSomething() {}",340 "}")341 .setFixChooser(FixChoosers.SECOND)342 .doTest();343 }344 @Test345 public void makePrivateFix() {346 refactoringHelper347 .addInputLines(348 "in/TestStuff.java",349 "import org.junit.runner.RunWith;",350 "import org.junit.runners.JUnit4;",351 "@RunWith(JUnit4.class)",352 "public class TestStuff {",353 " public void testDoSomething() {}",354 "}")355 .addOutputLines(356 "out/TestStuff.java",357 "import org.junit.runner.RunWith;",358 "import org.junit.runners.JUnit4;",359 "@RunWith(JUnit4.class)",360 "public class TestStuff {",361 " private void testDoSomething() {}",362 "}")363 .setFixChooser(FixChoosers.THIRD)364 .doTest();365 }366 @Test367 public void ignoreFixComesFirstWhenTestNamedDisabled() {368 refactoringHelper369 .addInputLines(370 "in/TestStuff.java",371 "import org.junit.runner.RunWith;",372 "import org.junit.runners.JUnit4;",373 "@RunWith(JUnit4.class)",374 "public class TestStuff {",375 " public void disabledTestDoSomething() {",376 " verify();",377 " }",378 " void verify() {}",379 "}")380 .addOutputLines(381 "out/TestStuff.java",382 "import org.junit.Ignore;",383 "import org.junit.Test;",384 "import org.junit.runner.RunWith;",385 "import org.junit.runners.JUnit4;",386 "@RunWith(JUnit4.class)",387 "public class TestStuff {",388 " @Test @Ignore public void disabledTestDoSomething() {",389 " verify();",390 " }",391 " void verify() {}",392 "}")393 .setFixChooser(FixChoosers.FIRST)394 .doTest();395 }396 @Test397 public void helperMethodCalledElsewhere() {398 compilationHelper399 .addSourceLines(400 "TestStuff.java",401 "import org.junit.runner.RunWith;",402 "import org.junit.runners.JUnit4;",403 "import org.junit.Test;",404 "@RunWith(JUnit4.class)",405 "public class TestStuff {",406 " public void shouldDoSomething() {",407 " verify();",408 " }",409 " @Test",410 " public void testDoesSomething() {",411 " shouldDoSomething();",412 " }",413 " void verify() {}",414 "}")415 .doTest();416 }417 @Test418 public void helperMethodCallFoundInNestedInvocation() {419 compilationHelper420 .addSourceLines(421 "TestStuff.java",422 "import org.junit.runner.RunWith;",423 "import org.junit.runners.JUnit4;",424 "import org.junit.Test;",425 "import java.util.function.Consumer;",426 "@RunWith(JUnit4.class)",427 "public class TestStuff {",428 " public void shouldDoSomething() {",429 " verify();",430 " }",431 " @Test",432 " public void testDoesSomething() {",433 " doSomething(u -> shouldDoSomething());",434 " }",435 " void doSomething(Consumer f) {}",436 " void verify() {}",437 "}")438 .doTest();439 }440 @Test441 public void runWithNotRequired() {442 compilationHelper443 .addSourceLines(444 "TestStuff.java",445 "import org.junit.Test;",446 "public class TestStuff {",447 " // BUG: Diagnostic contains: @Test",448 " public void testDoesSomething() {}",449 " @Test",450 " public void foo() {}",451 "}")452 .doTest();453 }454 @Test455 public void testNegativeCase1() {456 compilationHelper.addSourceFile("JUnit4TestNotRunNegativeCase1.java").doTest();457 }458 @Test459 public void testNegativeCase2() {460 compilationHelper.addSourceFile("JUnit4TestNotRunNegativeCase2.java").doTest();461 }462 @Test463 public void testNegativeCase3() {464 compilationHelper.addSourceFile("JUnit4TestNotRunNegativeCase3.java").doTest();465 }466 @Test467 public void testNegativeCase4() {468 compilationHelper.addSourceFile("JUnit4TestNotRunNegativeCase4.java").doTest();469 }470 @Test471 public void testNegativeCase5() {472 compilationHelper473 .addSourceFile("JUnit4TestNotRunBaseClass.java")474 .addSourceFile("JUnit4TestNotRunNegativeCase5.java")475 .doTest();476 }477 @Test478 public void junit3TestWithIgnore() {479 compilationHelper480 .addSourceLines(481 "TestStuff.java",482 "import org.junit.Ignore;",483 "import org.junit.runner.RunWith;",484 "import org.junit.runners.JUnit4;",485 "@RunWith(JUnit4.class)",486 "public class TestStuff {",487 " @Ignore",488 " public void testMethod() {}",489 "}")490 .doTest();491 }492 @Test493 public void junit4Theory_isTestAnnotation() {494 compilationHelper495 .addSourceLines(496 "TestTheories.java",497 "import org.junit.runner.RunWith;",498 "import org.junit.experimental.theories.Theories;",499 "import org.junit.experimental.theories.Theory;",500 "@RunWith(Theories.class)",501 "public class TestTheories {",502 " @Theory public void testMyTheory() {}",503 "}")504 .doTest();505 }506 @Test507 public void annotationOnSuperMethod() {508 compilationHelper509 .addSourceLines(510 "TestSuper.java",511 "import org.junit.Test;",512 "public class TestSuper {",513 " @Test public void testToOverride() {}",514 "}")515 .addSourceLines(516 "TestSub.java",517 "import org.junit.runner.RunWith;",518 "import org.junit.runners.JUnit4;",519 "@RunWith(JUnit4.class)",520 "public class TestSub extends TestSuper {",521 " @Override public void testToOverride() {}",522 "}")523 .doTest();524 }525}...