Skip to content

Commit af84b02

Browse files
authored
Add unit to @Timeout (#451)
Fixes #450
1 parent 326c048 commit af84b02

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,17 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
151151
}
152152
}
153153
if (cta.timeout != null) {
154-
m = JavaTemplate.builder("@Timeout(#{any(long)})")
154+
m = JavaTemplate.builder("@Timeout(value = #{any(long)}, unit = TimeUnit.MILLISECONDS)")
155155
.javaParser(javaParser(ctx))
156-
.imports("org.junit.jupiter.api.Timeout")
156+
.imports("org.junit.jupiter.api.Timeout", "java.util.concurrent.TimeUnit")
157157
.build()
158158
.apply(
159159
updateCursor(m),
160160
m.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)),
161161
cta.timeout
162162
);
163163
maybeAddImport("org.junit.jupiter.api.Timeout");
164+
maybeAddImport("java.util.concurrent.TimeUnit");
164165
}
165166
maybeAddImport("org.junit.jupiter.api.Test");
166167
}

src/test/java/org/openrewrite/java/testing/junit5/UpdateTestAnnotationTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ public void test2() {
272272
}
273273

274274
@Test
275+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/450")
275276
void annotationWithTimeout() {
276277
//language=java
277278
rewriteRun(
@@ -290,10 +291,12 @@ public void test() {
290291
import org.junit.jupiter.api.Test;
291292
import org.junit.jupiter.api.Timeout;
292293
294+
import java.util.concurrent.TimeUnit;
295+
293296
public class MyTest {
294297
295298
@Test
296-
@Timeout(500)
299+
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
297300
public void test() {
298301
}
299302
}
@@ -369,12 +372,14 @@ public void test() {
369372
import org.junit.jupiter.api.Test;
370373
import org.junit.jupiter.api.Timeout;
371374
375+
import java.util.concurrent.TimeUnit;
376+
372377
import static org.junit.jupiter.api.Assertions.assertThrows;
373378
374379
public class MyTest {
375380
376381
@Test
377-
@Timeout(500)
382+
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
378383
public void test() {
379384
assertThrows(IllegalArgumentException.class, () -> {
380385
throw new IllegalArgumentException("boom");
@@ -518,7 +523,7 @@ public void feature1() {
518523
}
519524
520525
@Test
521-
void feature2() {
526+
public void feature2() {
522527
}
523528
}
524529
""",
@@ -531,7 +536,7 @@ public void feature1() {
531536
}
532537
533538
@Test
534-
void feature2() {
539+
public void feature2() {
535540
}
536541
}
537542
"""

0 commit comments

Comments
 (0)