I would like to set a timeout for Spring integration test.
For example this basic test hangs on my side:
@SpringBootTest
public class MainTest {
@Test
@Timeout(value = 10)
void contextLoads() {
}
}
It hangs, because I have JMS client which cannot initialize connection and waits forever.
Obviously one approach is to change the JMS client, so I set it some timeout during the connection.
But I would like to have more generic approach - to be able so set timeout on tests, so if for some reason they do not complete within specific timeframe they fail.
Obviosly in the test above I tried to use the @Timeout
annotation (from Junit), but it didn't work.
In my gradle configs I have:
test {
useJUnitPlatform()
}
For some reason I cannot find such information in internet and cannot find related topics/question. Maybe I am missing something super basic.
So can you advice me how to achieve having timeout per test in Spring? I am looking for something integrated (as this sounds super basic functionality for me) which close contexts, etc and I don't want something super complicated hand made thing. Maybe it will be even more useful, if it can do thread dump in case of timeout, but that's a bonus for now.
The version I am using is 2.7.18 of spring boot (this) and soon we will migrate to latest 3+ version.
Thanks in advance for your attention!
@Timeout
? are u using junit 5 and test cases executing on junit 5 engine ?import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.springframework.boot.test.context.SpringBootTest;
About your second question - doesn'tuseJUnitPlatform()
set exactly to use the Junit engine? Should I add something else?junit.jupiter.execution.timeout.beforeall.method.default=10s
andjunit.jupiter.execution.timeout.beforeeach.method.default=10s
does not work, because with the SpringExtension, the Spring initialization is also not run in a@BeforeEach
or a@BeforeAll
method