Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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 从maven运行ant junit目标_Java_Maven_Ant_Junit_Pom.xml - Fatal编程技术网

Java 从maven运行ant junit目标

Java 从maven运行ant junit目标,java,maven,ant,junit,pom.xml,Java,Maven,Ant,Junit,Pom.xml,我们首先在maven中包装ant构建,开始向maven迁移 我们可以让maven构建项目,但不能运行测试。我们正试图从maven执行build.xml中的ant测试目标。当然,当我们从ant运行测试目标时,一切都很好 下面是一个测试类显示的错误(其他测试类也是如此)。这个类中有几个测试方法,但都没有运行。这似乎是JUnit版本的问题;这些测试是JUnit4测试,如果我们将测试类更改为扩展TestCase(JUnit3样式),它将查找测试 No tests found in org.mathfor

我们首先在maven中包装ant构建,开始向maven迁移

我们可以让maven构建项目,但不能运行测试。我们正试图从maven执行build.xml中的ant测试目标。当然,当我们从ant运行测试目标时,一切都很好

下面是一个测试类显示的错误(其他测试类也是如此)。这个类中有几个测试方法,但都没有运行。这似乎是JUnit版本的问题;这些测试是JUnit4测试,如果我们将测试类更改为扩展TestCase(JUnit3样式),它将查找测试

No tests found in org.mathforum.common.dao.ExampleBaseObjectDaoTest

junit.framework.AssertionFailedError: No tests found in org.mathforum.common.dao.ExampleBaseObjectDaoTest   at 
org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:327)  at 
org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)  at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)  at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)  at 
org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)  at 
org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)    at 
org.apache.maven.cli.MavenCli.main(MavenCli.java:362)   at 
org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60) at 
org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) at 
org.codehaus.classworlds.Launcher.launch(Launcher.java:255) at     
org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)   at     
org.codehaus.classworlds.Launcher.main(Launcher.java:375)
以下是我们的pom:

<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>mf-common</artifactId>
<groupId>org.mathforum</groupId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <version>1.5.0</version>
                    <scope>system</scope>
                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId> <!-- apectj ant plugin -->
                    <version>1.6.1</version>
                </dependency>
                <dependency>
                    <groupId>ant</groupId>
                    <artifactId>ant-junit</artifactId>
                    <version>1.6.5</version>
                    <!--scope>test</scope -->
                </dependency>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.8.2</version>
                    <!--scope>test</scope -->
                </dependency>
                <dependency>
                    <groupId>commons-net</groupId>
                    <artifactId>commons-net</artifactId>
                    <version>1.4.1</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <configuration>
                        <target>
                            <ant antfile="${basedir}/build.xml">
                                <target name="build" />
                            </ant>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test</id>
                    <phase>test</phase>
                    <configuration>
                        <target>
                            <ant antfile="${basedir}/build.xml">
                                <target name="test" />
                                <!-- need test failure to fail the build -->
                            </ant>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>ant</groupId>
        <artifactId>ant-junit</artifactId>
        <version>1.6.5</version>
        <!--scope>test</scope -->
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <!--scope>test</scope -->
    </dependency>
</dependencies>
</project>

4.0.0

这就解释了JUnit3是如何被引入的


那我现在该怎么办?有没有办法使用maven antrun插件通过build.xml中的ant任务运行我的JUnit 4测试?

不确定您是否仍然需要解决此问题,但我们遇到了类似的问题

您可以使用
Junit.framework.JUnit4TestAdapter
类在JUnit3测试运行程序中运行JUnit4测试

因此,以您的测试类为例,您可以简单地向该类添加以下方法定义:

public static junit.framework.Test suite() {
    return new junit.framework.JUnit4TestAdapter(ExampleBaseObjectDaoTest.class);
}
然后,测试应该在maven中运行


此解决方案的缺点是,您需要将
suite()
方法添加到所有测试类中。

这不是对问题的直接回答,而是我处理此问题的建议(因此我首先将其作为注释):


移动到maven不应该通过包装ant目标来完成。试着(至少)采用这个方法。如果您不能这样做,请尝试按配置更改标准maven测试源代码directoy(覆盖Supor POM设置)。最后使用maven build进行编译和测试。

以防有人仍在为此而挣扎

我遇到了同样的“找不到测试”问题,并尝试添加扩展TestCase和添加suite()方法,如上所述。这种方法很有效(取决于所使用的TestRunner),但不是很令人满意

我偶然发现了一个更好的运行Ant JUnit4测试任务的解决方案:

  • 在maven antrun插件配置中包括以下依赖项:
  • 
    朱尼特
    朱尼特
    4.13
    
    你确定正在使用Maven中的Junit依赖项吗?可能是ANT访问的Junit库正在使用。我查看了它,maven antrun插件依赖于Junit 3.8.1,这就是原因。我从中了解到,这与此类似,但除了删除导致冲突的插件外,没有其他解决方案。除非有另一种方法从maven运行ant,否则我无法删除antrun。移动到maven不应该通过包装ant目标来完成。试着(至少)采用这个方法。如果您无法做到这一点,请尝试通过配置(覆盖Supor POM设置)来更改标准maven测试源代码directoy。最后使用maven进行编译和测试。你能回答这个问题吗?谢谢!我只是放弃了从maven运行ant目标,就像上面评论中建议的@FrVaBe一样。非常好,谢谢,对我有用。我需要在maven下的ant下运行junit4,以便将所有测试放在一个地方。花了很多时间在谷歌上寻找你的答案!对我来说,它没有junit依赖项,也没有
    maven\u test\u classpath
    元素。
    public static junit.framework.Test suite() {
        return new junit.framework.JUnit4TestAdapter(ExampleBaseObjectDaoTest.class);
    }