-1

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!

7
  • can u show the import of @Timeout ? are u using junit 5 and test cases executing on junit 5 engine ?
    – Ryuzaki L
    Commented Jul 1 at 19:17
  • Of course, these are all of the import (of that simple test) import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.springframework.boot.test.context.SpringBootTest; About your second question - doesn't useJUnitPlatform() set exactly to use the Junit engine? Should I add something else?
    – DPM
    Commented Jul 2 at 6:38
  • you should be able to see in the log or try to run test using junit5 through IDE configs
    – Ryuzaki L
    Commented Jul 2 at 8:11
  • I had a look at this last night and found that the @Timeout doesn't work on the test because the initialization of the Spring context is not part of the test. Also, setting the propertiesjunit.jupiter.execution.timeout.beforeall.method.default=10s and junit.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
    – Polly Shaw
    Commented Jul 2 at 9:00
  • @RyuzakiL, yep I did that. Furthermore when I create thread dump I can see Junit5 classes in the begginng of the call stack.
    – DPM
    Commented Jul 2 at 9:30

0

Browse other questions tagged or ask your own question.