java - How to return from JUnit test to main method if part of test takes too long -


i try run junit main() method:

public static void main(string... args) throws classnotfoundexception,         ioexception {   //...   logger.debug("classname " + classname + "methodname " + methodname);    request request = request.method(class.forname(classname), methodname);   return new junitcore().run(request); } 

i have e2e test 10 commands (say). run junit , want limit run time of commands 3-5 x millis (where x determined @ run time). if runs longer x want return main() , print something.

i have tried system.exit() closes whole application. tried:

public void settimeout(string criticalblocktimeoutmilli) {     if (criticalblocktimeoutmilli != null) {         timertask timertask = new timertask() {             @override             public void run() {                 e2eresult e2eresult = e2eresult.getinstance();                 e2eresult.status = e2estatus.timeout;                 //system.exit(2);             }         };         new timer().schedule(timertask, long.parselong(criticalblocktimeoutmilli));     } }  public void settimeout(final thread thread, string criticalblocktimeoutmilli) {     if (criticalblocktimeoutmilli != null) {         timertask timertask = new timertask() {             @override             public void run() {                 e2eresult e2eresult = e2eresult.getinstance();                 e2eresult.status = e2estatus.timeout;                 thread.interrupt();             }         };         new timer().schedule(timertask, long.parselong(criticalblocktimeoutmilli));     } } 

but main thread continues run test if exceeds limit. suggest?

unit testing might not best approach solving sort of performance testing. however, if there's reason must done, read on...

use executorservice run commands want, given timeout. if timeout expires, throw own exception can catch in main thread:

@test public void yourtest() throws exception {    // commands 1-2    executorservice service = executors.newsinglethreadexecutor();   future<void> result = service.submit(new callable<void>() {     @override     public void call() throws exception {       // call commands 3-5       return null;     }   });    try {     result.get(42, timeunit.milliseconds);   } catch (timeoutexception e) {     throw new yourownexception();   }    service.shutdown();    // commands 6-10 } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -