java - How to give null value to a parameter when using @FileParameters of JUnitParams -
i try use fileparameters annotation of junitparamsrunner. cannot give null variable. code , test file below.
@runwith(junitparamsrunner.class) public class passwordcheckerfileparameterizedtest { @test @fileparameters("src/test/resources/testscenarios.csv") public void checkpasswordisvalid_checkmultiplecase(string password,boolean expectedresult){ passwordchecker passwordchecker = new passwordchecker(); assertequals(expectedresult,passwordchecker.checkpasswordisvalid(password)); } }
testscenarios.csv
,false sd1.,false ssfdsdfsdf234.,false sewerwer234.,false ssfdsdfsdsdfsdf.,false ssfdsdfsdsdfsdf3234,false ssfdsdfsdsdfsdf23.,true
this not work default since fileparameters
uses identitymapper
map lines in file parameters , works if used @parameters({"aaa,bbb", "ccc,ddd"}
syntax in cannot provide null
value - writing null
give string saying "null
".
you can provide own mapper means of fileparameters#mapper
. has return mapped object[][]
(same format if used method parameter provider @parameters(method = ...)
). need decide in way you'll mark null
in file.
a sample mapper treats string "xxx"
null
marker this:
public class xxxtonullmapper implements datamapper { @override public object[] map(reader reader) { return new bufferedreader(reader).lines() .map(line -> line.split(",")) .map(columns -> stream.of(columns) .map(column -> column.equals("xxx") ? null : column) .collect(collectors.tolist()).toarray() ) .collect(collectors.tolist()).toarray(); } }
usage:
@test @fileparameters( value = "/test.csv", mapper = xxxtonullmapper.class ) public void xxxisnullfileparameters(string a, string b) { system.out.println("params are: " + + " (is null? " + (a == null) + "), " + b + " (is null? " + (b == null) + ")"); }
/test.csv
:
aaa,bbb aaa,xxx
prints
params are: aaa (is null? false), bbb (is null? false) params are: aaa (is null? false), null (is null? true)
Comments
Post a Comment