简体   繁体   English

Maven编译错误:包org.junit不存在

[英]Maven compilation error: package org.junit does not exist

I'm just trying to run a simple JUnit test from the command line 我只是想从命令行运行一个简单的JUnit测试

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. 我不明白,因为依赖项肯定包含在pom.xml文件中。 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; 我删除了所有<scope>标记,并按照其他类似问题中的建议设置了源和目标,并且还将sourceEncoding设置为UTF-8,因为我对此有所警告,并认为可能不是'@'符号正确识别,但说实话,我不知道出了什么问题; just throwing {{poop emoji}} at the wall to see what sticks. 只需将{{poop emoji}}扔在墙上,看看有什么木棍。

<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, 您需要将junit依赖项添加到pom文件中,最终通过范围测试,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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