Skip to content

Commit 2392cc5

Browse files
committed
Add missing @AUTOCONFIGURE annotations, new testing
1 parent 5abf325 commit 2392cc5

File tree

4 files changed

+238
-2
lines changed

4 files changed

+238
-2
lines changed

src/before/java/org/openrewrite/java/testing/junit5/ExampleJunitTestClass.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.BeforeClass;
2222
import org.junit.Rule;
2323
import org.junit.Test;
24+
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
2426
import org.junit.rules.TemporaryFolder;
2527
import org.junit.rules.Timeout;
2628
import org.mockito.Mock;
@@ -81,6 +83,15 @@ public void foo2() {
8183
int arr = new int[]{}[0];
8284
}
8385

86+
@Rule
87+
public ExpectedException throwz = ExpectedException.none();
88+
89+
@Test
90+
public void foo3() {
91+
throwz.expect(RuntimeException.class);
92+
throw new RuntimeException();
93+
}
94+
8495
@Test
8596
public void assertsStuff() {
8697
Assert.assertEquals("One is one", 1, 1);

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

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

18+
import org.openrewrite.AutoConfigure;
1819
import org.openrewrite.java.AddImport;
1920
import org.openrewrite.java.AutoFormat;
2021
import org.openrewrite.java.JavaIsoRefactorVisitor;
@@ -36,6 +37,7 @@
3637
* Migrating the other methods of ExpectedException is not yet implemented.
3738
*
3839
*/
40+
@AutoConfigure
3941
public class ExpectedExceptionToAssertThrows extends JavaIsoRefactorVisitor {
4042

4143
private static final String ExpectedExceptionFqn = "org.junit.rules.ExpectedException";

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

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

18+
import org.openrewrite.AutoConfigure;
1819
import org.openrewrite.Formatting;
1920
import org.openrewrite.java.JavaIsoRefactorVisitor;
2021
import org.openrewrite.java.search.SemanticallyEqual;
@@ -32,6 +33,7 @@
3233
* JUnit4 Spring test classes are annotated with @RunWith(SpringRunner.class)
3334
* Turn this into the JUnit5-compatible @ExtendsWith(SpringExtension.class)
3435
*/
36+
@AutoConfigure
3537
public class SpringRunnerToSpringExtension extends JavaIsoRefactorVisitor {
3638
private static final JavaType.Class runWithType = JavaType.Class.build("org.junit.runner.RunWith");
3739
private static final J.Ident runWithIdent = J.Ident.build(

src/test/kotlin/org/openrewrite/java/testing/mockito/JunitMockitoUpgradeIntegrationTest.kt

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import org.openrewrite.java.tree.J
2626
*/
2727
class JunitMockitoUpgradeIntegrationTest : RefactorVisitorTestForParser<J.CompilationUnit> {
2828
override val parser: JavaParser = JavaParser.fromJavaVersion()
29-
.classpath("mockito-all", "junit")
29+
.classpath("mockito-all", "junit", "hamcrest")
3030
.build()
31-
override val visitors: Iterable<RefactorVisitor<*>> = loadVisitorsForTest("org.openrewrite.java.testing.JUnit5Migration", "org.openrewrite.java.testing.Mockito1to3Migration")
31+
override val visitors: Iterable<RefactorVisitor<*>> = loadVisitorsForTest(
32+
"org.openrewrite.java.testing.JUnit5Migration",
33+
"org.openrewrite.java.testing.Mockito1to3Migration")
3234

3335
/**
3436
* Replace org.mockito.MockitoAnnotations.Mock with org.mockito.Mock
@@ -233,4 +235,223 @@ class JunitMockitoUpgradeIntegrationTest : RefactorVisitorTestForParser<J.Compil
233235
public class ExampleTest { }
234236
"""
235237
)
238+
239+
@Test
240+
fun theBigOne() = assertRefactored(
241+
before = """
242+
package org.openrewrite.java.testing.junit5;
243+
244+
import org.junit.AfterClass;
245+
import org.junit.Assert;
246+
import org.junit.Before;
247+
import org.junit.BeforeClass;
248+
import org.junit.Rule;
249+
import org.junit.Test;
250+
import org.junit.Test;
251+
import org.junit.rules.ExpectedException;
252+
import org.junit.rules.TemporaryFolder;
253+
import org.junit.rules.Timeout;
254+
import org.mockito.Mock;
255+
import org.mockito.MockitoAnnotations;
256+
257+
import java.io.File;
258+
import java.io.IOException;
259+
import java.util.List;
260+
261+
import static org.mockito.Mockito.*;
262+
263+
public class ExampleJunitTestClass {
264+
265+
// @Rule
266+
// public Timeout globalTimeout = new Timeout(500);
267+
268+
@Rule
269+
public TemporaryFolder folder = new TemporaryFolder();
270+
271+
@Before
272+
public void beforeClass() {
273+
MockitoAnnotations.initMocks(this);
274+
}
275+
276+
@AfterClass
277+
public static void afterClass() { }
278+
279+
@Mock
280+
List<String> mockedList;
281+
282+
public void initMocks() {
283+
MockitoAnnotations.initMocks(this);
284+
}
285+
286+
@Test
287+
public void usingAnnotationBasedMock() {
288+
mockedList.add("one");
289+
mockedList.clear();
290+
291+
verify(mockedList).add("one");
292+
verify(mockedList).clear();
293+
}
294+
295+
@Test(expected = RuntimeException.class)
296+
public void foo() throws IOException {
297+
File tempFile = folder.newFile();
298+
File tempFile2 = folder.newFile("filename");
299+
File tempDir = folder.getRoot();
300+
File tempDir2 = folder.newFolder("parent", "child");
301+
File tempDir3 = folder.newFolder("subdir");
302+
File tempDir4 = folder.newFolder();
303+
String foo = "foo";
304+
throw new RuntimeException(foo);
305+
}
306+
307+
@Test(expected = IndexOutOfBoundsException.class)
308+
public void foo2() {
309+
int arr = new int[]{}[0];
310+
}
311+
312+
@Rule
313+
public ExpectedException throwz = ExpectedException.none();
314+
315+
@Test
316+
public void foo3() {
317+
throwz.expect(RuntimeException.class);
318+
throw new RuntimeException();
319+
}
320+
321+
@Test
322+
public void assertsStuff() {
323+
Assert.assertEquals("One is one", 1, 1);
324+
Assert.assertArrayEquals("Empty is empty", new int[]{}, new int[]{});
325+
Assert.assertNotEquals("one is not two", 1, 2);
326+
Assert.assertFalse("false is false", false);
327+
Assert.assertTrue("true is true", true);
328+
Assert.assertEquals("foo is foo", "foo", "foo");
329+
Assert.assertNull("null is null", null);
330+
Assert.fail("fail");
331+
}
332+
333+
@Test(timeout = 500)
334+
public void bar() { }
335+
336+
@Test
337+
public void aTest() {
338+
String foo = mock(String.class);
339+
when(foo.concat(any())).then(invocation -> invocation.getArgumentAt(0, String.class));
340+
}
341+
}
342+
343+
""",
344+
after = """
345+
package org.openrewrite.java.testing.junit5;
346+
import org.junit.jupiter.api.*;
347+
import org.junit.jupiter.api.io.TempDir;
348+
import org.mockito.Mock;
349+
import org.mockito.MockitoAnnotations;
350+
351+
import java.io.File;
352+
import java.io.IOException;
353+
import java.nio.file.Files;
354+
import java.util.List;
355+
356+
import static org.junit.jupiter.api.Assertions.assertThrows;
357+
import static org.mockito.Mockito.*;
358+
359+
public class ExampleJunitTestClass {
360+
361+
// @Rule
362+
// public Timeout globalTimeout = new Timeout(500);
363+
364+
@TempDir
365+
public File folder;
366+
367+
@BeforeEach
368+
public void beforeClass() {
369+
MockitoAnnotations.initMocks(this);
370+
}
371+
372+
@AfterAll
373+
public static void afterClass() { }
374+
375+
@Mock
376+
List<String> mockedList;
377+
378+
public void initMocks() {
379+
MockitoAnnotations.initMocks(this);
380+
}
381+
382+
@Test
383+
public void usingAnnotationBasedMock() {
384+
mockedList.add("one");
385+
mockedList.clear();
386+
387+
verify(mockedList).add("one");
388+
verify(mockedList).clear();
389+
}
390+
391+
@Test
392+
public void foo() throws IOException {
393+
assertThrows(RuntimeException.class, () -> {
394+
File tempFile = File.createTempFile("junit", null, folder);
395+
File tempFile2 = newFile(folder, "filename");
396+
File tempDir = folder;
397+
File tempDir2 = newFolder(folder, "parent", "child");
398+
File tempDir3 = newFolder(folder, "subdir");
399+
File tempDir4 = Files.createTempDirectory(folder.toPath(), "junit").toFile();
400+
String foo = "foo";
401+
throw new RuntimeException(foo);
402+
});
403+
}
404+
405+
@Test
406+
public void foo2() {
407+
assertThrows(IndexOutOfBoundsException.class, () -> {
408+
int arr = new int[]{}[0];
409+
});
410+
}
411+
412+
@Test
413+
public void foo3() {
414+
assertThrows(RuntimeException.class, () -> {
415+
throw new RuntimeException();
416+
});
417+
}
418+
419+
@Test
420+
public void assertsStuff() {
421+
Assertions.assertEquals(1, 1, "One is one");
422+
Assertions.assertArrayEquals(new int[]{}, new int[]{}, "Empty is empty");
423+
Assertions.assertNotEquals(1, 2, "one is not two");
424+
Assertions.assertFalse(false, "false is false");
425+
Assertions.assertTrue(true, "true is true");
426+
Assertions.assertEquals("foo", "foo", "foo is foo");
427+
Assertions.assertNull(null, "null is null");
428+
Assertions.fail("fail");
429+
}
430+
431+
@Test
432+
@Timeout(500)
433+
public void bar() { }
434+
435+
@Test
436+
public void aTest() {
437+
String foo = mock(String.class);
438+
when(foo.concat(any())).then(invocation -> invocation.getArgument(0, String.class));
439+
}
440+
441+
private static File newFile(File root, String fileName) throws IOException {
442+
File file = new File(root, fileName);
443+
file.createNewFile();
444+
return file;
445+
}
446+
447+
private static File newFolder(File root, String ... folders) throws IOException {
448+
File result = new File(root, String.join("/", folders));
449+
if(!result.mkdirs()) {
450+
throw new IOException("Couldn't create folders " + root);
451+
}
452+
return result;
453+
}
454+
}
455+
"""
456+
)
236457
}

0 commit comments

Comments
 (0)