Java: unable to run runnable in test case: void run () collides

So, I have a test case that I want to do in a thread. I cannot extend Thread and cannot implement runnable since TestCase already has a void run () method. The compilation error I get is Error(62,17): method run() in class com.util.SeleneseTestCase cannot override method run() in class junit.framework.TestCase with different return type, was class junit.framework.TestResult.

What I'm trying to do is scale the Selenium test for stress testing. Currently, I cannot use the selenium grid /pushtotest.com/amazon cloud (installation problems / installation time / resource problems). Therefore, for me, this is really a Java language problem.

FYI: SeleniumTestCase is what I want to do multi-threaded in order to scale it for stress testing. SelniumTestCase extends TestCase (from junit). I am extending SeleniumTestCase and trying to implement Runnable.

+3
source share
4 answers

Create an inner class that implements Runnable and is called from a new thread in com.util.SeleneseTestCase run (). Something like that:

class YourTestCase extends SeleneseTestCase {
    public class MyRunnable implements Runnable {
        public void run() {
            // Do your have work here
        }
    }

    public void testMethodToExecuteInThread() {
        MyRunnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start();
    }
}

Update for use outside of YourTestCase class

To start the inner class from another class, you need to make it public, and then from the outer class, do this:

YourTestCase testCase = new YourTestCase();
YourTestCase.MyRunnable r = testCase.new MyRunnable();

, , MyRunnable , YourTestCase.

, .

+6

: .

+4

, - , . Runnable Thread ExecutorService.submit(Callable<T>):

public class SeleneseTestCase extends SeleniumTestCase {
  private class StressServer implements Callable<Void> {
    public Void call() {
      // do your work here
      return null;
    }
  }

  public void testUnderLoad() throws Exception {
    ExecutorService executorService = Executors.newFixedThreadPool(
        NUM_CONCURRENT_WORKERS);
    List<Callable<Void>> stressers = new ArrayList<Callable<Void>>();
    for (int i = 0; i < NUM_WORKERS; i++) }
      stressers.add(new StressServer());
    }
    List<Future<Void>> futures =if ( executorService.invokeAll(
        stressers, TIMEOUT_IN_SECS, TimeUnit.SECONDS);
    for (Future<Void> future : futures) {
      if (!future.isCancelled()) {
        future.get(1, TimeUnit.MILLISECONDS); // may throw exception
      }
    }
    executorService.shutdown();
  }
}

, , , StressServer Callable<YourResultType>

+1

The TestCase extension is almost certainly not the right OO model. the model with has no is. that is, create a new class that extends the stream, and use instances of that class in your testing methods.

0
source

Source: https://habr.com/ru/post/1745842/


All Articles