我们首先在maven中包装我们的ant构建,开始转向maven .
我们可以让maven构建项目,但不能运行测试 . 我们正在尝试从maven在build.xml中执行ant测试目标 . 当然,当我们从ant运行测试目标时,一切都很好 .
以下是为一个测试类显示的错误(对于其他测试类相同) . 这个类中有几个测试方法,但没有一个正在运行 . 这似乎是一个JUnit版本的问题;测试是JUnit 4测试,如果我们更改测试类以扩展TestCase(JUnit 3样式),它会找到测试 .
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>
这是我的build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="mf-common" xmlns:aspectj="antlib:org.aspectj">
<path id="EAR Libraries.libraryclasspath" />
<path id="aspectj.libraryclasspath">
<fileset dir="lib">
<include name="*.jar" />
</fileset>
</path>
<path id="common.classpath">
<fileset dir="test/lib">
<include name="*.jar" />
</fileset>
<path refid="aspectj.libraryclasspath" />
<path refid="EAR Libraries.libraryclasspath" />
</path>
<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
<classpath>
<path refid="common.classpath" />
<pathelement location="lib/aspectjtools.jar" />
</classpath>
</taskdef>
<property environment="env" />
<property name="debuglevel" value="source,lines,vars" />
<property name="target" value="1.6" />
<property name="source" value="1.6" />
<target name="init">
<mkdir dir="bin" />
<copy includeemptydirs="false" todir="bin">
<fileset dir="src" excludes="**/*.launch, **/*.java" />
</copy>
<copy includeemptydirs="false" todir="bin">
<fileset dir="test/src" excludes="**/*.launch, **/*.java" />
</copy>
</target>
<target name="clean">
<delete dir="bin" />
<delete file="dist/${ant.project.name}.jar" />
</target>
<target depends="clean" name="cleanall" />
<target depends="init" name="build">
<echo message="${ant.project.name}: ${ant.file}" />
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="src" />
<classpath refid="common.classpath" />
</javac>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="test/src" />
<classpath refid="common.classpath" />
</javac>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src" excludes="**/*.launch, **/*.java" />
</copy>
<copy includeemptydirs="false" todir="bin">
<fileset dir="test/src" excludes="**/*.launch, **/*.java" />
</copy>
<echo message="after javac" />
<aspectj:iajc debug="true" noweave="true" outJar="dist/mf-common.jar" sourceRootCopyFilter="**/CVS/*,**/*.java,**/*properties" sourceroots="bin/" source="${source}" target="${target}">
<classpath refid="common.classpath" />
</aspectj:iajc>
</target>
<target name="test" depends="build">
<ant dir="${basedir}" inheritAll="false" antfile="test.xml" />
</target>
</project>
UPDATE:
那么,当我们告诉它使用JUnit 4时,为什么maven会运行JUnit 3?我运行了mvn -X测试,看到JUnit 3.8.1 jar因某种原因被拉入,但我不知道如何找出原因 .
UPDATE:
我发现maven antrun插件取决于junit 3.8.1 http://maven.apache.org/plugins/maven-antrun-plugin/dependencies.html
这就解释了Junit 3如何被吸引进来 .
那我现在该怎么办?有没有办法使用maven antrun插件通过build.xml中的ant任务运行我的JUnit 4测试?
2 回答
这不是问题的直接答案,而是我处理问题的建议(因此我首先将其作为评论):
不应该通过包装 Ant 目标来移动到maven . 尝试(至少)采用maven standard directory layout . 如果您不能这样做,请尝试通过配置更改标准maven测试源directoy(覆盖Supor POM设置) . 最后使用maven build lifecycle进行编译和测试 .
不确定您是否仍需要解决此问题,但我们遇到了类似的问题 .
您可以使用
junit.framework.JUnit4TestAdapter
类在Junit 3测试运行器中运行Junit 4测试 .因此,使用您的测试类作为示例,您只需将以下方法定义添加到类中:
然后测试应该在maven中运行 .
此解决方案的缺点是您需要将
suite()
方法添加到所有测试类中 .