Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 为什么在netbeans项目中从ant命令行运行groovy测试时不运行测试?_Java_Netbeans_Groovy_Ant - Fatal编程技术网

Java 为什么在netbeans项目中从ant命令行运行groovy测试时不运行测试?

Java 为什么在netbeans项目中从ant命令行运行groovy测试时不运行测试?,java,netbeans,groovy,ant,Java,Netbeans,Groovy,Ant,我有一个netbeans项目,我还有groovy用于spock测试。当我右键单击项目并说test时,它运行一个名为 test-with-groovy 但是,当我使用groovy运行ant测试时,测试是编译的,而不是运行的。我觉得netbeans ide必须添加一些东西,但我不知道是什么,半天的搜索没有结果 有人能帮我吗 以下是您如何获得我得到的结果: 我在netbeans 8.0.2中创建了一个简单的java项目,其中包含一个简单的main package simpleantjava; pu

我有一个netbeans项目,我还有groovy用于spock测试。当我右键单击项目并说test时,它运行一个名为

test-with-groovy
但是,当我使用groovy运行ant测试时,测试是编译的,而不是运行的。我觉得netbeans ide必须添加一些东西,但我不知道是什么,半天的搜索没有结果

有人能帮我吗

以下是您如何获得我得到的结果:

我在netbeans 8.0.2中创建了一个简单的java项目,其中包含一个简单的main

package simpleantjava;

public class SimpleAntJava {

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Main Ran");
    }

}
然后我创建了两个测试文件,一个是junit java文件:

package simpleantjava;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author vextorspace
 */
public class SimpleAntJavaTest {

    public SimpleAntJavaTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of main method, of class SimpleAntJava.
     */
    @Test
    public void testMain() {
        System.out.println("main");
        String[] args = null;
        SimpleAntJava.main(args);
        // TODO review the generated test code and remove the default call to fail.
        fail("Main JUnit Test is a prototype.");
    }

}
一个是基于groovy的spock测试:

package simpleantjava

import org.junit.Test
import spock.lang.Specification

/**
 *
 * @author vextorspace
 */
class NewGroovyTest extends Specification{
    @Test
    def "Our groovy test should run"() {
        expect:
          true
    }

    @Test
    def "Failing tests should fail"() {
        expect:
          false
    }
}
如果我从netbeans运行测试,输出显示它正在运行:

ant -f /home/vextorspace/NetBeansProjects/SimpleAntJava -Dtest.binarytestincludes=**/*Test.class -Dtest.binaryincludes= -Dtest.binaryexcludes=**/*$* test-with-groovy
但是如果我在ant命令行中运行它,它不会运行任何测试,尽管它没有给出错误,它也会将两个测试文件编译成build/test/classes中的类文件

如果我运行AntClean测试,它将构建两个测试文件,但不运行groovy测试,只运行java测试

build-impl.xml有test的定义,我不包括整个文件,但它是由netbeans创建的标准文件:以下是我相信的相关部分:

<target if="${nb.junit.single}" name="-init-macrodef-junit-single" unless="${nb.junit.batch}">
    <macrodef name="junit" uri="http://www.netbeans.org/ns/j2se-project/3">
        <attribute default="${includes}" name="includes"/>
        <attribute default="${excludes}" name="excludes"/>
        <attribute default="**" name="testincludes"/>
        <attribute default="" name="testmethods"/>
        <element name="customize" optional="true"/>
        <sequential>
            <property name="junit.forkmode" value="perTest"/>
            <junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}">
                <test methods="@{testmethods}" name="@{testincludes}" todir="${build.test.results.dir}"/>
                <syspropertyset>
                    <propertyref prefix="test-sys-prop."/>
                    <mapper from="test-sys-prop.*" to="*" type="glob"/>
                </syspropertyset>
                <formatter type="brief" usefile="false"/>
                <formatter type="xml"/>
                <jvmarg value="-ea"/>
                <customize/>
            </junit>
        </sequential>
    </macrodef>
</target>
<target depends="-init-test-properties" if="${nb.junit.batch}" name="-init-macrodef-junit-batch" unless="${nb.junit.single}">
    <macrodef name="junit" uri="http://www.netbeans.org/ns/j2se-project/3">
        <attribute default="${includes}" name="includes"/>
        <attribute default="${excludes}" name="excludes"/>
        <attribute default="**" name="testincludes"/>
        <attribute default="" name="testmethods"/>
        <element name="customize" optional="true"/>
        <sequential>
            <property name="junit.forkmode" value="perTest"/>
            <junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}">
                <batchtest todir="${build.test.results.dir}">
                    <fileset dir="${test.src.dir}" excludes="@{excludes},${excludes}" includes="@{includes}">
                        <filename name="@{testincludes}"/>
                    </fileset>
                    <fileset dir="${build.test.classes.dir}" excludes="@{excludes},${excludes},${test.binaryexcludes}" includes="${test.binaryincludes}">
                        <filename name="${test.binarytestincludes}"/>
                    </fileset>
                </batchtest>
                <syspropertyset>
                    <propertyref prefix="test-sys-prop."/>
                    <mapper from="test-sys-prop.*" to="*" type="glob"/>
                </syspropertyset>
                <formatter type="brief" usefile="false"/>
                <formatter type="xml"/>
                <jvmarg value="-ea"/>
                <customize/>
            </junit>
        </sequential>
    </macrodef>
</target>
<target depends="-init-macrodef-junit-init,-init-macrodef-junit-single, -init-macrodef-junit-batch" if="${junit.available}" name="-init-macrodef-junit"/>

使用groovy ant target进行的测试包含在随附的groovy-build.xml中:

<target depends="init,compile-test,-pre-test-run" if="have.tests" name="-do-test-run-with-groovy">
    <j2seproject3:test testincludes=""/>
</target>
<target depends="init,compile-test,-pre-test-run,-do-test-run-with-groovy" if="have.tests" name="-post-test-run-with-groovy">
    <fail if="tests.failed" unless="ignore.failing.tests">Some tests failed; see details above.</fail>
</target>
<target depends="init,compile-test,-pre-test-run,-do-test-run-with-groovy,test-report,-post-test-run-with-groovy,-test-browse" description="Run unit tests." name="test-with-groovy"/>
<target depends="init,compile-test-single,-pre-test-run-single" if="have.tests" name="-do-test-run-single-groovy">
    <fail unless="test.binarytestincludes">Must select some files in the IDE or set test.includes</fail>
    <j2seproject3:test testincludes=""/>
</target>
我必须承认,我不知道马克洛德家族到底在干什么。但我已经尝试将**/*Test.java更改为**/*Test.groovy、***/*Test.class和**/Test。无济于事

有人知道怎么做吗?我不想放弃整个构建过程,因为我实际上正在处理一个已有6年历史的遗留项目,该项目使用了jogl插件和ant文件中的许多自定义内容,所以我想弄清楚如何实现这一点

谢谢大家!

NewGroovyTest是使用Spock的完全独立的测试,与其他类没有关系

为了包含test/***test.groovy文件中定义的类,我将编译后的类作为所有groovy编译到Java中,并将它们输入JUnit。如前所述,默认行为是只使用来自*Test.java文件的类

此链接将帮助您:

具体而言,添加以下内容:

<target name="-post-test-run-with-groovy">
<junit dir="${work.dir}" errorproperty="tests.failed"
       failureproperty="tests.failed" fork="true"
       showoutput="true">
  <batchtest todir="${build.test.results.dir}">
    <fileset dir="${build.test.classes.dir}">
      <include name="**/*Test.class"/>
      <exclude name="**/*$*"/>
    </fileset>
  </batchtest>
  <classpath>
    <path path="${run.test.classpath}"/>
  </classpath>
  <syspropertyset>
  <propertyref prefix="test-sys-prop."/>
  <mapper from="test-sys-prop.*" to="*" type="glob"/>
  </syspropertyset>
  <formatter type="brief" usefile="false"/>
  <formatter type="xml"/>
  <jvmarg line="${run.jvmargs}"/>
</junit>

<mkdir dir="${build.test.results.dir}/../report"/>
<mkdir dir="${build.test.results.dir}/../report/html"/>

<junitreport todir="${build.test.results.dir}/../report">
  <fileset dir="${build.test.results.dir}">
    <include name="TEST-*.xml"/>
  </fileset>
  <report format="frames" todir="${build.test.results.dir}/../report/html"/>
</junitreport>

您是否尝试过在调试模式下运行ant以查看发生了什么?命令is-ant-debugh执行了与您描述的相同的操作。唯一的问题是,我只通过了groovy junit类中的测试。这在使用Netbeans IDE时运行正常。在命令和提示符下运行相同的命令时,会出现错误,如中所示。那么您面临的是相同的问题吗?看来NewGroovyTest是使用Spock进行的完全独立的测试,与其他类没有关系。找到了关于运行测试命令行的链接,但它适用于java和little-aged。和另一个链接到。可能对你有用。谢谢Rao,我现在正在运行我的测试,看来你的groovy测试链接成功了!有趣的是,我刚刚发现我可以通过在-do测试运行目标的testinclude中添加,***Test.groovy来修改我的nbproject/build-impl.xml。我以前试过,但一定是弄糟了什么。这样做的另一个好处是不需要运行两次Java测试,但缺点是build-impl.xml是自动生成的。有人知道模板来自哪里吗?
<target name="-post-test-run-with-groovy">
<junit dir="${work.dir}" errorproperty="tests.failed"
       failureproperty="tests.failed" fork="true"
       showoutput="true">
  <batchtest todir="${build.test.results.dir}">
    <fileset dir="${build.test.classes.dir}">
      <include name="**/*Test.class"/>
      <exclude name="**/*$*"/>
    </fileset>
  </batchtest>
  <classpath>
    <path path="${run.test.classpath}"/>
  </classpath>
  <syspropertyset>
  <propertyref prefix="test-sys-prop."/>
  <mapper from="test-sys-prop.*" to="*" type="glob"/>
  </syspropertyset>
  <formatter type="brief" usefile="false"/>
  <formatter type="xml"/>
  <jvmarg line="${run.jvmargs}"/>
</junit>

<mkdir dir="${build.test.results.dir}/../report"/>
<mkdir dir="${build.test.results.dir}/../report/html"/>

<junitreport todir="${build.test.results.dir}/../report">
  <fileset dir="${build.test.results.dir}">
    <include name="TEST-*.xml"/>
  </fileset>
  <report format="frames" todir="${build.test.results.dir}/../report/html"/>
</junitreport>
ant -f ~/NetBeansProjects/jdesigner -Dtest.binarytestincludes=**/*Test.class -Dtest.binaryincludes= -Dtest.binaryexcludes=**/* test-with-groovy