我只是尝试从命令行运行一个简单的 JUnit 测试
mvn clean test -Dtest=src/test/java/scraper/ScrapeTest#dbConnectionTest
结果(为简洁起见缩短)
[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
我不明白,因为依赖关系肯定包含在 pom.xml 文件中。我已删除任何
<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>
这是我正在尝试运行的测试。
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);
}
}
}
最佳答案
您需要将 junit 依赖项添加到您的 pom 文件中,最终进行范围测试,
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
关于java - Maven编译错误: package org. junit不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54952610/