我有一天要使用Junit尝试与hibernate进行集成测试。我对集成测试很新,所以我希望我做错了。 我在githhub上找到了这个例子: example hibernate junit test 我已经下载并尝试了,它可以工作,所以我尝试在我的pom上复制依赖项和类(因为我的目标是将来使用我的内部数据库并测试我已编写的函数),并复制这个课程:
package org.hibernate.tutorial.annotations;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class AnnotationsIllustrationTest extends TestCase {
private SessionFactory sessionFactory;
@Override
protected void setUp() throws Exception {
// A SessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
}
@Override
protected void tearDown() throws Exception {
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
@SuppressWarnings({ "unchecked" })
public void testBasicUsage() {
// create a couple of events...
Session session = sessionFactory.openSession();
session.beginTransaction();
session.close();
// now lets pull events from the database and list them
session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery( "from Event" ).list();
// for ( Event event : (List<Event>) result ) {
// System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
// }
session.getTransaction().commit();
session.close();
}
但是当我尝试&#34; Junit测试&#34;或者&#34; Maven测试&#34;我有这个错误: java.lang.NoSuchMethodError:org.hibernate.internal.CoreMessageLogger.debugf(Ljava / lang / String; I)V
我已经看到问题出在依赖关系上了,所以我试图改变我的dependecies的版本,只使用一些依赖关系测试但是doensn没有改变任何东西....这里我的pom:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<!-- JBoss dependency versions -->
<version.jboss.maven.plugin>7.1.1.Final</version.jboss.maven.plugin>
<bcpkix-jdk15on.version>1.48</bcpkix-jdk15on.version>
<cxf-bundle.version>2.5.2</cxf-bundle.version>
<javax.ejb-api.version>3.2</javax.ejb-api.version>
<commons-codec.version>1.6</commons-codec.version>
<commons-configuration2.version>2.0</commons-configuration2.version>
<commons-io.version>2.4</commons-io.version>
<commons-lang.version>2.6</commons-lang.version>
<commons-logging.version>1.2</commons-logging.version>
<commons-logging-api.version>1.1</commons-logging-api.version>
<commons-net.version>3.4</commons-net.version>
<commons-collections.version>3.2.2</commons-collections.version>
<commons-fileupload.version>1.2</commons-fileupload.version>
<fluent-hc.version>4.3.6</fluent-hc.version>
<httpclient-osgi.version>4.2.1</httpclient-osgi.version>
<httpclient-cache.version>4.3.6</httpclient-cache.version>
<httpcore.version>4.3.3</httpcore.version>
<httpmime.version>4.3.6</httpmime.version>
<log4j.version>1.2.17</log4j.version>
<mysql-connector-java>5.1.31</mysql-connector-java>
<!-- Maven Plugin -->
<maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.0.0</maven-source-plugin.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<jdk.source>1.7</jdk.source>
<jdk.target>1.7</jdk.target>
</properties>
<dependencies>
<!-- Not provided: -->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>${commons-configuration2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<!-- Commons Logging -->
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging-api -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>${commons-logging-api.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/com.springsource.javax.persistence -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version> <!-- In questo modo prende la versione da sopra -->
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>${httpclient-cache.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpmime.version}</version>
</dependency>
<!-- Da qui in poi sono tutti Provided: -->
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-web</artifactId>
<version>${version.jboss.maven.plugin}</version>
<scope>provided</scope>
</dependency>
<!-- Per Base32 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>${httpclient-osgi.version}</version>
<scope>provided</scope>
</dependency>
<!-- Database -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>${javax.ejb-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>${bcpkix-jdk15on.version}</version>
<!-- <scope>provided</scope> -->
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
<version>${cxf-bundle.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>${fluent-hc.version}</version>
<scope>provided</scope>
</dependency>
<!-- Prova Junit -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<!-- The H2 in-memory database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.2.145</version>
</dependency>
<!-- to use Hibernate as the JPA provider at a guess! -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.2.Final</version>
</dependency>
</dependencies>
<!-- build -->
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.maven.plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${jdk.source}</source>
<target>${jdk.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
提供了一些依赖项,因为我使用jboss来部署我的项目;我还复制了src / test / resources中的persistence.xml和hibernate.cfg.xml。
我做错了什么?做我想做的最好的做法是什么?
答案 0 :(得分:1)
正如我提出的那样,我遇到了类似的问题,在我的情况下,我解决了检查maven依赖树的问题......我在提示中使用了这个命令:
mvn依赖:tree -Dverbose 并且阅读响应我发现了一个库已经宣布了两次,但它不是一个Hibernate的库,只有在我从hibernate4移植到hibernate5之后才会引起这个问题。
我希望任何人都有这个问题是有用的。