5

I am using Maven with Junit tests. I am using the standard Maven project structure and I can run the "run as Junit test" in Eclipse and they all succeed but if I want to run Maven test / install then the tests are not running, resulting in the error "Could not initialize class main.sushi.persistence.Persistor".

Here is our project-tree:

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── main
│   │   │       └── sushi
│   │   │           └── persistence
│   │   │               ├── DatabaseEnvironments.java
│   │   │               ├── META-INF
│   │   │               │   ├── persistence.xml
│   │   │               │   └── persistence_template.xml
│   │   │               └── Persistor.java
│   │   └── resources
│   └── test
│       ├── java
│       │   ├── META-INF
│       │   │   ├── persistence.xml
│       │   │   └── persistence_template.xml
│       │   └── test
│       │       └── sushi
│       │           └── persistence
│       │               ├── ProcessPersistorTest.java
│       │               └── TestPersistor.java
│       └── resources
└── target
    ├── classes
    │   ├── META-INF
    │   │   ├── MANIFEST.MF
    │   │   └── maven
    │   │       └── sushi
    │   │           └── SushiPersistence
    │   │               ├── pom.properties
    │   │               └── pom.xml
    │   └── main
    │       └── sushi
    │           └── persistence
    │               ├── DatabaseEnvironments.class
    │               ├── META-INF
    │               │   ├── persistence.xml
    │               │   └── persistence_template.xml
    │               └── Persistor.class
    ├── generated-sources
    │   └── annotations
    ├── surefire
    ├── surefire-reports
    │   ├── TEST-test.sushi.persistence.ProcessPersistorTest.xml
    │   ├── TEST-test.sushi.persistence.TestPersistor.xml
    │   ├── test.sushi.persistence.ProcessPersistorTest.txt
    │   └── test.sushi.persistence.TestPersistor.txt
    └── test-classes
        ├── META-INF
        │   ├── persistence.xml
        │   └── persistence_template.xml
        └── test
            └── sushi
                └── persistence
                    ├── ProcessPersistorTest.class
                    └── TestPersistor.class

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sushi</groupId>
    <artifactId>SushiPersistence</artifactId>
    <version>SNAPSHOT</version>
    <repositories>
        <repository>
            <id>EclipseLink</id>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>
    <build>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>de.hpi-web.sushicommon</groupId>
            <artifactId>SushiCommon</artifactId>
            <version>SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.22</version>
        </dependency>
    </dependencies>


</project>
martieva
  • 131
  • 2
  • 11
ThommyH
  • 310
  • 2
  • 6
  • 15
  • 1
    Were you able to solve this issue? I'm having exactly the same problem. All junit tests are running in Eclipse but getting an error when running them with Maven (in command line) – Velth Jun 13 '14 at 05:43
  • I think the main problem was that we referred to classes in other projects in eclipse. Eclipse somewhat allows this. Maven is more strict here. – ThommyH Jun 25 '14 at 11:58

3 Answers3

2

Old question but I'll provide my answer since I had a very similar issue the other day. I was writing a unittest for class Foo in package project.model which had a member of type Bar in package project.cache. When maven were to run the unittests, I got the above error on creating a mock of type Bar.

When running mvn verify -X, I noticed that target/classes was missing from the classpath when running the test.

I solved it by adding the following to the pom.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.0</version>
      <configuration>
        <additionalClasspathElements>
          <additionalClasspathElement>${project.basedir}/target/classes</additionalClasspathElement>
        </additionalClasspathElements>
      </configuration>
    </plugin>

  </plugins>
</build>
Erik
  • 21
  • 4
1

The resources in src/main/sushi/persistence/META-INF have to be located under src/main/resources and the appropriate folder for test must be under src/test/resources instead in src/main/java or src/test/java.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • thanks for the advice but it didnt fixed the classdefnotfounderror :( its kinda strange, that with ecplise i can run the junit tests – ThommyH Dec 20 '12 at 11:50
  • Can you add the complete error message you got instead of writing noClassFound or **"Could not initialize class main.sushi.persistence.Persisto"** etc. ? Apart from that you should follow the conventions in Maven and don't try to go an other way. – khmarbaise Dec 20 '12 at 17:25
0

Remove scope tag from all your dependency.. currently u have set it compile so maven doing what you have asked for it do.. means it doesn't include your dependency during test phase.

Chetan
  • 1,507
  • 7
  • 30
  • 43
  • tried it (set to "test" and tried with "provided") but still the same. acutally, the class he does not find is in the project itself, so its not a dependency problem :( – ThommyH Dec 23 '12 at 13:15
  • Scope "compile" is the default scope. Removing the scope does nothing, according to the documentation: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope – Gijs Overvliet Dec 04 '14 at 11:06