|
16 | 16 | package org.openrewrite.java.testing.mockito; |
17 | 17 |
|
18 | 18 | import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
19 | 21 | import org.openrewrite.DocumentExample; |
20 | 22 | import org.openrewrite.InMemoryExecutionContext; |
21 | 23 | import org.openrewrite.java.JavaParser; |
@@ -819,4 +821,67 @@ public final void testNumbers() throws Exception { |
819 | 821 | ) |
820 | 822 | ); |
821 | 823 | } |
| 824 | + |
| 825 | + @ParameterizedTest |
| 826 | + @ValueSource(strings = {"withArguments(\"Have a nice day!\")", "withAnyArguments()"}) |
| 827 | + void whenNewWithArguments(String methodCall) { |
| 828 | + //language=java |
| 829 | + rewriteRun( |
| 830 | + java( |
| 831 | + """ |
| 832 | + import org.powermock.api.mockito.PowerMockito; |
| 833 | + import static org.powermock.api.mockito.PowerMockito.*; |
| 834 | +
|
| 835 | + import org.junit.jupiter.api.Test; |
| 836 | + import static org.junit.jupiter.api.Assertions.assertEquals; |
| 837 | +
|
| 838 | + public class MyTest2 { |
| 839 | + public static class SomeTexts { |
| 840 | + String text; |
| 841 | + public SomeTexts(String text) { this.text = text; } |
| 842 | + public String getText() { return text; } |
| 843 | + } |
| 844 | +
|
| 845 | + @Test |
| 846 | + public final void testWords() throws Exception { |
| 847 | + SomeTexts mock = PowerMockito.mock(SomeTexts.class); |
| 848 | + PowerMockito.whenNew(SomeTexts.class).METHODCALL.thenReturn(mock); |
| 849 | +
|
| 850 | + SomeTexts st = new SomeTexts("Have a nice day!"); |
| 851 | + when(st.getText()).thenReturn("overridden"); |
| 852 | +
|
| 853 | + assertEquals("overridden", st.getText()); |
| 854 | + } |
| 855 | + } |
| 856 | + """.replaceAll("METHODCALL", methodCall), |
| 857 | + """ |
| 858 | + import org.mockito.MockedConstruction; |
| 859 | + import org.mockito.Mockito; |
| 860 | + import static org.mockito.Mockito.when; |
| 861 | +
|
| 862 | + import org.junit.jupiter.api.Test; |
| 863 | + import static org.junit.jupiter.api.Assertions.assertEquals; |
| 864 | +
|
| 865 | + public class MyTest2 { |
| 866 | + public static class SomeTexts { |
| 867 | + String text; |
| 868 | + public SomeTexts(String text) { this.text = text; } |
| 869 | + public String getText() { return text; } |
| 870 | + } |
| 871 | +
|
| 872 | + @Test |
| 873 | + public final void testWords() throws Exception { |
| 874 | + try (MockedConstruction<SomeTexts> mockSomeTexts = Mockito.mockConstruction(SomeTexts.class)) { |
| 875 | +
|
| 876 | + SomeTexts st = new SomeTexts("Have a nice day!"); |
| 877 | + when(st.getText()).thenReturn("overridden"); |
| 878 | +
|
| 879 | + assertEquals("overridden", st.getText()); |
| 880 | + } |
| 881 | + } |
| 882 | + } |
| 883 | + """ |
| 884 | + ) |
| 885 | + ); |
| 886 | + } |
822 | 887 | } |
0 commit comments