简体   繁体   中英

Java: JUnit tests run in IntelliJ but not in Maven

I've written some JUnit tests and, according to how our our boss decided, these tests sit on some laptop and being run manually when needed, by someone right-clicking (in IntelliJ) the test class and "run".

Now, someone finally listened to me and picked up the tests for running automatically, in the build system... However, this way we've found out that the tests don't even compile from command line!

On the same machine, which has only one JDK installed - Oracle JDK-8 (and I guess the JBR that comes with IntelliJ), IntelliJ reports no problems when editing the code, nor when compiling and running the code successfully... But when I use IntelliJ's terminal: mvn test -Pmytests - it complains about some missing packages (and stops the build):

[ERROR] /C:/.../soaptest/customhttp/HttpSoapConnection.java:[3,43] package com.sun.xml.internal.messaging.saaj does not exist

I want to know what is it that IntelliJ knows, that Maven doesn't? I try to keep the configuration as same as possible between Maven and IntelliJ. So, that we don't have any craziness like this and things work even without the IDE... Actually, my project is super simple... Pretty sure everything in IntelliJ is just the defaults.

I am sorry I don't know exactly what more to share with you, I have no idea what could be the difference. If someone suggests things to check, I will update: :)

  • Oh, one idea that I have: I looked into the resulting "run configuration" that IntelliJ creates when rightclick-run-ing the class and I see it does -cp mymodule - is that something I need to configure in the pom.xml file?

Update: Since the classes I am having trouble to compile are linked to each other, I've been unable to show just "one" class in my reproducible example. Instead, I've hugely cut down the project and uploaded here: https://github.com/DraxDomax/intellijmaven

To reproduce (requires JDK8):
1] Open in IntelliJ and right-click on the src/test/java/com/nominet/qa/proto/SoapProtoTest.java and then "run"
= This will run a test that will fail - but notice that the method gets executed because it compiled...

2] Delete the target folder
3] In the command line (I used the IDEA Terminal), type: mvn test -Psurefiredemo
= Notice now the test doesn't even get run and you get compilation errors!

I execute unit tests from both IntelliJ and the command line using mvn test .

My IntelliJ project:

在此处输入图像描述

My Java project has this class:

package com.example;

public class Test1 {

    public String foo() {

        return "1";
    }

    public String foo2() {

        return "2";
    }

    public String foo3() {

        return "3";
    }
}

My Test class is this:

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");
    }
}

My POM is this. Make sure you have this plug-in too:

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>

My test was successful from IntelliJ:

在此处输入图像描述

My tests were successful from the command line using mvn test.

在此处输入图像描述

UPDATE....

I downloaded your project from Github. It did not work from the command line. I updated your POM to this:

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

I changed the logic in the test class as the point of this was not to actually test your code but to ensure the tests are executed from the command line. After I updated the POM - the tests were successfully executed via the command line. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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