Java:无法在测试用例上实现可运行:void run() 发生冲突

发布于 2024-09-01 12:18:11 字数 576 浏览 5 评论 0原文

所以我有一个测试用例想要做成一个线程。我无法扩展 Thread,也无法实现 runnable,因为 TestCase 已经有一个方法 void run()。我收到的编译错误是 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.框架.TestResult

我想做的是扩大 Selenium 测试用例的规模以执行压力测试。我目前无法使用 selenium grid/pushtotest.com/amazon cloud(安装问题/安装时间/资源问题)。所以这对我来说实际上更像是一个 Java 语言问题。

仅供参考: SeleniumTestCase 是我想要进行多线程扩展以进行压力测试的工具。 SelniumTestCase 扩展了 TestCase(来自 junit)。我正在扩展 SeleniumTestCase 并尝试使其实现 Runnable。

So I have a test case that I want to make into a thread. I cannot extend Thread nor can I implement runnable since TestCase already has a method void run(). The compilation error I am getting 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 am trying to do is to scale a Selenium testcase up to perform stress testing. I am not able to use selenium grid/pushtotest.com/amazon cloud at this time (installation issues/install time/resource issues). So this really is more of a Java language issue for me.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2024-09-08 12:18:11

创建一个实现 Runnable 的内部类,并从 com.util.SeleneseTestCase run() 方法中的新线程调用它。像这样的东西:

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();
    }
}

更新以在 YourTestCase 类外部使用

要从另一个类运行内部类,您需要将其公开,然后从外部类执行此操作:

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

但是如果您不需要调用从您的测试用例内部,您最好使用普通类,使 MyRunnable 成为公共类,而不位于 YourTestCase 中。

希望有帮助。

Create a inner class that implements Runnable and call it from a new Thread in com.util.SeleneseTestCase run() method. Something like this:

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 to use outside YourTestCase class

To run an inner class from another class you would need to make it public and then from the outer class execute this:

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

But if you don't need to call it from inside your test case you'd better go with a normal class, make MyRunnable a public class without being in YourTestCase.

Hope it helps.

撕心裂肺的伤痛 2024-09-08 12:18:11

在这种情况下,您没有其他选择:您必须委托给另一个对象而不是继承。

In this case, you don't have other option: you have to delegate to another object instead of inheritance.

摘星┃星的人 2024-09-08 12:18:11

请记住,如果线程抛出任何异常,测试不一定会失败。您可能希望使用 ExecutorService.submit(Callable),而不是使用 RunnableThread

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

Keep in mind that if the thread throws any exceptions, the test would not necessarily fail. Instead of using Runnable and Thread, you might want to use 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();
  }
}

Note if you want the workers to return the result, you can change the type of StressServer to Callable<YourResultType>

总以为 2024-09-08 12:18:11

无论如何,扩展 TestCase 几乎肯定不是正确的 OO 模型。模型中带有“has a”而不是“is a”。也就是说,创建一个扩展线程的新类,并在测试方法中使用该类的实例。

Extending TestCase is almost certainly not the correct OO model anyhow. Model with "has a" not "is a". That is, create a new class that extends thread, and use instances of that class within your test methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文