mvn 测试不适用于 maven 3.3.9 和 java 1.8.0_45

mvn test not working with maven 3.3.9 and java 1.8.0_45

我是 运行 mvn clean test
maven 3.3.9
java 1.8.0_45
testng 6.8.5
和我的 spring 版本是 4.0.4.RELEASE
但出现以下错误。

[ERROR] springTestContextPrepareTestInstance(com.inn.app17apr02.db.TestAuditDb) Time elapsed: 1.205 s <<< FAILURE! java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [/home/ist/apache-tomcat-7.0.59/webapps/REPO/app17apr02/app17apr02/1.0/app17apr02/target/classes/applicationContext/application.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index; Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

我搜索了很多,尝试了不同的组合和排列,但仍然没有成功。 以下是我在 pom

中的插件条目
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <properties>
            <property>
                <name>usedefaultlisteners</name>
                <value>false</value>
            </property>
            <property>
                <name>listener</name>
                <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
            </property>
            <property>
                <name>testname</name>
                <value>${project.name} TestNG tests reports</value>
            </property>
        </properties>
        <systemProperties>
            <property>
                <name>org.uncommons.reportng.title</name>
                <value>${project.name} TestNG Report</value>
            </property>
            <property>
                <name>org.uncommons.reportng.stylesheet</name>
                <value>/home/ist/apache-tomcat-7.0.59/webapps/REPO/app17apr02/app17apr02/1.0/app17apr02/headstart/artifacts/custom.css</value>
            </property>
            <property>
                <name>org.uncommons.reportng.coverage-report</name>
                <value>https://www.google.co.in</value>
            </property>
        </systemProperties>
        <workingDirectory>target/</workingDirectory>
        <reportsDirectory>/home/ist/apache-tomcat-7.0.59/webapps/REPO/app17apr02/app17apr02/1.0/app17apr02/headstart/sartifacts/surefire-reports</reportsDirectory>
        <classpathDependencyExcludes>
            <classpathDependencyExclude>org.apache.tika:tika-app</classpathDependencyExclude>
        </classpathDependencyExcludes>
        <forkCount>3</forkCount>
        <reuseForks>true</reuseForks>
        <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
    </dependencies>
</plugin>

如果需要更多信息,请告诉我。
我尝试过使用不同的 JAP 2.x jar,因为很多关于 SO 的答案都说这是 JPA jar 的问题,并且还尝试了不同的 surefire 版本,例如 2.18.x、2.19.x。
任何帮助将不胜感激。

Table#indexes()方法是在2.1版本中加入的,所以你使用的jar是正确的。 可能在 classpath 中添加了一些其他 jar,它也有 @Table 注释,例如 persistence-api-1.0.jar 或其他一些 jar。检查您的 class 路径和 remove/exclude 这些罐子。

您可能需要 hibernate-entitymanager 依赖项

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
        <scope>runtime</scope>
    </dependency>