0
votes

I am new to maven. I was using IntelliJ and want to create a maven quickstart project.

My pom is

<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>com.travisprogramming.spring.test</groupId>
    <artifactId>testprog</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>testprog</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

The java file is under src, and I got errors like this:

Error:(3, 23) java: package junit.framework does not exist
Error:(4, 23) java: package junit.framework does not exist
Error:(5, 23) java: package junit.framework does not exist
Error:(11, 13) java: cannot find symbol
symbol: class TestCase
Error:(26, 19) java: cannot find symbol
symbol: class Test
location: class com.travisprogramming.spring.test.AppTest

I tried deleting the <scope>test</scope> it didn't work.

I tried creating a new java file under the test, and run that file, it got same errors.

root

3
right click on pom.xml, Maven -> Reimport - buræquete
Are you trying to run the @test class from the package src>main>java? Your @test classes should be under src>test>java and not in the src>main>java. - VPK
1. I suppose you imported the Maven project into IntelliJ properly, and do you see your declared junit dependency in External Libraries of IntelliJ's project view? 2. Does it works for maven command line build? 3. Are you really sure you want to use JUnit 3 given that JUnit 4 and even 5 has been around for quite a long time? - Adrian Shum
Use JUnit 4 which means JUnit 4.12 etc. - khmarbaise
I tried reimport maven and it works. Thanks! - travis

3 Answers

0
votes

The possible cause for this problem could be problems while importing your project in IntelliJ. Try re-importing the pom. Also ensure that you have appropriate directory structure for Test and Application source files.

  • Application Source should be in - src > main > java
  • Test source should be in - src > test > java

If that still doesn't work, try compiling project through command line using mvn compile and check if there are any problems while downloading artifacts.

0
votes
  1. Make sure you installed the Maven Integration plugin.
  2. Reimport the Project again and try to make default for enabling auto import which is helpful.
0
votes

you are declared it as <scope>test</scope>

<dependency>
       <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
</dependency>

So junit will be available src/test/java so do not use the scope if you not make sure your codes are not in src/test/java packages

if it is not works then delete all your files in ~/.m2/repository and run mvn clean -X install you will get detailed logs