java - 如何从 src 文件夹调用测试类并使用 maven 构建

标签 java maven maven-2 maven-3 maven-plugin

我尝试使用 Maven 从 src main 调用测试类,但无法使用 Maven 进行编译,我的项目结构如下所示

enter image description here

我的主类看起来像

包 com.automation.selenium.demo;

import com.automation.selenium.test.demo.TestClass;

public class EntryClass {

    public static void main(String[] args) {
        System.out.println("!!!!!!!!!!!!!!! Calling From Main Class !!!!!!!!!!!!");
        TestClass testNGtest = new TestClass();
        testNGtest.testMethod();
    }
}

我的测试类看起来像

package com.automation.selenium.test.demo;

public class TestClass {

    public void testMethod() {
        System.out.println("!!!!!!!!!!!!!!! Calling From Test Class !!!!!!!!!!!!");
    }
}

我的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>com.ibm.automation.selenium.demo</groupId>
    <artifactId>Demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath/>
                        <argument>${project.build.directory}\test-classes\com\automation\selenium\test\demo</argument>
                    </arguments>
                    <!-- <testSourceRoot>${project.basedir}/src/test{</testSourceRoot> -->
                    <mainClass>com.automation.selenium.demo.EntryClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我使用以下命令来运行

clean compiler:testCompile exec:java

但显示以下错误

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project Demo: Unable to parse configuration of mojo org.codehaus.mojo:exec-maven-plugin:1.6.0:java for parameter arguments: Cannot store value into array: ArrayStoreException -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginConfigurationException

我是 Maven 新手,请帮助我找出问题。

最佳答案

src/main/java 中的类永远不应该调用 src/test/java 中的类。 src/test/java 中的类旨在测试 src/main/java 中的类。它们不应该用作自建测试框架的依赖项或一部分。

如果您想构建一个 jar 进行测试,请将类放入 src/main/java 中。

关于java - 如何从 src 文件夹调用测试类并使用 maven 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897981/

相关文章:

java - ArrayList 未正确填充

java - 有没有办法让 mysql 返回的行可以被 5 整除

maven-site-plugin 未使用 javadocs 生成 apidocs 文件夹

java - 如何使 m2e(eclipse 的 Maven 集成)知道在哪里可以找到依赖项?

spring - 如何从 STS Boot Dashboard 中删除不需要的项目

java - Guice辅助注入(inject)

java - 如何使用 BigDecimal 正确划分

java - 如何在 Linux 上使用 SIGAR 和 maven?

java - Maven 模块由于编译错误无法安装,但会编译成功

c - Maven2 + 原生maven插件