Java:无法在测试用例上实现可运行:void run() 发生冲突
所以我有一个测试用例想要做成一个线程。我无法扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个实现 Runnable 的内部类,并从 com.util.SeleneseTestCase run() 方法中的新线程调用它。像这样的东西:
更新以在 YourTestCase 类外部使用
要从另一个类运行内部类,您需要将其公开,然后从外部类执行此操作:
但是如果您不需要调用从您的测试用例内部,您最好使用普通类,使 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:
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:
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.
在这种情况下,您没有其他选择:您必须委托给另一个对象而不是继承。
In this case, you don't have other option: you have to delegate to another object instead of inheritance.
请记住,如果线程抛出任何异常,测试不一定会失败。您可能希望使用
ExecutorService.submit(Callable)
,而不是使用Runnable
和Thread
:请注意,如果您希望工作人员返回结果,可以将
StressServer
的类型更改为Callable
Keep in mind that if the thread throws any exceptions, the test would not necessarily fail. Instead of using
Runnable
andThread
, you might want to useExecutorService.submit(Callable<T>)
:Note if you want the workers to return the result, you can change the type of
StressServer
toCallable<YourResultType>
无论如何,扩展 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.