java - How to utlilize Error Collector for junit testing methods with parameters -
i using errorcollector in junit test case aim of printing out errors , not stopping @ erroneous locations. tried errorcollector using methods without parameters. reduce code duplicacy (i wrote same method 6 times without parameter ; since using 6 files comparison seen in code), have generic method can used achieve same purpose of printing out errors , continuing check. when tried using method parameter got exception "method should have no parameters"..
the following code.
import org.gdal172.ogr.ogr; import org.hamcrest.corematchers; import org.junit.rule; import org.junit.test; import org.junit.rules.errorcollector; import org.junit.runner.junitcore; import org.junit.runner.result; import org.junit.runner.notification.failure; public class dgntester { private readdgn readdgn = new readdgn(); private linkedhashmap<string, integer> layermapcountforcompare = new linkedhashmap<string, integer>(); @rule public errorcollector collector = new errorcollector(); private file output = null; static { // perform ogr format registration once if (ogr.getdrivercount() == 0) ogr.registerall(); } /** * @param args */ public static void main(string[] args) { dgntester dtest = new dgntester(); string dgnfilename_43k10 = "input\\43k10.dgn"; string dgnfilename_43k11 = "input\\43k11.dgn"; string dgnfilename_43k12 = "input\\43k12.dgn"; //the 6 files iam using input. dtest.test(dgnfilename_43k10, "dvd"); dtest.test(dgnfilename_43k10, "all"); dtest.test(dgnfilename_43k11, "dvd"); dtest.test(dgnfilename_43k11, "all"); dtest.test(dgnfilename_43k12, "dvd"); dtest.test(dgnfilename_43k12, "all"); } @test public void test(string filename, string inputtype) { system.out.println("for file -->" + filename); system.out .println("---------------------------------------------------------------------------------------------------"); string fileidentifier = filename.substring(6, 11); string dstfilepath = null; string outputname = null; if (layermapcountforcompare != null) layermapcountforcompare.clear(); if (inputtype.equals("dvd")) { dstfilepath = "f:\\eclipse_helios_3.6.1_64_bit_with_jre_and_add-ons\\eclipse\\resources\\dst\\dvd.dst"; outputname = "output\\outputfile_" + fileidentifier + "_dvd.dst.txt"; } if (inputtype.equals("all")) { dstfilepath = "f:\\eclipse_helios_3.6.1_64_bit_with_jre_and_add-ons\\eclipse\\resources\\dst\\alllayers.dst"; outputname = "output\\outputfile_" + fileidentifier + ".txt"; } layermapcountforcompare = readdgn.getlayerfeaturecount(filename, dstfilepath); // read text output file , compare map. these 6 out put files against each input file output = new file(outputname); if (output.exists()) { try { set keys = layermapcountforcompare.keyset(); iterator itr = keys.iterator(); string key = ""; integer val; string line; bufferedreader br = new bufferedreader(new filereader(output)); while ((line = br.readline()) != null && itr.hasnext()) { key = (string) itr.next(); val = layermapcountforcompare.get(key); string compare = key + "=" + val; compare.trim(); line.trim(); //when print out in positive scenario; able see values of 'compare' , 'line' same /*system.out.println("compare >>> " + compare + " --------------- , --------- line " + line);*/ assertequals("comparing input , output", line, compare); } br.close(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } else { system.out.println("output file not exist"); } } public void assertequals(string msg, object expected, object actual) { collector.checkthat(actual, corematchers.equalto(expected)); } }
in previous example; did not use parameters;
result result = junitcore.runclasses(dgntester.class); (failure failure : result.getfailures()) { system.out.println(failure.tostring()); } if (result.wassuccessful()) { system.out.println("tests finished successfully..."); }
this code helped in triggering test methods , print appropriate methods.
can please guide me how can use generic method thet takes 2 parameters utilize errorcollector.
how utlilize error collector junit testing methods parameters
the @test
annotation doesn't support test methods parameters in signature.
example:
trying run method brokentest
exception thrown. correct test method in junit should correcttest
:
/** method uses parameters in signature. not work! */ @test public void brokentest(string filename) {...} /** correct test method has no parameters in signature. */ @test public void correcttest() {...}
parameterized tests junit
to support parameterized testing can use @runwith(parameterized.class) annotation (class-level).
the test class needs static method returns parameters in iterable
(like list
object). annotate method @parameters
.
furthermore need public
(!) member variable each parameter used, each 1 annotated @parameter(0)
, @parameter(1)
, , on.
thus junit run testwithparameters()
each test case produced createparameters()
method. automatically assign correct parameters @parameter(n) fields (firstparameter/secondparameter).
you can produce many parameters need.
use these parameters in test methods needed refering field names.
example excerpt of class provided:
@runwith(parameterized.class) public class dgntester { @rule public errorcollector collector = new errorcollector(); /** * method generates parameters. * each testvalues.add(...) line produces new test case. * * @return array test values. */ @parameters public static iterable<object[]> createparameters() { list<object[]> testvalues = new arraylist<>(); try { testvalues.add(new object[]{"pre-case1-value1", "case1-value2"}); testvalues.add(new object[]{"case2-param1", "case2-value2"}); testvalues.add(new object[]{"pre-case3-value1", "case3-value2"}); } return testvalues; } /** first parameter. */ @parameter(0) public string firstparameter; /** second parameter. */ @parameter(1) public string secondparameter; /** test using parameters generated createparameters(). * in example check, if first parameter equal * concatenation of string "pre-" , second parameter */ @test public void testwithparameters() { assertthat("wrong parameter values", firstparameter, is("pre-" + secondparameter)); } ... }
Comments
Post a Comment