JagWire
JagWire

Reputation: 269

Adding junit test single file target to free-form java project in netbeans

I promise, I've read:http://netbeans.org/kb/articles/freeform-config.html

I have a java free-form project that I would like to modify to include a "Test Single File" target to the context menu within Netbeans 7.2

The included link outlines creating an action with the name "test.single" (in order to override Netbeans' Test Single File command) and within that action creation, one must specify an ant target and a context object like so:

<context>
    <property>testclass</property>
    <folder>${current.dir}</folder>
    <pattern>\.java$</pattern>
    <format>java-name</format>
    <arity>
        <one-file-only/>
    </arity>
 </context>

So to summarize, I have:

Created the action in project.xml within the ide-actions block:

<action name="test.single">                                     
  <target>test-single</target>

  <context>
      <property>testclass</property>
      <folder>${current.dir}</folder>
      <pattern>\.java$</pattern>
      <format>java-name</format>
      <arity>
          <one-file-only/>
      </arity>
  </context>
</action>

Added the ide-action to the context-menu block"

<ide-action name="test.single"/>

Adding this to the free-form project's project.xml file yields a grayed out "test.single" entry in the context menu upon right-clicking on the project name. Further, right clicking on a test class in my src/test directory yields a grayed out "Test Single File" entry.

I've checked and validated the xml and it all seems to check out. What could I be doing wrong?

thanks in advance!

Upvotes: 3

Views: 1420

Answers (1)

richardeigenmann
richardeigenmann

Reputation: 433

Had the same problem and managed to solve it by cloning the action run.single in by nbproject/project.xml and calling it test.single:

<action name="test.single">
    <script>build.xml</script>
    <target>test-single</target>
    <context>
        <property>test.class</property>
        <folder>src/java</folder>
        <pattern>\.java$</pattern>
        <format>java-name</format>
        <arity>
            <one-file-only/>
        </arity>
    </context>
</action>

I also changed the property to test.class as we need this in the projects build.xml to run the appropriate test class.

In the main build.xml I have a target test-single:

<target name="test-single" description="Run individual Unit tests" depends="compile, compile-test" >  
    <junit
        printsummary="yes"
        errorProperty="test.failed"
        failureproperty="test.failed"
        haltonfailure="yes" 
        fork="yes"
        showoutput="yes">
        <formatter type="plain" usefile="false"/>
        <classpath>
            <pathelement location="${build.base.classes.dir}" />
            <pathelement location="${unit.test.classes.dir}" />
            <pathelement location="${junit.dir}" />
        </classpath>
        <test name="${test.class}Test" todir="${results}">
        </test>
    </junit>
    <fail message="Tests failed!" if="test.failed"/>
</target>

Note that the refers to the property ${test.class}. But when I leave it like that it tries to run the class being tested instead of the JUnit test class. Since this is always called the same as the class being tested with the word "Test" at the end I have written it so that the name is ${test.class}Test.

Upvotes: 1

Related Questions