java - JUnit 4.11 not showing parameter value but index -


we have project number of junit testclasses has been using eclipse internal junit implementation until recently. in order run our tests ant build script have changed build path our project use external junit-4.11.jar , required hamcrest-core library. of our test classes use parameterized junit runner (name = "{0}") option in @parameters annotation. while running these test classes eclipse built-in junit output showed value of first parameter instead of index. now, after using external junit index shown no matter if run test inside eclipse or ant build script "test" target. here's test class:

package foo;  import java.util.arrays; import org.junit.test; import org.junit.runner.runwith; import org.junit.runners.parameterized; import org.junit.runners.parameterized.parameter; import org.junit.runners.parameterized.parameters;  @runwith(parameterized.class) public class parametertest {      public parametertest(string _name, string _value)     {         name = _name;         value = _value;     }         @parameters(name = "{0}")     public static iterable<string[]> testdata() {         return arrays.aslist(new string[][] {                 { "name1", "value1" },                 { "name2", "value2" } });     }      @parameter(0)     public string name;      @parameter(1)     public string value;      @test     public void test() {         system.out.println(name+value);     } } 

the constructor had added in order run test ant script, otherwise illegalargumentexception result. @parameter(x) annotations removed doesn't change result.

edit: if run class inside eclipse ("run -> junit test") "initializationerror" , failure trace says "java.lang.exception: test class should have 1 public zero-argument constructor". if run test within ant build script "test" target test runs without errors output says "test[0]" , "test[1]" instead of "test[name1]" , "test[name2]".

to summarize: 1. if add constructor test class many parameters need won't run within eclipse. 2. without constructor test runs within eclipse , naming of tests correctly taken configured parameter. however, without constructor, test won't run ant script.

the aim tests run both within eclipse , ant script , show correct name in both scenarios.

edit2: according documentation can use either @parameter inject parameters or can use constructor. when edit test class above , remove @parameter annotations works fine in eclipse , shows correct name next each run. however, when run ant script still runs ok doesn't show name index position next test.

important add correct junit version (in case 4.11) classpath of junit ant task. here simple ant file compiling , executing parametertest. directory structure looks this:

src    /test/parametertest.java lib    /junit-4.11.jar    /hamcrest-core-1.3.jar 

build.xml:

<?xml version="1.0" encoding="utf-8"?>  <project name="test"> <property name="src" value="src" /> <property name="build" value="target/classes" /> <property name="lib" value="lib" /> <property name="test.result" value="target/test-results" /> <property name="java.version" value="1.8" />   <target name="init">     <mkdir dir="target/test-results" />     <mkdir dir="target/classes" /> </target>  <target name="compile" depends="init">     <javac srcdir="src" destdir="${build}" listfiles="no" deprecation="off" debug="on" nowarn="on"            includeantruntime="true" source="${java.version}" target="${java.version}">         <classpath>             <fileset dir="${lib}" includes="*.jar"/>         </classpath>     </javac> </target>  <target name="test"  depends="compile">   <junit printsummary="on" haltonfailure="no" showoutput="no"        fork="yes"        forkmode="once" tempdir="/tmp">      <formatter type="xml"/>      <classpath>         <!-- adding first classpath inherited shell -->         <pathelement path="${java.class.path}"/>         <!-- add lib folder junit-4.11.jar -->         <fileset dir="${lib}" includes="*.jar"/>     </classpath>      <classpath location="${build}" />      <batchtest todir="${test.result}">         <!-- location of compiled junit classes -->         <fileset dir="${src}">             <include name="**/*test*.java" />         </fileset>     </batchtest> </junit> </target> 

after executing ant test can have @ target/test-results/test-test.parametertest.xml , see lines:

  <testcase classname="test.parametertest" name="test[name1]" time="0.0"/>   <testcase classname="test.parametertest" name="test[name2]" time="0.0" /> 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -