@@ -26,9 +26,11 @@ import org.openrewrite.java.tree.J
2626 */
2727class 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