[jvm] Move JUnit inspections into JUnit plugin

#IDEA-334017 Fixed

GitOrigin-RevId: 919fb236aeb44c192944eae2bae8da621e5a0e74
This commit is contained in:
Bart van Helvert
2023-12-11 23:00:28 +01:00
committed by intellij-monorepo-bot
parent f68d08af16
commit 58298dd53c
92 changed files with 865 additions and 845 deletions

View File

@@ -1,189 +0,0 @@
package com.intellij.codeInspection.tests.java.test
import com.intellij.jvm.analysis.internal.testFramework.test.HamcrestAssertionsConverterInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaHamcrestAssertionsConverterInspectionTest : HamcrestAssertionsConverterInspectionTestBase() {
fun `test highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import org.junit.Assert;
import java.util.Collection;
class Foo {
void m() {
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(2 != 3);
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(2 == 3);
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(2 > 3);
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(2 < 3);
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(2 >= 3);
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(2 <= 3);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(2 != 3);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(2 == 3);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(2 > 3);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(2 < 3);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(2 >= 3);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(2 <= 3);
}
void m2() {
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>("asd".equals("zxc"));
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>("asd" == "zxc");
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>("asd".contains("qwe"));
}
void m3(Collection<String> c, String o) {
Assert.<warning descr="Assert expression 'assertTrue' can be replaced with 'assertThat()' call">assertTrue</warning>(c.contains(o));
Assert.<warning descr="Assert expression 'assertEquals' can be replaced with 'assertThat()' call">assertEquals</warning>(c, o);
Assert.<warning descr="Assert expression 'assertEquals' can be replaced with 'assertThat()' call">assertEquals</warning>("msg", c, o);
Assert.<warning descr="Assert expression 'assertNotNull' can be replaced with 'assertThat()' call">assertNotNull</warning>(c);
Assert.<warning descr="Assert expression 'assertNull' can be replaced with 'assertThat()' call">assertNull</warning>(c);
Assert.<warning descr="Assert expression 'assertFalse' can be replaced with 'assertThat()' call">assertFalse</warning>(c.contains(o));
}
void m(int[] a, int[] b) {
Assert.<warning descr="Assert expression 'assertArrayEquals' can be replaced with 'assertThat()' call">assertArrayEquals</warning>(a, b);
}
}
""".trimIndent())
}
fun `test quickfix binary expression`() {
myFixture.testAllQuickfixes(JvmLanguage.JAVA, """
import org.junit.Assert;
class MigrationTest {
void migrate() {
Assert.assertTrue(2 != 3);
Assert.assertTrue(2 == 3);
Assert.assertTrue(2 > 3);
Assert.assertTrue(2 < 3);
Assert.assertTrue(2 >= 3);
Assert.assertTrue(2 <= 3);
Assert.assertFalse(2 != 3);
Assert.assertFalse(2 == 3);
Assert.assertFalse(2 > 3);
Assert.assertFalse(2 < 3);
Assert.assertFalse(2 >= 3);
Assert.assertFalse(2 <= 3);
}
}
""".trimIndent(), """
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
class MigrationTest {
void migrate() {
assertThat(2, not(is(3)));
assertThat(2, is(3));
assertThat(2, greaterThan(3));
assertThat(2, lessThan(3));
assertThat(2, greaterThanOrEqualTo(3));
assertThat(2, lessThanOrEqualTo(3));
assertThat(2, is(3));
assertThat(2, not(is(3)));
assertThat(2, lessThanOrEqualTo(3));
assertThat(2, greaterThanOrEqualTo(3));
assertThat(2, lessThan(3));
assertThat(2, greaterThan(3));
}
}
""".trimIndent(), "Replace with 'assertThat()'")
}
fun `test quickfix string`() {
myFixture.testAllQuickfixes(JvmLanguage.JAVA, """
import org.junit.Assert;
class Foo {
void migrate() {
Assert.assertTrue("asd".equals("zxc"));
Assert.assertTrue("asd" == "zxc");
Assert.assertTrue("asd".contains("qwe"));
}
}
""".trimIndent(), """
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
class Foo {
void migrate() {
assertThat("asd", is("zxc"));
assertThat("asd", sameInstance("zxc"));
assertThat("asd", containsString("qwe"));
}
}
""".trimIndent(), "Replace with 'assertThat()'")
}
fun `test quickfix collection`() {
myFixture.testAllQuickfixes(JvmLanguage.JAVA, """
import org.junit.Assert;
import java.util.Collection;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
class Foo {
void migrate(Collection<String> c, String o) {
Assert.assertTrue(c.contains(o));
Assert.assertEquals(c, o);
Assert.assertEquals("msg", c, o);
Assert.assertNotNull(c);
Assert.assertNull(c);
Assert.assertFalse(c.contains(o));
}
}
""".trimIndent(), """
import org.junit.Assert;
import java.util.Collection;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
class Foo {
void migrate(Collection<String> c, String o) {
assertThat(c, hasItem(o));
assertThat(o, is(c));
assertThat("msg", o, is(c));
assertThat(c, notNullValue());
assertThat(c, nullValue());
assertThat(c, not(hasItem(o)));
}
}
""".trimIndent(), "Replace with 'assertThat()'")
}
fun `test quickfix array`() {
myFixture.testAllQuickfixes(JvmLanguage.JAVA, """
import org.junit.Assert;
class Foo {
void migrate(int[] a, int[] b) {
Assert.assertArrayEquals(a, b);
}
}
""".trimIndent(), """
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
class Foo {
void migrate(int[] a, int[] b) {
assertThat(b, is(a));
}
}
""".trimIndent(), "Replace with 'assertThat()'")
}
}

View File

@@ -1,49 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnit3SuperTearDownInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnit3SuperTearDownInspectionTest : JUnit3SuperTearDownInspectionTestBase() {
fun `test teardown in finally no highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
class NoProblem extends junit.framework.TestCase {
public void tearDown() throws Exception {
super.tearDown();
}
}
class CalledInFinally extends junit.framework.TestCase {
public void tearDown() throws Exception {
try {
System.out.println();
} finally {
super.tearDown();
}
}
}
class SomeTest extends junit.framework.TestCase {
@Override
protected void setUp() throws Exception {
try {
super.setUp();
}
catch (Throwable t) {
super.tearDown();
}
}
public void test_something() {}
}
""".trimIndent())
}
fun `test teardown in finally highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
class SuperTearDownInFinally extends junit.framework.TestCase {
@Override
public void tearDown() throws Exception {
super.<warning descr="'tearDown()' is not called from 'finally' block">tearDown</warning>();
System.out.println();
}
}
""".trimIndent())
}
}

View File

@@ -1,289 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnit4ConverterInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
import com.intellij.refactoring.BaseRefactoringProcessor
class JavaJUnit4ConverterInspectionTest : JUnit4ConverterInspectionTestBase() {
fun `test highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import junit.framework.TestCase;
class <warning descr="'JUnit3Test' could be converted to JUnit4 test case">JUnit3Test</warning> extends TestCase {
public void testAddition() {
assertEquals(2, 1 + 1);
}
}""".trimIndent())
}
fun `test quickfix lifecycle method name conflict`() {
myFixture.addFileToProject("AbstractJUnit3Test.java", """
import junit.framework.TestCase;
public abstract class AbstractJUnit3Test extends TestCase {
@Override
public void setUp() throws Exception {
System.out.println("setup 2");
super.setUp();
}
@Override
public void tearDown() throws Exception {
try {
System.out.println("tearDown 2");
} finally {
super.tearDown();
}
}
}
""".trimIndent())
myFixture.configureByText("JUnit3Test.java", """
import junit.framework.TestCase;
class JUnit3<caret>Test extends AbstractJUnit3Test {
@Override
public void setUp() throws Exception {
System.out.println("setup 1");
super.setUp();
}
public void testAddition() {
assertEquals(2, 1 + 1);
}
@Override
public void tearDown() throws Exception {
try {
System.out.println("tearDown 1");
} finally {
super.tearDown();
}
}
}
""".trimIndent())
try {
myFixture.runQuickFix("Convert to JUnit 4 test case")
fail("Expected ConflictsInTestsException exception te be thrown.")
} catch(e: BaseRefactoringProcessor.ConflictsInTestsException) {
assertEquals(e.messages.size, 2)
assertContainsElements(
e.messages,
"Method setUp will have a name collision with its super method",
"Method tearDown will have a name collision with its super method"
)
}
}
fun `test quickfix semantic change`() {
myFixture.testQuickFixException<BaseRefactoringProcessor.ConflictsInTestsException>(JvmLanguage.JAVA, """
import junit.framework.TestCase;
class JUnit3<caret>Test extends TestCase {
public void testAddition() {
System.out.println(toString());
}
}
""".trimIndent(), "Convert to JUnit 4 test case") { e ->
assertEquals(e.messages.size, 1)
assertContainsElements(
e.messages,
"Method call toString() may change semantics when class JUnit3Test is converted to JUnit 4"
)
}
}
fun `test quickfix removed method`() {
myFixture.testQuickFixException<BaseRefactoringProcessor.ConflictsInTestsException>(JvmLanguage.JAVA, """
import junit.framework.TestCase;
class JUnit3<caret>Test extends TestCase {
public void testAddition() {
System.out.println(countTestCases());
}
}
""".trimIndent(), "Convert to JUnit 4 test case") { e ->
assertEquals(e.messages.size, 1)
assertContainsElements(
e.messages,
"Method call countTestCases() will not compile when class JUnit3Test is converted to JUnit 4"
)
}
}
fun `test quickfix non convertable suite`() {
myFixture.testQuickFixException<BaseRefactoringProcessor.ConflictsInTestsException>(JvmLanguage.JAVA, """
import junit.framework.TestCase;
import junit.framework.Test;
class JUnit3<caret>Test extends TestCase {
public static Test suite() {
System.out.println("Creating test suite");
TestSuite suite = new TestSuite();
suite.addTestSuite(Foo.class);
suite.addTestSuite(Bar.class);
return suite;
}
}
""".trimIndent(), "Convert to JUnit 4 test case") { e ->
assertEquals(e.messages.size, 1)
assertContainsElements(
e.messages,
"Migration of suite method for class JUnit3Test has side effects which will be deleted"
)
}
}
fun `test quickfix class expression suite converter`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
class Foo extends TestCase { }
class Bar extends TestCase { }
class JUnit3<caret>Test extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(Foo.class);
suite.addTestSuite(Bar.class);
return suite;
}
}
""".trimIndent(), """
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
class Foo extends TestCase { }
class Bar extends TestCase { }
@RunWith(Suite.class)
@Suite.SuiteClasses({Foo.class, Bar.class})
class JUnit3Test {
}
""".trimIndent(), "Convert to JUnit 4 test case", testPreview = true)
}
fun `test quickfix nested suite converter`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
class Foo extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(Bar.class);
return suite;
}
}
class Bar extends TestCase { }
class JUnit3<caret>Test extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(Foo.suite());
return suite;
}
}
""".trimIndent(), """
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
class Foo extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(Bar.class);
return suite;
}
}
class Bar extends TestCase { }
@RunWith(Suite.class)
@Suite.SuiteClasses({Foo.class})
class JUnit3Test {
}
""".trimIndent(), "Convert to JUnit 4 test case", testPreview = true)
}
fun `test quickfix assertion converter`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import junit.framework.TestCase;
class JUnit3<caret>Test extends TestCase {
public void testAddition() {
assertEquals(2, 1 + 1);
}
}
""".trimIndent(), """
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
class JUnit3Test {
@Test
public void testAddition() {
Assert.assertEquals(2, 1 + 1);
}
}
""".trimIndent(), "Convert to JUnit 4 test case", testPreview = true)
}
fun `test quickfix setup and teardown converter`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import junit.framework.TestCase;
class JUnit3<caret>Test extends TestCase {
@Override
public void setUp() {
System.out.println("setup");
super.setUp();
}
public void testAddition() {
assertEquals(2, 1 + 1);
}
@Override
void tearDown() {
try {
System.out.println("tearDown");
} finally {
super.tearDown();
}
}
}
""".trimIndent(), """
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
class JUnit3Test {
@Before
public void setUp() {
System.out.println("setup");
}
@Test
public void testAddition() {
Assert.assertEquals(2, 1 + 1);
}
@After
public void tearDown() {
System.out.println("tearDown");
}
}
""".trimIndent(), "Convert to JUnit 4 test case", testPreview = true)
}
}

View File

@@ -1,218 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnit5AssertionsConverterInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnit5AssertionsConverterInspectionTest : JUnit5AssertionsConverterInspectionTestBase() {
fun `test AssertArrayEquals`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
assert<caret>ArrayEquals(new Object[] {}, null);
}
}
""".trimIndent(), """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
Assertions.assertArrayEquals(new Object[] {}, null);
}
}
""".trimIndent(), "Replace with 'org.junit.jupiter.api.Assertions' method call")
}
fun `test AssertArrayEquals message`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
assert<caret>ArrayEquals("message", new Object[] {}, null);
}
}
""".trimIndent(), """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
Assertions.assertArrayEquals(new Object[] {}, null, "message");
}
}
""".trimIndent(), "Replace with 'org.junit.jupiter.api.Assertions' method call")
}
fun `test AssertEquals`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
assert<caret>Equals("message", "Expected", "actual");
}
}
""".trimIndent(), """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
Assertions.assertEquals("Expected", "actual", "message");
}
}
""".trimIndent(), "Replace with 'org.junit.jupiter.api.Assertions' method call")
}
fun `test AssertNotEqualsWithDelta`() {
myFixture.testQuickFixUnavailable(JvmLanguage.JAVA, """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
assert<caret>NotEquals(1, 1, 1);
}
}
""".trimIndent())
}
fun `test AssertThat`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import static org.junit.Assert.*;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst(Matcher matcher) throws Exception {
assert<caret>That("reason", "null", matcher);
}
}
""".trimIndent(), """
import static org.junit.Assert.*;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst(Matcher matcher) throws Exception {
MatcherAssert.assertThat("reason", "null", matcher);
}
}
""".trimIndent(), "Replace with 'org.hamcrest.MatcherAssert' method call")
}
fun `test AssertTrue`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
assert<caret>True("message", false);
}
}
""".trimIndent(), """
import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
Assertions.assertTrue(false, "message");
}
}
""".trimIndent(), "Replace with 'org.junit.jupiter.api.Assertions' method call")
}
fun `test AssertTrue method reference`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
class Test1 {
@Test
public void testFirst() throws Exception {
List<Boolean> booleanList = new ArrayList<>();
booleanList.forEach(Assert::assert<caret>True);
}
}
""".trimIndent(), """
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
class Test1 {
@Test
public void testFirst() throws Exception {
List<Boolean> booleanList = new ArrayList<>();
booleanList.forEach(Assertions::assertTrue);
}
}
""".trimIndent(), "Replace with 'org.junit.jupiter.api.Assertions' method call")
}
fun `test AssumeTrue`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import static org.junit.Assume.*;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
assume<caret>True("message", false);
}
}
""".trimIndent(), """
import static org.junit.Assume.*;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
class Test1 {
@Test
public void testFirst() throws Exception {
Assumptions.assumeTrue(false, "message");
}
}
""".trimIndent(), "Replace with 'org.junit.jupiter.api.Assumptions' method call")
}
}

View File

@@ -1,155 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnit5ConverterInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnit5ConverterInspectionTest : JUnit5ConverterInspectionTestBase() {
fun `test qualified conversion`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import org.junit.Test;
import org.junit.Before;
import org.junit.Assert;
import java.util.*;
public class Qual<caret>ified {
@Before
public void setUp() {}
@Test
public void testMethodCall() throws Exception {
Assert.assertArrayEquals(new Object[] {}, null);
Assert.assertArrayEquals("message", new Object[] {}, null);
Assert.assertEquals("Expected", "actual");
Assert.assertEquals("message", "Expected", "actual");
Assert.fail();
Assert.fail("");
}
@Test
public void testMethodRef() {
List<Boolean> booleanList = new ArrayList<>();
booleanList.add(true);
booleanList.forEach(Assert::assertTrue);
}
}
""".trimIndent(), """
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import java.util.*;
class Qualified {
@BeforeEach
public void setUp() {}
@Test
void testMethodCall() throws Exception {
Assertions.assertArrayEquals(new Object[] {}, null);
Assertions.assertArrayEquals(new Object[] {}, null, "message");
Assertions.assertEquals("Expected", "actual");
Assertions.assertEquals("Expected", "actual", "message");
Assertions.fail();
Assertions.fail("");
}
@Test
void testMethodRef() {
List<Boolean> booleanList = new ArrayList<>();
booleanList.add(true);
booleanList.forEach(Assertions::assertTrue);
}
}
""".trimIndent(), "Migrate to JUnit 5")
}
fun `test unqualified conversion`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Assert;
import java.util.*;
public class UnQual<caret>ified {
@Test
public void testMethodCall() throws Exception {
assertArrayEquals(new Object[] {}, null);
assertArrayEquals("message", new Object[] {}, null);
assertEquals("Expected", "actual");
assertEquals("message", "Expected", "actual");
fail();
fail("");
}
@Test
public void testMethodRef() {
List<Boolean> booleanList = new ArrayList<>();
booleanList.add(true);
booleanList.forEach(Assert::assertTrue);
}
}
""".trimIndent(), """
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.*;
class UnQualified {
@Test
void testMethodCall() throws Exception {
Assertions.assertArrayEquals(new Object[] {}, null);
Assertions.assertArrayEquals(new Object[] {}, null, "message");
Assertions.assertEquals("Expected", "actual");
Assertions.assertEquals("Expected", "actual", "message");
Assertions.fail();
Assertions.fail("");
}
@Test
void testMethodRef() {
List<Boolean> booleanList = new ArrayList<>();
booleanList.add(true);
booleanList.forEach(Assertions::assertTrue);
}
}
""".trimIndent(), "Migrate to JUnit 5")
}
fun `test remove public modifier`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import org.junit.Test;
public class Presen<caret>ter {
@Test
public void testJUnit4() {}
@org.junit.jupiter.api.Test
public void testJUnit5() {}
}
""".trimIndent(), """
import org.junit.jupiter.api.Test;
class Presenter {
@Test
void testJUnit4() {}
@org.junit.jupiter.api.Test
void testJUnit5() {}
}
""".trimIndent(), "Migrate to JUnit 5")
}
fun `test expected on test annotation`() {
myFixture.testQuickFixUnavailable(JvmLanguage.JAVA, """
import org.junit.Test;
import static org.junit.Assert.*;
public class Simp<caret>le {
@Test(expected = Exception.class)
public void testFirst() throws Exception { }
}
""".trimIndent())
}
}

View File

@@ -1,63 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnitAssertEqualsMayBeAssertSameInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnitAssertEqualsMayBeAssertSameInspectionTest : JUnitAssertEqualsMayBeAssertSameInspectionTestBase() {
fun `test JUnit 3 highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
class Test extends junit.framework.TestCase {
public void testOne() {
<warning descr="'assertEquals()' may be 'assertSame()'">assertEquals</warning>(A.a, A.b);
}
}
""".trimIndent())
}
fun `test JUnit 3 quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
class Test extends junit.framework.TestCase {
public void testOne() {
asser<caret>tEquals(A.a, A.b);
}
}
""".trimIndent(), """
class Test extends junit.framework.TestCase {
public void testOne() {
assertSame(A.a, A.b);
}
}
""".trimIndent(), "Replace with 'assertSame()'")
}
fun `test JUnit 4 highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
class Test {
@org.junit.Test
public void test() {
org.junit.Assert.<warning descr="'assertEquals()' may be 'assertSame()'">assertEquals</warning>(A.a, A.b);
}
}
""".trimIndent())
}
fun `test JUnit 4 quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
class Test {
@org.junit.Test
public void test() {
org.junit.Assert.assert<caret>Equals(A.a, A.b);
}
}
""".trimIndent(), """
import org.junit.Assert;
class Test {
@org.junit.Test
public void test() {
Assert.assertSame(A.a, A.b);
}
}
""".trimIndent(), "Replace with 'assertSame()'")
}
}

View File

@@ -1,44 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnitAssertEqualsOnArrayInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnitAssertEqualsOnArrayInspectionTest : JUnitAssertEqualsOnArrayInspectionTestBase() {
fun `test highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import org.junit.jupiter.api.Assertions;
class MyTest {
void myTest() {
Object[] a = {};
Object[] e = {""};
Assertions.<warning descr="'assertEquals()' called on array">assertEquals</warning>(a, e, "message");
}
}
""".trimIndent())
}
fun `test quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
import org.junit.jupiter.api.Assertions;
class MyTest {
void myTest() {
Object[] a = {};
Object[] e = {""};
Assertions.assert<caret>Equals(a, e, "message");
}
}
""".trimIndent(), """
import org.junit.jupiter.api.Assertions;
class MyTest {
void myTest() {
Object[] a = {};
Object[] e = {""};
Assertions.assertArrayEquals(a, e, "message");
}
}
""".trimIndent(), "Replace with 'assertArrayEquals()'")
}
}

View File

@@ -1,43 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnitIgnoredTestInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnitIgnoredTestInspectionTest : JUnitIgnoredTestInspectionTestBase() {
fun `test JUnit 4 @Ignore`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import org.junit.*;
@Ignore("for good reason")
class IgnoredJUnitTest {
@Ignore
@Test
public void <warning descr="Test method 'foo1()' is ignored/disabled without reason">foo1</warning>() { }
@Ignore("valid description")
@Test
public void foo2() { }
}
""".trimIndent())
}
fun `test JUnit 5 @Disabled`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.Ignore;
@Disabled
class <warning descr="Test class 'DisabledJUnit5Test' is ignored/disabled without reason">DisabledJUnit5Test</warning> {
@Disabled
@Test
void <warning descr="Test method 'foo1()' is ignored/disabled without reason">foo1</warning>() { }
@Disabled
@Ignore("valid reason")
@Test
void foo2() { }
}
""".trimIndent())
}
}

View File

@@ -1,183 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnitMixedFrameworkInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class JavaJUnitMixedFrameworkInspectionTest : JUnitMixedFrameworkInspectionTestBase() {
fun `test no highlighting`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class JUnit3Test extends junit.framework.TestCase {
public void testFoo() { }
}
""".trimIndent(), fileName = "JUnit3Test")
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class JUnit4Test {
@org.junit.Test
public void testFoo() { }
}
""".trimIndent(), fileName = "JUnit4Test")
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class JUnit5Test {
@org.junit.jupiter.api.Test
public void testFoo() { }
}
""".trimIndent(), fileName = "JUnit5Test")
myFixture.addFileToProject("JUnit5TestCase.java", """
public class JUnit5TestCase {
@org.junit.jupiter.api.BeforeAll
public void beforeAll() { }
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class JUnit5Test extends JUnit5TestCase {
@org.junit.jupiter.api.Test
public void testFoo() { }
}
""".trimIndent(), fileName = "JUnit5Test")
}
fun `test highlighting junit 3 test case with junit 4 test`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class MyTest extends junit.framework.TestCase {
@org.junit.Test
public void <warning descr="Method 'testFoo()' annotated with '@Test' inside class extending JUnit 3 TestCase">testFoo</warning>() { }
}
""".trimIndent(), fileName = "MyTest")
}
fun `test highlighting junit 3 test case with junit 5 test`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class MyTest extends junit.framework.TestCase {
@org.junit.jupiter.api.Test
public void <warning descr="Method 'testFoo()' annotated with '@Test' inside class extending JUnit 3 TestCase">testFoo</warning>() { }
}
""".trimIndent(), fileName = "MyTest")
}
fun `test junit 3 test case with junit 4 test remove before each annotation quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
public class MyTest extends junit.framework.TestCase {
@org.junit.jupiter.api.BeforeEach
public void do<caret>Something() { }
}
""".trimIndent(), """
public class MyTest extends junit.framework.TestCase {
public void doSomething() { }
}
""".trimIndent(), fileName = "MyTest", hint = "Remove '@BeforeEach' annotation", testPreview = true)
}
fun `test junit 3 test case with junit 4 test remove test annotation without rename quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
public class MyTest extends junit.framework.TestCase {
@org.junit.Test
public void test<caret>Foo() { }
}
""".trimIndent(), """
public class MyTest extends junit.framework.TestCase {
public void testFoo() { }
}
""".trimIndent(), fileName = "MyTest", hint = "Remove '@Test' annotation", testPreview = true)
}
fun `test junit 3 test case with junit 4 test remove test annotation and rename quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
public class MyTest extends junit.framework.TestCase {
@org.junit.Test
public void f<caret>oo() { }
}
""".trimIndent(), """
public class MyTest extends junit.framework.TestCase {
public void testFoo() { }
}
""".trimIndent(), fileName = "MyTest", hint = "Remove '@Test' annotation", testPreview = true)
}
fun `test junit 3 test case with junit 4 test remove ignore annotation and rename quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
public class MyTest extends junit.framework.TestCase {
@org.junit.Ignore
public void test<caret>Foo() { }
}
""".trimIndent(), """
public class MyTest extends junit.framework.TestCase {
public void _testFoo() { }
}
""".trimIndent(), fileName = "MyTest", hint = "Remove '@Ignore' annotation", testPreview = true)
}
fun `test highlighting junit 4 test case with junit 5 test`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class MyTest {
@org.junit.jupiter.api.Test
public void <warning descr="Method 'testFoo()' annotated with '@Test' inside class extending JUnit 4 TestCase">testFoo</warning>() { }
@org.junit.Test
public void testBar() { }
@org.junit.Test
public void testFooBar() { }
}
""".trimIndent(), fileName = "MyTest")
}
fun `test highlighting junit 5 test case with junit 4 test`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class MyTest {
@org.junit.Test
public void <warning descr="Method 'testFoo()' annotated with '@Test' inside class extending JUnit 5 TestCase">testFoo</warning>() { }
@org.junit.jupiter.api.Test
public void testBar() { }
@org.junit.jupiter.api.Test
public void testFooBar() { }
}
""".trimIndent(), fileName = "MyTest")
}
fun `test junit 5 test case with junit 4 quickfix`() {
myFixture.testQuickFix(JvmLanguage.JAVA, """
public class MyTest {
@org.junit.Test
public void test<caret>Foo() { }
@org.junit.jupiter.api.Test
public void testBar() { }
@org.junit.jupiter.api.Test
public void testFooBar() { }
}
""".trimIndent(), """
import org.junit.jupiter.api.Test;
class MyTest {
@Test
void testFoo() { }
@org.junit.jupiter.api.Test
void testBar() { }
@org.junit.jupiter.api.Test
void testFooBar() { }
}
""".trimIndent(), fileName = "MyTest", hint = "Migrate to JUnit 5")
}
fun `test highlighting junit 5 test case with junit 4 super class`() {
myFixture.addFileToProject("JUnit4TestCase.java", """
public class JUnit4TestCase {
@org.junit.BeforeClass
public void beforeClass() { }
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
public class MyTest extends JUnit4TestCase {
@org.junit.jupiter.api.Test
public void <warning descr="Method 'testFoo()' annotated with '@Test' inside class extending JUnit 4 TestCase">testFoo</warning>() { }
}
""".trimIndent(), fileName = "MyTest")
}
}

View File

@@ -1,61 +0,0 @@
package com.intellij.codeInspection.tests.kotlin.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.JUnitParameterizedSourceGoToRelatedTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ContentEntry
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.pom.java.LanguageLevel
import com.intellij.psi.PsiMethod
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.PsiTestUtil
import com.intellij.util.PathUtil
import java.io.File
class JavaJUnitParameterizedSourceGoToRelatedTest : JUnitParameterizedSourceGoToRelatedTestBase() {
override fun getProjectDescriptor(): LightProjectDescriptor = object : JUnitProjectDescriptor(LanguageLevel.HIGHEST) {
override fun configureModule(module: Module, model: ModifiableRootModel, contentEntry: ContentEntry) {
super.configureModule(module, model, contentEntry)
val jar = File(PathUtil.getJarPathForClass(JvmStatic::class.java))
PsiTestUtil.addLibrary(model, "kotlin-stdlib", jar.parent, jar.name)
}
}
fun `test go to method source with explicit name`() {
myFixture.testGoToRelatedAction(JvmLanguage.JAVA, """
class Test {
@org.junit.jupiter.params.ParameterizedTest
@org.junit.jupiter.params.provider.MethodSource("foo")
public void a<caret>bc(Integer i) { }
public static List<Integer> foo() {
return new ArrayList<String>();
}
}
""".trimIndent()) { item ->
val element = item.element as? PsiMethod
assertNotNull(element)
assertEquals("foo", element?.name)
assertEquals(0, element?.parameters?.size)
}
}
fun `test go to method source without explicit name`() {
myFixture.testGoToRelatedAction(JvmLanguage.JAVA, """
class Test {
@org.junit.jupiter.params.ParameterizedTest
@org.junit.jupiter.params.provider.MethodSource
public void a<caret>bc(Integer i) { }
public static List<Integer> abc() {
return new ArrayList<String>();
}
}
""".trimIndent()) { item ->
val element = item.element as? PsiMethod
assertNotNull(element)
assertEquals("abc", element?.name)
assertEquals(0, element?.parameters?.size)
}
}
}

View File

@@ -1,106 +0,0 @@
package com.intellij.codeInspection.tests.java.test.junit
import com.intellij.jvm.analysis.internal.testFramework.test.junit.TestCaseWithMultipleRunnersInspectionTestBase
import com.intellij.jvm.analysis.testFramework.JvmLanguage
class TestCaseWithMultipleRunnersInspectionJavaTest : TestCaseWithMultipleRunnersInspectionTestBase() {
fun `test parent annotation`() {
myFixture.addClass("""
@org.junit.runner.RunWith(org.junit.runners.Suite.class)
@org.junit.runners.Suite.SuiteClasses(Object.class)
class ParentTestBaseSuite {
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
@org.junit.runner.<warning descr="@RunWith annotation already exists in ParentTestBaseSuite class">RunWith</warning>(org.junit.runners.Parameterized.class)
class MyTest extends ParentTestBaseSuite {
@org.junit.Test
public void test() {
}
}
""".trimIndent())
}
fun `test interface annotation`() {
myFixture.addClass("""
@org.junit.runner.RunWith(org.junit.runners.Suite.class)
@org.junit.runners.Suite.SuiteClasses(Object.class)
interface ParentTestBaseSuite {
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
@org.junit.runner.<warning descr="@RunWith annotation already exists in ParentTestBaseSuite class">RunWith</warning>(org.junit.runners.Parameterized.class)
class MyTest implements ParentTestBaseSuite {
@org.junit.Test
public void test() {
}
}
""".trimIndent())
}
fun `test inherited annotation1`() {
myFixture.addClass("""
@org.junit.runner.RunWith(org.junit.runners.Suite.class)
@org.junit.runners.Suite.SuiteClasses(Object.class)
interface SecondParentSuite {
}
""".trimIndent())
myFixture.addClass("""
interface FirstParent extends SecondParentSuite {
}
""".trimIndent())
myFixture.addClass("""
interface DummyInterface {
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
@org.junit.runner.<warning descr="@RunWith annotation already exists in SecondParentSuite class">RunWith</warning>(org.junit.runners.Parameterized.class)
class MyTest implements DummyInterface, FirstParent {
@org.junit.Test
public void test() {
}
}
""".trimIndent())
}
fun `test inherited annotation2`() {
myFixture.addClass("""
@org.junit.runner.RunWith(org.junit.runners.Suite.class)
@org.junit.runners.Suite.SuiteClasses(Object.class)
class SecondParentSuite {
}
""".trimIndent())
myFixture.addClass("""
class FirstParent extends SecondParentSuite {
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
@org.junit.runner.<warning descr="@RunWith annotation already exists in SecondParentSuite class">RunWith</warning>(org.junit.runners.Parameterized.class)
class MyTest extends FirstParent {
@org.junit.Test
public void test() {
}
}
""".trimIndent())
}
fun `test not inherited annotation`() {
myFixture.addClass("""
class ParentTestBase {
}
""".trimIndent())
myFixture.testHighlighting(JvmLanguage.JAVA, """
@org.junit.runner.RunWith(org.junit.runners.Parameterized.class)
class MyTest extends ParentTestBase {
@org.junit.Test
public void test() {
}
}
""".trimIndent())
}
}