Ant执行任意程序,并在junit任务结束时终止



我正试图针对一系列RMI接口/实现编写单元测试,并依靠Ant+Junit来促进这些测试。主要问题是,无论是失败、错误还是成功,我都需要在Junit任务开始时启动rmiregistry,并在执行结束时关闭。Ant脚本:

<project>
    <target name="test">  
      <javac srcdir="Test/src" destdir="Test/bin" />  
      <junit fork="true" printsummary="true">  
          <batchtest>   
             <fileset dir="Test/test">  
                 <include name="**/*Test.*"/>  
             </fileset> 
          </batchtest>  
      </junit>  
    </target>  
</project>

基本上,只需为junit任务设置haltonerror="false"failonerror="false即可。
这样junit就不会因为测试而失败。启动和停止是通过exec任务完成的。我在这里使用了taskkill来杀死rmiregistry.exe.

<exec executable="start"> <--your start command here -->
  <arg value="rmiregistry"/>
</exec>
<junit fork="true" printsummary="true" haltonerror="false" failonerror="false" errorproperty="juniterrors" failureproperty="junitfailures"> 
  ...
</junit>
<exec executable="taskkill"> <--your shutdown command here -->
  <arg value="/IM"/>
  <arg value="rmiregistry.exe"/>
</exec>
<fail if="juniterrors"/> <!-- ant can fail now, if desired -->
<fail if="junitfailures"/>

另一种选择是使用ant contrib的try/catch,但我认为这里不需要这样做。

最新更新