In addition to the other answers which specify a timeout for a specific test (per the OP request), beginning in JUnit 5.5 it is also possible to configure a global timeout, a useful option instead of adding the @Timeout
annotation to every method.
Per the JUnit 5 User Guide, which documents the syntax in the other answers:
configuration parameters can be used to specify global timeouts for all methods of a certain category unless they or an enclosing test class is annotated with @Timeout
For example, this would set a global timeout of 500 milliseconds for all testable and lifecycle methods:
junit.jupiter.execution.timeout.default = 500 ms
The timeouts can be more narrowly scoped, for example just @Test
methods:
junit.jupiter.execution.timeout.testtemplate.method.default = 500 ms
The @Timeout
annotation described in the other answers will override these defaults if it is used.