|
18 | 18 | import org.junit.jupiter.api.Test; |
19 | 19 | import org.openrewrite.DocumentExample; |
20 | 20 | import org.openrewrite.InMemoryExecutionContext; |
| 21 | +import org.openrewrite.Issue; |
21 | 22 | import org.openrewrite.java.JavaParser; |
22 | 23 | import org.openrewrite.test.RecipeSpec; |
23 | 24 | import org.openrewrite.test.RewriteTest; |
@@ -267,4 +268,137 @@ void test() { |
267 | 268 | ) |
268 | 269 | ); |
269 | 270 | } |
| 271 | + |
| 272 | + @Issue("https://github.com/moderneinc/customer-requests/issues/1731") |
| 273 | + @Test |
| 274 | + void shouldHandleFullyQualifiedMockitoMockConstruction() { |
| 275 | + //language=java |
| 276 | + rewriteRun( |
| 277 | + java( |
| 278 | + """ |
| 279 | + import org.junit.jupiter.api.Test; |
| 280 | + import org.mockito.MockedConstruction; |
| 281 | + import org.mockito.Mockito; |
| 282 | +
|
| 283 | + import static org.junit.jupiter.api.Assertions.assertEquals; |
| 284 | + import static org.mockito.Mockito.when; |
| 285 | + import static org.mockito.ArgumentMatchers.any; |
| 286 | +
|
| 287 | + class TestClass { |
| 288 | + @Test |
| 289 | + void test() { |
| 290 | + MockedConstruction<A> aMockedConstruction = Mockito.mockConstruction(A.class, (mock, context) -> { |
| 291 | + when(mock.method(any())).thenReturn("XYZ"); |
| 292 | + }); |
| 293 | + A instance = new A(); |
| 294 | + assertEquals("XYZ", instance.method("test")); |
| 295 | + aMockedConstruction.close(); |
| 296 | + } |
| 297 | + } |
| 298 | + """, |
| 299 | + """ |
| 300 | + import org.junit.jupiter.api.Test; |
| 301 | + import org.mockito.MockedConstruction; |
| 302 | + import org.mockito.Mockito; |
| 303 | +
|
| 304 | + import static org.junit.jupiter.api.Assertions.assertEquals; |
| 305 | + import static org.mockito.Mockito.when; |
| 306 | + import static org.mockito.ArgumentMatchers.any; |
| 307 | +
|
| 308 | + class TestClass { |
| 309 | + @Test |
| 310 | + void test() { |
| 311 | + try (MockedConstruction<A> aMockedConstruction = Mockito.mockConstruction(A.class, (mock, context) -> { |
| 312 | + when(mock.method(any())).thenReturn("XYZ"); |
| 313 | + })) { |
| 314 | + A instance = new A(); |
| 315 | + assertEquals("XYZ", instance.method("test")); |
| 316 | + } |
| 317 | + } |
| 318 | + } |
| 319 | + """ |
| 320 | + ) |
| 321 | + ); |
| 322 | + } |
| 323 | + |
| 324 | + @Issue("https://github.com/moderneinc/customer-requests/issues/1731") |
| 325 | + @Test |
| 326 | + void shouldHandleMultipleWhensInLambda() { |
| 327 | + //language=java |
| 328 | + rewriteRun( |
| 329 | + spec -> spec.parser(JavaParser.fromJavaVersion() |
| 330 | + .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5", "mockito-core-5") |
| 331 | + //language=java |
| 332 | + .dependsOn( |
| 333 | + """ |
| 334 | + import java.util.List; |
| 335 | + public class ItemInflator { |
| 336 | + public List<String> inflateNodesWithItemStore(List<Object> items) { |
| 337 | + return null; |
| 338 | + } |
| 339 | + public List<String> inflateWithIRO(List<Object> items) { |
| 340 | + return null; |
| 341 | + } |
| 342 | + } |
| 343 | + """ |
| 344 | + ) |
| 345 | + ), |
| 346 | + java( |
| 347 | + """ |
| 348 | + import org.junit.jupiter.api.Test; |
| 349 | + import org.mockito.MockedConstruction; |
| 350 | + import org.mockito.Mockito; |
| 351 | +
|
| 352 | + import java.util.List; |
| 353 | +
|
| 354 | + import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 355 | + import static org.mockito.Mockito.when; |
| 356 | + import static org.mockito.ArgumentMatchers.anyList; |
| 357 | +
|
| 358 | + class TestClass { |
| 359 | + private static final List<String> productList = List.of("product1", "product2"); |
| 360 | +
|
| 361 | + @Test |
| 362 | + void test() { |
| 363 | + MockedConstruction<ItemInflator> itemInflatorMockedConstruction = Mockito.mockConstruction(ItemInflator.class, (mock, context) -> { |
| 364 | + when(mock.inflateNodesWithItemStore(anyList())).thenReturn(productList); |
| 365 | + when(mock.inflateWithIRO(anyList())).thenReturn(productList); |
| 366 | + }); |
| 367 | + ItemInflator inflator = new ItemInflator(); |
| 368 | + assertNotNull(inflator.inflateNodesWithItemStore(List.of())); |
| 369 | + assertNotNull(inflator.inflateWithIRO(List.of())); |
| 370 | + itemInflatorMockedConstruction.close(); |
| 371 | + } |
| 372 | + } |
| 373 | + """, |
| 374 | + """ |
| 375 | + import org.junit.jupiter.api.Test; |
| 376 | + import org.mockito.MockedConstruction; |
| 377 | + import org.mockito.Mockito; |
| 378 | +
|
| 379 | + import java.util.List; |
| 380 | +
|
| 381 | + import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 382 | + import static org.mockito.Mockito.when; |
| 383 | + import static org.mockito.ArgumentMatchers.anyList; |
| 384 | +
|
| 385 | + class TestClass { |
| 386 | + private static final List<String> productList = List.of("product1", "product2"); |
| 387 | +
|
| 388 | + @Test |
| 389 | + void test() { |
| 390 | + try (MockedConstruction<ItemInflator> itemInflatorMockedConstruction = Mockito.mockConstruction(ItemInflator.class, (mock, context) -> { |
| 391 | + when(mock.inflateNodesWithItemStore(anyList())).thenReturn(productList); |
| 392 | + when(mock.inflateWithIRO(anyList())).thenReturn(productList); |
| 393 | + })) { |
| 394 | + ItemInflator inflator = new ItemInflator(); |
| 395 | + assertNotNull(inflator.inflateNodesWithItemStore(List.of())); |
| 396 | + assertNotNull(inflator.inflateWithIRO(List.of())); |
| 397 | + } |
| 398 | + } |
| 399 | + } |
| 400 | + """ |
| 401 | + ) |
| 402 | + ); |
| 403 | + } |
270 | 404 | } |
0 commit comments