从Java程序调用Junit:java.lang.Exception:没有可运行的方法

时间:2014-12-09 07:08:59

标签: java junit jboss

请在下面找到JBOSS-EAP-6.2中可部署的结构:

mytest.ear
|
|
|---- mytestjar.jar (contains the test-suites which need to be called from the .WAR below)
|
|---- mytestwar.war (extracted folder)
|       |---- META-INF
|       |---- WEB-INF
|       |       |---- classes (MyTestCaller.class - Code to call the test-suites from above .jar)
|       |       |---- lib
|       |       |---- web.xml
|
|---- META-INF
|
|---- lib (Third-party jars)
|       |---- xyz.jar
|       |---- abc.jar

我通过以下方式通过Java程序从mytestwar.war调用Junit在mytestjar.jar中的内容:

public class MyTestCaller 
{
    private static final String JUNIT_TASK_SUMMARY_TYPE = "withOutAndErr";
    private static final String FORMATTER_TYPE = "xml";
    private static final String TEST_SUITE_PKG_NAME = "com.try.test.suites.";

    public void callTests(TestControllerDTO testControllerDto)
    {
        String testSuiteNamesFromDto = testControllerDto.getRequestParams().getTestSuiteNames();
        String reportsToDirFromDto = testControllerDto.getRequestParams().getReportsToDir();

        JUnitTask.SummaryAttribute summary = new JUnitTask.SummaryAttribute();
        summary.setValue(JUNIT_TASK_SUMMARY_TYPE);

        FormatterElement formater = new FormatterElement();
        FormatterElement.TypeAttribute type = new FormatterElement.TypeAttribute();
        type.setValue(FORMATTER_TYPE);
        formater.setType(type);

        reportsToDirFromDto = reportsToDirFromDto + File.separator + "test-reports";
        File outputDir = new File(reportsToDirFromDto);
        if (outputDir.exists())
        {
            LOGGER.info("Deleting old reports from : " + outputDir.getAbsolutePath());
            deleteDirectory(outputDir);
        }
        LOGGER.info("Creating directory for generating reports: " + outputDir.getAbsolutePath());
        outputDir.mkdirs();

        Project project = new Project();
        project.setCoreLoader(Thread.currentThread().getContextClassLoader());
        JUnitTask task = new JUnitTask();
        task.setHaltonerror(true);
        task.setHaltonfailure(true);
        task.setProject(project);
        task.setFork(false);
        task.setPrintsummary(summary);
        task.addFormatter(formater);

        String[] suiteNamesWithoutPkg = testSuiteNamesFromDto.trim().split(COMMA_STRING);
        for (String testSuiteName : suiteNamesWithoutPkg)
        {
            // Creating fully qualified name for test suite.
            StringBuilder suiteNameWithPkg = new StringBuilder();
            suiteNameWithPkg.append(TEST_SUITE_PKG_NAME);
            suiteNameWithPkg.append(testSuiteName.trim());
            LOGGER.info("Adding test-suite: " + testSuiteName + " to JUNIT task.");

            // Adding individual test to JUNIT ANT task.
            JUnitTest test = new JUnitTest();
            test.setName(suiteNameWithPkg.toString());
            test.setTodir(outputDir);
            test.setHaltonerror(true);
            test.setHaltonfailure(true);
            task.addTest(test);
        }
        LOGGER.info("Executing the added test-suites: " + testSuiteNamesFromDto);
        task.execute();
    }
}

另外,PFB是测试套件& mytestjar.jar中存在的测试用例:

MyTestSuite.java:

@RunWith(Categories.class)
@SuiteClasses(
{ MyTestCase.class })
public class MyTestSuite
{

}

MyTestCase.java:

public class MyTestCase
{
    @Test
    public void myTestcaseMethod()
    {
        System.out.println("Test-case called.");
    }
}

问题是,当我从JBOSS应用程序调用MyTestCaller.callTests()方法时,我收到以下错误,PFB生成的测试报告:

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="1" failures="0" hostname="acme-THINK" name="com.try.test.suites.MyTestSuite" skipped="0" tests="1" time="0.01" timestamp="2014-12-08T12:52:56">
  <properties />
  <testcase classname="com.try.test.suites.MyTestSuite" name="initializationError" time="0.01">
    <error message="No runnable methods" type="java.lang.Exception">java.lang.Exception: No runnable methods
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.try.server.MyTestCaller.callTests(Unknown Source)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149)
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97)
    at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:389)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926)
    at java.lang.Thread.run(Thread.java:745)
</error>
  </testcase>
  <system-out><![CDATA[]]></system-out>
  <system-err><![CDATA[]]></system-err>
</testsuite>

请帮我解决此错误。

0 个答案:

没有答案