Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit测试用例未通过maven运行_Java_Maven_Junit - Fatal编程技术网

Java JUnit测试用例未通过maven运行

Java JUnit测试用例未通过maven运行,java,maven,junit,Java,Maven,Junit,以前也有人问过类似的问题,但我无法解决这个问题。 我想通过Maven自动化JUnit测试用例的执行 JUnit jar version - 4.8.2 Maven version - 3.2.5 Surefire jar version - 2.14.1 当我运行mvn编译测试时,出现了以下异常: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14.1:test (defaul

以前也有人问过类似的问题,但我无法解决这个问题。 我想通过Maven自动化JUnit测试用例的执行

JUnit jar version - 4.8.2

Maven version - 3.2.5

Surefire jar version - 2.14.1
当我运行mvn编译测试时,出现了以下异常:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14.1:test (default-test) on project my_project: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14.1:test (default-test) on project my_project: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)
    at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution(SurefireHelper.java:55)
    at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary(SurefirePlugin.java:169)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:733)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:631)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 19 more
这些条目位于pom.xml中:

<dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>maven-surefire-common</artifactId>
            <version>2.14.1</version>
            <scope>compile</scope>
</dependency>

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <type>jar</type>
            <scope>compile</scope>

</dependency>

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
            </configuration>

</plugin>

org.apache.maven.surefire
maven surefire common
2.14.1
编译
朱尼特
朱尼特
4.8.2
罐子
编译
maven编译器插件
1.7
1.7

我已经尝试了所有的东西从互联网上,但没有工作。我遗漏了什么吗?

默认情况下,您的测试必须在
src/test/java

此外,还有一些名称约定。报告提到:

默认情况下,Surefire插件将自动包含具有以下通配符模式的所有测试类:

  • “**/Test*.java”-包括其所有子目录和以“Test”开头的所有java文件名
  • “***Test.java”-包括其所有子目录和以“Test”结尾的所有java文件名
  • “***TestCase.java”-包括其所有子目录和以“TestCase”结尾的所有java文件名
这是一个您应该拥有的示例结构

project
+-src
  +-main
  | +-java
  |   + tmp
  |     + App.java
  +-test
  | +-java
  |   +-tmp
  |     + AppTest.java
  +-pom.xml
您的
AppTest.java
应该至少有一个
@org.junit.Test
-注释的方法

package tmp;
public class AppTest {
  @org.junit.Test
  public void testSomeMethod() {
    System.out.println("Tests have been found");
  }
}
您的pom.xml应该与此类似

<?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>stackoverflow</groupId>
    <artifactId>tmp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4.0.0
栈溢出
tmp
0.0.1-快照
罐子
UTF-8
朱尼特
朱尼特
4.12
测试
maven编译器插件
3.3
1.7
1.7

我刚刚测试了它,效果非常好。

您没有任何单元测试,或者它们位于错误的位置。两个想法。。1) 首先尝试删除所有关于surefire的提及,并发布“mvn测试”。2) 与其将surefire配置为依赖项,不如将其配置为构建插件,如Olivier所示。3) 应该能够在junit依赖项上使用测试范围,而不是编译。两种方法都尝试过。没有一个奏效。仍然得到相同的异常。然后请编辑您的问题并在下面添加测试的结构和名称,以及您如何在Maven中定义junit依赖项。