how can I set a timeout to a block in a test (junit)?

Elad Benda

I want to limit a certain block in a test to 1 second.

I have tried this:

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            throw new TimeoutException("test time: expected:"+expectedTimeMilli);
        }
    } ;
    new Timer().schedule(timerTask, 1L);

but the complier asks to wrap the run() body with try-catch.

I want to throw this exception to be caught in main() methd (and return result code 3)

how can I do this?

user1717259

Rather than throwing TimeoutException, have it throw your own Exception that extends RuntimeException.

Note that this will kill your timer thread. If this is an issue, use ScheduledExecutorService

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I simulate keypress in junit test?

From Dev

How can I JUnit test "printlist"?

From Dev

How can I analyze a junit test suite?

From Dev

How can I set a timeout on "return false"?

From Dev

How to set timeout for anonoymous block or query in plsql?

From Dev

How to set timeout for anonoymous block or query in plsql?

From Dev

How can I build a configurable JUnit4 test suite?

From Dev

How can I return a multidimensional array as a collection of parameters for a JUnit test?

From Dev

How can I write a JUnit-Test for custom Jackson Serializer?

From Dev

How can I JUnit test a model within Android?

From Dev

How can i test Optmistic Locking using junit, spring and JPA?

From Dev

How can I JUnit test a model within Android?

From Dev

How can I set a timeout for a zmq request in Node.js?

From Dev

How can I set the job timeout using the Jenkins DSL

From Dev

how can I set a callback for the user session timeout

From Dev

How can I set the Liquibase database connection timeout and retry count?

From Dev

How can I set connection timeout in Retrofit library for Android?

From Dev

How can I retain a local variable that is set in a completion block?

From Dev

Magento - How can I set a diffrent phtml for a block in an observer?

From Dev

How to set the timeout of test in dart's unittest?

From Java

How to fail a test after a timeout is exceeded in JUnit 5?

From Dev

How to fail a test after a timeout is exceeded in JUnit 5?

From Dev

Junit test - No errors that I can see?

From Dev

Can I change junit test execution order?

From Dev

Can I change junit test execution order?

From Java

In JUnit 5 test, how can I test a number whether it is positve or negative or a mix of both?

From Dev

Can I set a timeout for copying files in Ubuntu?

From Dev

Why I get this error in Protractor of Jasmine timeout and how can i test correctly?

From Dev

JUnit how set up test class?

Related Related

  1. 1

    How can I simulate keypress in junit test?

  2. 2

    How can I JUnit test "printlist"?

  3. 3

    How can I analyze a junit test suite?

  4. 4

    How can I set a timeout on "return false"?

  5. 5

    How to set timeout for anonoymous block or query in plsql?

  6. 6

    How to set timeout for anonoymous block or query in plsql?

  7. 7

    How can I build a configurable JUnit4 test suite?

  8. 8

    How can I return a multidimensional array as a collection of parameters for a JUnit test?

  9. 9

    How can I write a JUnit-Test for custom Jackson Serializer?

  10. 10

    How can I JUnit test a model within Android?

  11. 11

    How can i test Optmistic Locking using junit, spring and JPA?

  12. 12

    How can I JUnit test a model within Android?

  13. 13

    How can I set a timeout for a zmq request in Node.js?

  14. 14

    How can I set the job timeout using the Jenkins DSL

  15. 15

    how can I set a callback for the user session timeout

  16. 16

    How can I set the Liquibase database connection timeout and retry count?

  17. 17

    How can I set connection timeout in Retrofit library for Android?

  18. 18

    How can I retain a local variable that is set in a completion block?

  19. 19

    Magento - How can I set a diffrent phtml for a block in an observer?

  20. 20

    How to set the timeout of test in dart's unittest?

  21. 21

    How to fail a test after a timeout is exceeded in JUnit 5?

  22. 22

    How to fail a test after a timeout is exceeded in JUnit 5?

  23. 23

    Junit test - No errors that I can see?

  24. 24

    Can I change junit test execution order?

  25. 25

    Can I change junit test execution order?

  26. 26

    In JUnit 5 test, how can I test a number whether it is positve or negative or a mix of both?

  27. 27

    Can I set a timeout for copying files in Ubuntu?

  28. 28

    Why I get this error in Protractor of Jasmine timeout and how can i test correctly?

  29. 29

    JUnit how set up test class?

HotTag

Archive