我已经编写了一些 JUnit 测试,并且根据我们老板的决定,这些测试位于一些笔记本电脑上,并在需要时由某人右键单击(在 IntelliJ 中)测试类并“运行”来手动运行。
现在,终于有人听我的,在构建系统中选择了自动运行的测试......但是,通过这种方式我们发现测试甚至不能从命令行编译!
在同一台机器上,只安装了一个 JDK - Oracle JDK-8(我猜是 IntelliJ 自带的 JBR),IntelliJ 在编辑代码时报告没有问题,在编译和运行代码时也没有问题......但是当我使用 IntelliJ 的终端: mvn test -Pmytests - 它提示一些丢失的包(并停止构建):[ERROR] /C:/.../soaptest/customhttp/HttpSoapConnection.java:[3,43] package com.sun.xml.internal.messaging.saaj does not exist
我想知道 IntelliJ 知道什么,Maven 不知道什么?我尽量保持 Maven 和 IntelliJ 之间的配置相同。所以,我们没有任何像这样的疯狂,即使没有 IDE,事情也能工作......实际上,我的项目非常简单......很确定 IntelliJ 中的所有内容都只是默认值。
很抱歉,我不知道还有什么要与您分享,我不知道有什么不同。如果有人建议检查的东西,我会更新! :)
-cp mymodule
- 这是我需要在 pom.xml 文件中配置的内容吗? 更新:由于我无法编译的类相互链接,因此我无法在可重现的示例中仅显示“一个”类。相反,我大大减少了项目并上传到这里:https://github.com/DraxDomax/intellijmaven
重现(需要 JDK8):
1] 在 IntelliJ 中打开并右键单击
src/test/java/com/nominet/qa/proto/SoapProtoTest.java
然后“跑”= 这将运行一个将失败的测试 - 但请注意该方法被执行,因为它已编译...
2]删除目标文件夹
3]在命令行中(我使用了IDEA终端),输入:mvn test -Psurefiredemo
= 注意现在测试甚至没有运行,你会得到编译错误!
最佳答案
我使用 从 IntelliJ 和命令行执行单元测试mvn 测试 .
我的 IntelliJ 项目:
我的 Java 项目有这个类:
package com.example;
public class Test1 {
public String foo() {
return "1";
}
public String foo2() {
return "2";
}
public String foo3() {
return "3";
}
}
我的测试课是这样的:import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.example.*;
import java.io.*;
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestClass {
public static Test1 testOb;
@BeforeAll
public static void setUp() throws IOException {
testOb = new Test1();
}
@Test
@Order(1)
public void whenInitializingAWSService_thenNotNull() {
assertNotNull(testOb);
System.out.println("Running SNS Test 1");
}
@Test
@Order(2)
public void callFoo() {
testOb.foo();
System.out.println("Test 2 passed");
}
@Test
@Order(3)
public void callFoo2() {
testOb.foo2();
System.out.println("Test 3 passed");
}
@Test
@Order(4)
public void callFoo3() {
testOb.foo3();
System.out.println("Test 4 passed");
}
}
我的 POM 是这样的。确保你也有这个插件:Surefire Plugin
<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>org.example</groupId>
<artifactId>TestProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
我的 IntelliJ 测试成功:我的测试从命令行使用 mvn test 成功。
更新....
我从 Github 下载了你的项目。它在命令行中不起作用。我将您的 POM 更新为:
<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany</groupId>
<artifactId>qatoolbox</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>qatoolbox</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
我更改了测试类中的逻辑,因为这样做的重点不是实际测试您的代码,而是确保从命令行执行测试。在我更新 POM 之后 - 测试通过命令行成功执行。关于Java:JUnit 测试在 IntelliJ 中运行,但不在 Maven 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67677271/