Reputation: 681
It's been a day that I am tring to use Junit to try integration test with hibernate. I am very new to integration test, so I wish that I am doing something wrong. I have found on githhub this example: example hibernate junit test I have download and try it, and It works, so I have try to copy the dependencies and class on my pom(because my goals is to use, in the future, my internal db and test my functions that I have already write), and copy this class:
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();
}
but when I try "Junit test" or "Maven test" I have this error: java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V
I have see that the problem is on the dependency,so I have tried to change version of my dependecies, use some dependencies only with test but It doensn't change anything.... here my 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>
Some dependencies are provided because I use jboss to deploy my project; I have also copy the persistence.xml and hibernate.cfg.xml in src/test/resources.
What I am doing wrong ? There is a best practise to do what I want to do?
Upvotes: 3
Views: 3540
Reputation: 2735
As I xommented, I had a similar problem and, in my case, I resolved checking maven dependencies tree ... I use this command in my prompt:
mvn dependency:tree -Dverbose and reading the response I found a library declared two time but It wasn't an Hibernate's library and it caused this problem only after my porting from hibernate4 to hibernate5.
I hope that it is useful to anyone will have this problem.
Upvotes: 1