0

In keeping with the m2e bug/issue, I followed the solution mentioned @SL4j m2e fails to load in Eclipse IDE.

My Pom.xml looks like this (for my JUnit and Selenium dependencies):

<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>TestJUnit</groupId>
  <artifactId>TestJUnit</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <type>jar</type>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>2.41.0</version>
  <type>jar</type>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.10-FINAL</version>
</dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.41.0</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

However when I do a mvn compile I get the following kind of error:

[ERROR] /path:/to/file/<foldername>/<JUnitClassName>.java:[9,17] package org.junit does
 not exist

I am new to maven, is there something that I am doing wrong here.

Community
  • 1
  • 1
Segmented
  • 2,024
  • 2
  • 23
  • 44
  • can you run the command with debbuger turned on in eclipse. looks like the junit jar is not downloaded properly or junit versions are crashing in m2.you can google how to trun on the debug option in eclipse – vikeng21 May 23 '14 at 13:43
  • Do you use JUnit in a class located in the "main" package? http://stackoverflow.com/questions/12403497/maven-erro-package-org-junit-does-not-exist – jhamon May 23 '14 at 13:46
  • @Trichoko Thanks for pointing out. The problem was due to the fact that I used test. As I fixed this according to the solution provided below, I could resolve the issue. – Segmented May 23 '14 at 13:57

1 Answers1

2

In your <dependencies> list, you specified JUnit with the scope of test, which means that it is only included in the jar when you do mvn test, but not included in mvn compile. If you want JUnit to be included in your final jar, change the <scope>test</scope> to <scope>compile</scope> or you can jsut remove the tine, they both do the same.

TL;DR use mvn test to test your application; or change <scope>test</scope> to <scope>compile</scope> in the junit dependeny decleration.

I did the same thing when I first started using maven, almost everyone does :P

DirkyJerky
  • 1,130
  • 5
  • 10