Skip to content

Commit b079058

Browse files
timtebeekTeamModerne
authored and
app
committed
refactor: Java 17 features for tests
Use this link to re-run the recipe: https://app.moderne.io/builder/PM23vSNHB?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <[email protected]>
1 parent 4de7603 commit b079058

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void messageFromDirectObject(String message) {
300300
//language=java
301301
rewriteRun(
302302
java(
303-
String.format("""
303+
"""
304304
import static org.junit.jupiter.api.Assertions.assertThrows;
305305
306306
public class SimpleExpectedExceptionTest {
@@ -311,8 +311,8 @@ void foo() {
311311
throw new NullPointerException();
312312
}
313313
}
314-
""", message),
315-
String.format("""
314+
""".formatted(message),
315+
"""
316316
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
317317
318318
public class SimpleExpectedExceptionTest {
@@ -323,7 +323,7 @@ void foo() {
323323
throw new NullPointerException();
324324
}
325325
}
326-
""", message)
326+
""".formatted(message)
327327
)
328328
);
329329
}
@@ -334,7 +334,7 @@ void withMemberReference(String message) {
334334
//language=java
335335
rewriteRun(
336336
java(
337-
String.format("""
337+
"""
338338
import static org.junit.jupiter.api.Assertions.assertThrows;
339339
import java.util.concurrent.CompletableFuture;
340340
import java.util.concurrent.ExecutionException;
@@ -345,8 +345,8 @@ public void throwsExceptionWithSpecificType() {
345345
assertThrows(ExecutionException.class, future::get, %s);
346346
}
347347
}
348-
""", message),
349-
String.format("""
348+
""".formatted(message),
349+
"""
350350
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
351351
import java.util.concurrent.CompletableFuture;
352352
import java.util.concurrent.ExecutionException;
@@ -357,7 +357,7 @@ public void throwsExceptionWithSpecificType() {
357357
assertThatExceptionOfType(ExecutionException.class).as(%s).isThrownBy(future::get);
358358
}
359359
}
360-
""", message)
360+
""".formatted(message)
361361
)
362362
);
363363
}
@@ -368,7 +368,7 @@ void withReturnValue(String message) {
368368
//language=java
369369
rewriteRun(
370370
java(
371-
String.format("""
371+
"""
372372
import static org.junit.jupiter.api.Assertions.assertThrows;
373373
374374
public class SimpleExpectedExceptionTest {
@@ -379,8 +379,8 @@ void foo() {
379379
throw new NullPointerException();
380380
}
381381
}
382-
""", message),
383-
String.format("""
382+
""".formatted(message),
383+
"""
384384
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
385385
386386
public class SimpleExpectedExceptionTest {
@@ -391,7 +391,7 @@ void foo() {
391391
throw new NullPointerException();
392392
}
393393
}
394-
""", message)
394+
""".formatted(message)
395395
)
396396
);
397397

@@ -403,7 +403,7 @@ void messageFromVariable(String message) {
403403
//language=java
404404
rewriteRun(
405405
java(
406-
String.format("""
406+
"""
407407
import static org.junit.jupiter.api.Assertions.assertThrows;
408408
409409
public class SimpleExpectedExceptionTest {
@@ -416,8 +416,8 @@ void foo() {
416416
throw new NullPointerException();
417417
}
418418
}
419-
""", message),
420-
String.format("""
419+
""".formatted(message),
420+
"""
421421
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
422422
423423
public class SimpleExpectedExceptionTest {
@@ -430,7 +430,7 @@ void foo() {
430430
throw new NullPointerException();
431431
}
432432
}
433-
""", message)
433+
""".formatted(message)
434434
)
435435
);
436436
}

src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionsTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ void test() {
120120
String finalArgument = firstArg.isEmpty() && !"0".equals(secondArg) ? secondArg : firstArg;
121121
finalArgument = finalArgument.contains(".") ? finalArgument.split("\\.")[0] : finalArgument;
122122

123-
String before = String.format(template, formattedAssertBefore);
124-
String after = String.format(template, assertAfter.formatted(dedicatedAssertion, finalArgument));
123+
String before = template.formatted(formattedAssertBefore);
124+
String after = template.formatted(assertAfter.formatted(dedicatedAssertion, finalArgument));
125125

126126
rewriteRun(
127127
java(before, after)
@@ -176,8 +176,8 @@ void test() {
176176
String finalArgument = "".equals(firstArg) && !"0".equals(secondArg) ? secondArg : firstArg;
177177
finalArgument = finalArgument.contains(".") ? finalArgument.split("\\.")[0] : finalArgument;
178178

179-
String before = String.format(template, formattedAssertBefore);
180-
String after = String.format(template, assertAfter.formatted(dedicatedAssertion, finalArgument));
179+
String before = template.formatted(formattedAssertBefore);
180+
String after = template.formatted(assertAfter.formatted(dedicatedAssertion, finalArgument));
181181

182182
rewriteRun(
183183
java(before, after)
@@ -220,8 +220,8 @@ void test() {
220220
String finalArgument = "".equals(firstArg) && !"0".equals(secondArg) ? secondArg : firstArg;
221221
finalArgument = finalArgument.contains(".") ? finalArgument.split("\\.")[0] : finalArgument;
222222

223-
String before = String.format(template, formattedAssertBefore);
224-
String after = String.format(template, assertAfter.formatted(dedicatedAssertion, finalArgument));
223+
String before = template.formatted(formattedAssertBefore);
224+
String after = template.formatted(assertAfter.formatted(dedicatedAssertion, finalArgument));
225225

226226
rewriteRun(
227227
java(before, after)
@@ -266,8 +266,8 @@ void test(Collection<String> collection, Collection<String> otherCollection) {
266266

267267
String finalArgument = "".equals(firstArg) ? secondArg : firstArg;
268268

269-
String before = String.format(template, formattedAssertBefore);
270-
String after = String.format(template, assertAfter.formatted(dedicatedAssertion, finalArgument));
269+
String before = template.formatted(formattedAssertBefore);
270+
String after = template.formatted(assertAfter.formatted(dedicatedAssertion, finalArgument));
271271

272272
rewriteRun(
273273
java(before, after)
@@ -310,14 +310,14 @@ void test() {
310310
String assertAfter = !"".equals(firstArg) && !"".equals(secondArg) ? "assertThat(map).%s(%s, %s);" : "assertThat(map).%s(%s);";
311311

312312
String formattedAssertBefore = assertBefore.formatted(chainedAssertion, firstArg, assertToReplace, secondArg);
313-
String before = String.format(template, formattedAssertBefore);
313+
String before = template.formatted(formattedAssertBefore);
314314

315315
String finalArgument = "".equals(firstArg) ? secondArg : firstArg;
316316
List<String> formattedArgs = new ArrayList<>(Arrays.asList(dedicatedAssertion, finalArgument));
317317
if (!"".equals(firstArg) && !"".equals(secondArg)) {
318318
formattedArgs.add(secondArg);
319319
}
320-
String after = String.format(template, assertAfter.formatted(formattedArgs.toArray()));
320+
String after = template.formatted(assertAfter.formatted(formattedArgs.toArray()));
321321

322322
rewriteRun(
323323
java(before, after)
@@ -388,11 +388,11 @@ void test() {
388388
}
389389
""";
390390

391-
String assertBefore = String.format("assertThat(helloWorld.%s()).%s(%s);", chainedAssertion, assertToReplace, arg);
392-
String assertAfter = String.format("assertThat(helloWorld).%s(%s);", dedicatedAssertion, arg);
391+
String assertBefore = "assertThat(helloWorld.%s()).%s(%s);".formatted(chainedAssertion, assertToReplace, arg);
392+
String assertAfter = "assertThat(helloWorld).%s(%s);".formatted(dedicatedAssertion, arg);
393393

394-
String before = String.format(template, assertBefore);
395-
String after = String.format(template, assertAfter);
394+
String before = template.formatted(assertBefore);
395+
String after = template.formatted(assertAfter);
396396

397397
rewriteRun(java(before, after));
398398
}
@@ -428,8 +428,8 @@ void test(Iterator<String> iterator, Iterator<String> otherIterator) {
428428

429429
String formattedAssertBefore = assertBefore.formatted(chainedAssertion, assertToReplace);
430430

431-
String before = String.format(template, formattedAssertBefore);
432-
String after = String.format(template, assertAfter.formatted(dedicatedAssertion));
431+
String before = template.formatted(formattedAssertBefore);
432+
String after = template.formatted(assertAfter.formatted(dedicatedAssertion));
433433

434434
rewriteRun(
435435
java(before, after)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ void mockitoRunnerToExtension(String runnerName, String strictness) {
4646
//language=java
4747
rewriteRun(
4848
java(
49-
String.format("""
49+
"""
5050
import org.junit.runner.RunWith;
5151
import org.mockito.junit.MockitoJUnitRunner;
5252
5353
@RunWith(%s)
5454
public class ExternalAPIServiceTest {
5555
}
56-
""", runnerName),
57-
String.format("""
56+
""".formatted(runnerName),
57+
"""
5858
import org.junit.jupiter.api.extension.ExtendWith;
5959
import org.mockito.junit.jupiter.MockitoExtension;
6060
import org.mockito.junit.jupiter.MockitoSettings;
@@ -64,7 +64,7 @@ public class ExternalAPIServiceTest {
6464
@ExtendWith(MockitoExtension.class)
6565
public class ExternalAPIServiceTest {
6666
}
67-
""", strictness)
67+
""".formatted(strictness)
6868
)
6969
);
7070
}

0 commit comments

Comments
 (0)