简体   繁体   中英

Maven compilation error: package org.junit does not exist

I'm just trying to run a simple JUnit test from the command line

mvn clean test -Dtest=src/test/java/scraper/ScrapeTest#dbConnectionTest

results in this (shortened for brevity)

[ERROR] COMPILATION ERROR :
[ERROR] /mnt/c/Users/My Name/Workspace/MyProject/MyProject/src/test/java/scraper/ScrapeTest.java:[3,24] package org.junit does not exist
[ERROR] /mnt/c/Users/My Name/Workspace/MyProject/MyProject/src/test/java/scraper/ScrapeTest.java:[20,10] cannot find symbol
  symbol:   class Test
  location: class scraper.ScrapeTest

I don't understand, as the dependency is definitely included in the pom.xml file. I have removed any < scope > tags, and set the source and target as suggested in other similar questions, and also set the sourceEncoding to UTF-8 because I was getting warnings about that and thought perhaps the '@' symbol wasn't being recognized properly, but honestly, I have no idea what's wrong; just throwing {{poop emoji}} at the wall to see what sticks.

<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>myproj</groupId>
  <artifactId>web-scraper</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MyProject</name>
  <description>A web scraper to collect data for MyProject database</description>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
        <!-- jsoup HTML parser library @ https://jsoup.org/ -->
        <dependency>
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>1.11.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
           <groupId>com.microsoft.sqlserver</groupId>
           <artifactId>mssql-jdbc</artifactId>
            <version>7.2.1.jre8</version>
        </dependency>
    </dependencies>
</project>

This is the test I'm trying to run.

package scraper;

import static org.junit.Assert.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.junit.Test;

public class ScrapeTest {

    //TODO fix connection issues
    private final String connectionUrl = "nunyabizniz";

    @Test
    public void dbConnectionTest() {
        try {
            //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver.class");
            Connection connection = DriverManager.getConnection(connectionUrl);
            assert(true);
        }
        // Handle any errors that may have occurred.
        catch (Exception e) {
            System.out.println(e.getMessage());
            assert(false);
        }
    }
}

You need to add the junit dependency to your pom file, eventually with scope test,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM