我只是想从命令行运行一个简单的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);
}
}
}
答案 0 :(得分:3)
您最终需要通过范围测试将junit依赖项添加到pom文件中,
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>