NoSuchMethodException:main在Spring应用程序上运行TestNG测试时

时间:2015-10-15 07:50:11

标签: java spring intellij-idea testng functional-testing

我正在尝试使用TestNG编写一个调用REST API的测试。我尝试使用IntelliJ IDEA运行它,我得到以下错误:

  

线程中的异常" main" java.lang.NoSuchMethodException:   test.api.testHospitals.main([Ljava.lang.String;)at   java.lang.Class.getMethod(Class.java:1670)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)

这是我的测试课程。它有什么问题?为什么Java需要main方法?我完全迷失了。

package test.api;

import com.example.backend.entity.postgres.User;
import com.example.backend.utils.JsonUtils;
import com.example.mainserver.service.rest.CallBackLocalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import test.all.LoginFabric;
import test.all.PatientPersonFabric;

@ContextConfiguration(locations = {"classpath:spring-test-config.xml"})
public class testHospitals extends AbstractTestNGSpringContextTests {

    @Autowired
    private CallBackLocalService callBackLocalService;

    public static final String LOGIN = "user@example.com";
    public static final String PASS = "aEd2pbD5ae3lci";

    @Test(enabled = true)
    public void testAddPatient() {
        LoginFabric loginFabric = new LoginFabric();
        User user = loginFabric.loginUser(LOGIN, PASS, callBackLocalService);
        PatientPersonFabric.PatientPerson p = PatientPersonFabric.generatePatientPerson();
        String patientJson = JsonUtils.getJson(p, true);
        System.out.println(patientJson);
    }
}

最小配置在spring-test-config.xml

中设置
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
">
    <context:component-scan base-package="com.example.mainserver"/>

    <bean id="callBackLocalService" name="callBackLocalService"
          class="com.example.mainserver.service.rest.CallBackLocalService"/>

</beans>

我还有一个警告/错误标记&#34;未找到主要方法&#34;在IDEA的运行配置中:

Main method not found, IntelliJ IDEA

更新解决方案

正如M. Deinum所解释的那样,问题是由于IntelliJ中禁用了TestNG-J插件。我启用它并重新启动IDEA,然后我还必须删除它的测试运行配置。最后,我用Shift-F10重新运行测试,结果没问题。

0 个答案:

没有答案