Java:JUnit 测试在 IntelliJ 中运行,但不在 Maven 中运行

问题描述

我已经编写了一些 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 中的所有内容都只是认值。

很抱歉,我不知道还有什么要与您分享,我不知道有什么不同。如果有人建议检查的东西,我会更新! :)

  • 哦,我有一个想法:我查看了 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
= 请注意,现在测试甚至没有运行,您会收到编译错误

解决方法

我使用 mvn test 从 IntelliJ 和命令行执行单元测试。

我的 IntelliJ 项目:

enter image description here

我的 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 测试成功:

enter image description here

我使用 mvn test 从命令行成功测试。

enter image description here

更新....

我从 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 之后 - 测试通过命令行成功执行。 enter image description here