Java – run JUnit tests for a certain amount of time

I want to test for a period of time, such as 5 seconds, and then pass the test if no error is asserted Can this be annotated? Can I use something like @ test (uptime = 5000)?

Solution

The revised question is the revised answer

Basically, it feels like you're testing something wrong here - it seems strange that "nothing happened" is a sign of success

If you want to prove that your algorithm can run for a period of time without failure, I will actually extract a loop and write a similar test:

@Test
public void fineForFiveSeconds() {
    long start = System.nanoTime();
    long end = start + TimeUnit.SECONDS.toNanos(5);
    while (System.nanoTime < end()) {
        test.executeOneIteration();
    }
}

So you don't have a separate thread, it has to kill working code, etc

Original answer

This answer is written before the question indicates that timeout is a sign of success, not failure

I think you just want the timeout attribute in the @ test annotation:

@Test(timeout = 5000)

Available documents:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>