简体   繁体   中英

JUnit timeout on @BeforeClass

Tests in JUnit, annotated with @Test can have a timeout set in ms, for example:

@Test(timeout = 1000)
public void testSomething(){
    ....
}

However, I can't seem to find a way to put a timeout on the setup (or @BeforeClass).

Is there any other sensible way to do this? I wonder if there is some reason @BeforeClass cannot have a timeout?

I would like to do this:

@BeforeClass(timeout = 1000) // <-- Not currently possible
public static void setup(){
    doSomethingWhichMayRunForever();
}

Edit: added 'static'

I found some code related to the same in gitHub below. I had a scenario in our application as well to have timeout in @BeforeClass. I read its not a good practise to have conditions / logic added in @BeforeClass based annotated method. So, i handled condition in each @Test method.

See the link if it helps :

https://github.com/jaruk/junit/commit/c5176f18139b9b99dfbc6540d4a6b6a8f049e568

In JUnit marking the whole class for timeout is not possible .

In TestNG you can achieve that by anotating the test class with @Test annotation:

@Test(timeOut = 500)
public class Mavenproject1Suite {...}

在JUnit中,用@BeforeClass注释的方法必须是静态的,我想知道如果没有它,如何可以执行您的方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM