繁体   English   中英

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

[英]Java: JUnit tests run in IntelliJ but not in Maven

我已经编写了一些 JUnit 测试,根据我们老板的决定,这些测试位于某些笔记本电脑上,并在需要时手动运行,通过某人右键单击(在 IntelliJ 中)测试 class 并“运行”。

现在,终于有人听了我的意见,并在构建系统中选择了自动运行的测试......但是,这样我们发现测试甚至不能从命令行编译!

在同一台机器上,只安装了一个 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 在右键单击运行 class 时创建的结果“运行配置”,我看到它确实-cp mymodule - 这是我需要在 pom.xml 文件中配置的东西?

更新:由于我无法编译的类是相互链接的,所以我无法在我的可重现示例中只显示“一个”class。 相反,我已经大大缩减了项目并在此处上传: 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 项目:

在此处输入图像描述

我的 Java 项目有这个 class:

package com.example;

public class Test1 {

    public String foo() {

        return "1";
    }

    public String foo2() {

        return "2";
    }

    public String foo3() {

        return "3";
    }
}

我的测试 class 是这样的:

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 插件

<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>

我更改了测试 class 中的逻辑,因为这样做的目的不是实际测试您的代码,而是确保从命令行执行测试。 在我更新 POM 之后 - 测试通过命令行成功执行。 在此处输入图像描述

暂无
暂无

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

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