Eclipse+Junit Test Suite Error --java.lang.Exception: No runnable methods
上一篇 /
下一篇 2010-01-15 09:30:12
/ 个人分类:Selenium
使用Eclipse+(Junit4.x+Junit3.x 2个单位
测试插件)做回归测试, 单个执行TESTCASE的时候都是可以成功的,但是将TESTCASE放在TESTSUITE中执行时,提示
java.lang.Exception: No runnable methods at org.junit.internal.runners.TestClassMethodsRunner.testAborted(TestClassMethodsRunner.java:42) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:33) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
|
以下是测试套件代码
package Testsuite;
import junit.framework.Test; import junit.framework.TestSuite;
public class AllTests {
public static Test suite() { TestSuite suite = new TestSuite("Test for Testsuite"); //$JUnit-BEGIN$ suite.addTestSuite(GoogleTest2.class); suite.addTestSuite(GoogleTest1.class); suite.addTestSuite(GoogleTest.class); //$JUnit-END$ return suite; }
} |
在网上查了一下,原因是因为我使用了Junit4.x进行
单元测试,if you use the JUnit 4.4 core runner to execute
a class, you must have "@Test" method in the class.
有两种方法可以解决这个问题:
(1)比较简单的方法: 使用Junit3.x, 右击TESTSUITE CLASS,选择RunAs-Run,在弹出的窗口中,Testrunner选择为Junit3,默认是Junit4.(本人才疏学浅,实在不是太懂JUNIT里面的道道,就采用了第一种方法)
(2)修改TESTSUITE和TESTCASE测试代码,使其包含 the @Test annotation,修改后的代码
importorg.junit.runner.RunWith; importorg.junit.runners.Suite; importorg.junit.runners.Suite.SuiteClasses;
@RunWith(value=Suite.class) @SuiteClasses(value={TestCase.class}) publicclassAllTests{
}
|
importstaticorg.junit.Assert.assertTrue; importorg.junit.Test;
publicclassTestCase { @Test publicvoidtest1{ assertTrue(tmp.getTermin().equals(soll));(//注意以处要换成自己的测试代码,这只是一个例子) } }
|
在以下这个网站上还找到
其它可能引起No runnable method的原因:
http://www.dantoomeysoftware.com/pencils-down/2009/04/03/junit-error-javalangexception-no-runnable-methods/
This error is a catchall for anything else that might be wrong with
your test class. First make sure any of the below problems are not
occurring. Your unit test code is running JUnit4. At least one test method must have the @Test annotation. Otherwise this error occurs. If the class you are testing has @Required setters for Spring
injections they must be set otherwise JUnit will mask the missing
required exception as a no runnable methods exception. If you are initializing your mock objects outside of the @Begin method you will get this error. If you are attempting to mock the same class more than once you will get this error.
|
收藏
举报
TAG: