Skip to content

Commit 5459e88

Browse files
authored
Add test for Kotlin (#876)
* Add test for Kotlin * Add test for Kotlin
1 parent 20cca13 commit 5459e88

4 files changed

Lines changed: 164 additions & 1 deletion

File tree

src/main/java/org/openrewrite/java/testing/junit5/AddMissingNested.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openrewrite.java.search.UsesType;
2929
import org.openrewrite.java.tree.J;
3030
import org.openrewrite.java.tree.TypeUtils;
31+
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
3132

3233
import java.time.Duration;
3334
import java.util.Arrays;
@@ -74,7 +75,7 @@ public Set<String> getTags() {
7475

7576
@Override
7677
public TreeVisitor<?, ExecutionContext> getVisitor() {
77-
return Preconditions.check(PRECONDITION, new JavaIsoVisitor<ExecutionContext>() {
78+
return Preconditions.check(Preconditions.and(PRECONDITION, Preconditions.not(new KotlinFileChecker<>())), new JavaIsoVisitor<ExecutionContext>() {
7879
@Override
7980
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
8081
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);

src/test/java/org/openrewrite/java/testing/junit5/AddMissingNestedTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
*/
1616
package org.openrewrite.java.testing.junit5;
1717

18+
import org.junit.jupiter.api.Disabled;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.DocumentExample;
2021
import org.openrewrite.InMemoryExecutionContext;
2122
import org.openrewrite.Issue;
2223
import org.openrewrite.java.JavaParser;
24+
import org.openrewrite.kotlin.KotlinParser;
2325
import org.openrewrite.test.RecipeSpec;
2426
import org.openrewrite.test.RewriteTest;
2527

2628
import static org.openrewrite.java.Assertions.java;
29+
import static org.openrewrite.kotlin.Assertions.kotlin;
2730

2831
@SuppressWarnings("JUnit3StyleTestMethodInJUnit4Class")
2932
class AddMissingNestedTest implements RewriteTest {
@@ -33,6 +36,8 @@ public void defaults(RecipeSpec spec) {
3336
spec.recipe(new AddMissingNested())
3437
.parser(JavaParser.fromJavaVersion()
3538
.logCompilationWarningsAndErrors(true)
39+
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5"))
40+
.parser(KotlinParser.builder()
3641
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5"));
3742
}
3843

@@ -251,4 +256,46 @@ public void test() {
251256
)
252257
);
253258
}
259+
260+
@Disabled("Should be enabled when Preconditions.not(new KotlinFileChecker<>()) is removed from the recipe")
261+
@Test
262+
void doesNotAnnotateKotlinObject() {
263+
//language=kotlin
264+
rewriteRun(
265+
kotlin(
266+
"""
267+
import org.junit.jupiter.api.Test
268+
269+
class RootTest {
270+
object InnerObject {
271+
@Test
272+
fun test() {
273+
}
274+
}
275+
}
276+
"""
277+
)
278+
);
279+
}
280+
281+
@Disabled("Should be enabled when Preconditions.not(new KotlinFileChecker<>()) is removed from the recipe")
282+
@Test
283+
void doesNotAnnotateKotlinCompanionObject() {
284+
//language=kotlin
285+
rewriteRun(
286+
kotlin(
287+
"""
288+
import org.junit.jupiter.api.Test
289+
290+
class RootTest {
291+
companion object {
292+
@Test
293+
fun test() {
294+
}
295+
}
296+
}
297+
"""
298+
)
299+
);
300+
}
254301
}

src/test/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStaticTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import org.openrewrite.DocumentExample;
2121
import org.openrewrite.InMemoryExecutionContext;
2222
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.kotlin.KotlinParser;
2324
import org.openrewrite.test.RecipeSpec;
2425
import org.openrewrite.test.RewriteTest;
2526
import org.openrewrite.test.TypeValidation;
2627

2728
import static org.openrewrite.java.Assertions.java;
29+
import static org.openrewrite.kotlin.Assertions.kotlin;
2830

2931
class MockitoWhenOnStaticToMockStaticTest implements RewriteTest {
3032

@@ -62,6 +64,11 @@ public String getStringNonStatic() {
6264
}
6365
"""
6466
))
67+
.parser(KotlinParser.builder()
68+
.classpathFromResources(new InMemoryExecutionContext(),
69+
"junit-jupiter-api-5",
70+
"mockito-core-5"
71+
))
6572
// Known limitation with: /*~~(Identifier type is missing or malformed)~~>*/A
6673
.afterTypeValidationOptions(TypeValidation.builder().identifiers(false).build())
6774
;
@@ -1789,4 +1796,55 @@ public void setUp() {
17891796
)
17901797
);
17911798
}
1799+
1800+
@Test
1801+
void shouldNotModifyKotlinFilesUsingMockitoWhen() {
1802+
rewriteRun(
1803+
spec -> spec.typeValidationOptions(TypeValidation.all().methodInvocations(false)),
1804+
//language=kotlin
1805+
kotlin(
1806+
"""
1807+
import org.junit.jupiter.api.Test
1808+
import org.mockito.Mockito.`when`
1809+
import org.mockito.Mockito.mock
1810+
1811+
class MyTest {
1812+
@Test
1813+
fun testSomething() {
1814+
val mockList = mock(MutableList::class.java)
1815+
`when`(mockList.size).thenReturn(100)
1816+
}
1817+
}
1818+
"""
1819+
)
1820+
);
1821+
}
1822+
1823+
@Test
1824+
void shouldRefactorKotlinMockitoWhenOnStaticMethod() {
1825+
rewriteRun(
1826+
spec -> spec
1827+
.typeValidationOptions(TypeValidation.all().methodInvocations(false)),
1828+
//language=kotlin
1829+
kotlin(
1830+
"""
1831+
import org.junit.jupiter.api.Test
1832+
import org.mockito.MockedStatic
1833+
import org.mockito.Mockito.mock
1834+
import org.mockito.Mockito.mockStatic
1835+
import java.util.Calendar
1836+
1837+
class MyTest {
1838+
@Test
1839+
fun testStaticMethod() {
1840+
val calendarMock: Calendar = mock(Calendar::class.java)
1841+
mockStatic(Calendar::class.java).use { mockA1 ->
1842+
mockA1.`when`<Calendar>(Calendar::getInstance).thenReturn(calendarMock)
1843+
}
1844+
}
1845+
}
1846+
"""
1847+
)
1848+
);
1849+
}
17921850
}

src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import org.openrewrite.InMemoryExecutionContext;
2121
import org.openrewrite.Issue;
2222
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.kotlin.KotlinParser;
2324
import org.openrewrite.test.RecipeSpec;
2425
import org.openrewrite.test.RewriteTest;
2526
import org.openrewrite.test.TypeValidation;
2627

2728
import static org.openrewrite.groovy.Assertions.groovy;
2829
import static org.openrewrite.java.Assertions.java;
30+
import static org.openrewrite.kotlin.Assertions.kotlin;
2931

3032
class PowerMockitoMockStaticToMockitoTest implements RewriteTest {
3133
@Override
@@ -42,6 +44,11 @@ public void defaults(RecipeSpec spec) {
4244
"powermock-core-1",
4345
"testng-7"
4446
))
47+
.parser(KotlinParser.builder()
48+
.classpathFromResources(new InMemoryExecutionContext(),
49+
"junit-jupiter-api-5",
50+
"mockito-core-5"
51+
))
4552
.recipe(new PowerMockitoMockStaticToMockito())
4653
.typeValidationOptions(TypeValidation.builder().cursorAcyclic(false).build());
4754
}
@@ -712,4 +719,54 @@ void doesNotExplodeOnTopLevelMethodDeclaration() {
712719
)
713720
);
714721
}
722+
723+
@Test
724+
void shouldNotModifyKotlinFilesUsingMockitoStatic() {
725+
rewriteRun(
726+
spec -> spec.typeValidationOptions(TypeValidation.all().methodInvocations(false)),
727+
//language=kotlin
728+
kotlin(
729+
"""
730+
import org.junit.jupiter.api.Test
731+
import org.mockito.Mockito.mock
732+
import org.mockito.Mockito.`when`
733+
734+
class MyTest {
735+
@Test
736+
fun testSomething() {
737+
val mockList = mock(MutableList::class.java)
738+
`when`(mockList.size).thenReturn(100)
739+
}
740+
}
741+
"""
742+
)
743+
);
744+
}
745+
746+
@Test
747+
void shouldRefactorKotlinPowerMockitoToMockito() {
748+
rewriteRun(
749+
spec -> spec.typeValidationOptions(TypeValidation.all().methodInvocations(false)),
750+
//language=kotlin
751+
kotlin(
752+
"""
753+
import org.junit.jupiter.api.Test
754+
import org.mockito.MockedStatic
755+
import org.mockito.Mockito.mock
756+
import org.mockito.Mockito.mockStatic
757+
import java.util.Calendar
758+
759+
class MyTest {
760+
@Test
761+
fun testStaticMethod() {
762+
val calendarMock: Calendar = mock(Calendar::class.java)
763+
mockStatic(Calendar::class.java).use { mockedCalendar ->
764+
mockedCalendar.`when`<Calendar>(Calendar::getInstance).thenReturn(calendarMock)
765+
}
766+
}
767+
}
768+
"""
769+
)
770+
);
771+
}
715772
}

0 commit comments

Comments
 (0)