java - Are there any tools to create dummy objects to use for JUnit test cases? -
i writing junit test cases test crud operations in dao classes. pretty boilerplate code , bulk of create test object , assign dummy values instance variables. there tools in java create object , assign dummy values based on declared type?
i don't want use jmock or mockito need interact database , test crud operations successful.
i don't know tool create mock object , fill automatically fuzzy data.
but maybe following approach close want achieve.
pseudo code
foo foo = new foo(); foo.setintvalue(generateint()); foo.setstringvalue(generatestring(20)); ... // store record in database // retrieve record database // check if retrieved values equal values in `foo`
if want achieve might have quickcheck
the goal of quickcheck replace manually picked values generated values. quickcheck-based test tries cover laws of domain whereas classical testing can test validity distinct values.
basically, quickcheck generators of data. quickcheck runner method fancy loop implementation. quickcheck can in scenarios whole classes of test cases have tested , not feasible write tests distinct test scenarios.
so create own generator
provides instances of foo
filled fuzzy data. in combination junit theories close initial requirement.
an example using theories
custom generator can find here. example written similar library (junit-quickcheck). should demonstrate idea.
edit based on junit-quickcheck
example. might in following snippet.
import net.java.quickcheck.generator; import static net.java.quickcheck.generator.primitivegenerators.characters; public class mycharactergenerator implements generator<string> { private static final string lowercase_chars = "abcdefghijklmnopqrstuvwxyz"; private static final string uppercase_chars = "abcdefghijklmnopqrstuvwxyz"; private static final string numbers = "0123456789"; private static final string special_chars = ".-\\;:_@[]^/|}{"; private static final string all_my_chars = lowercase_chars + uppercase_chars + numbers + special_chars; public static final int capacity = 40; generator<character> charactergenerator = characters(all_my_chars); public string generate() { stringbuilder sb = new stringbuilder(capacity); (int = 0; < capacity; i++) { sb.append(charactergenerator.next()); } return sb.tostring(); } @override public string next() { return generate(); } }
.
import net.java.quickcheck.generator; import static org.testng.assert.asserttrue; import org.testng.annotations.test; public class mycharactergeneratortest { @test public void teststringgenerator() { generator<string> fuzzystring = new mycharactergenerator(); (int = 0; < 10; i++) { string fuzzy = fuzzystring.next(); system.out.println("fuzzy: " + fuzzy); asserttrue(fuzzy.length() == mycharactergenerator.capacity); asserttrue(fuzzy.matches("[a-za-z0-9.\\-\\\\;:_@\\[\\]^/|}{]*")); } } }
output
------------------------------------------------------- t e s t s ------------------------------------------------------- running mycharactergeneratortest fuzzy: ;d|xrs|dfs3h@xzrnze6n\.ly600{c@ll;de5:jn fuzzy: uczbo|qj/6flqbh9qwfppcuk.qa5hegfr_3a1@;b fuzzy: jg}xd44_afvqy\ukmehgpnv8xmkky]ddxjsyig9c fuzzy: k-en-sf^ek.bqqn4pr2[93{wyzgwr_f_ktbgktp} fuzzy: 1udchf3awn0d/95@}k[w2|p]}.epzkvrjmjtb0/z fuzzy: @j2}kmk@.uzy]smpkwz;c4@p-kp9}kutan@ovlx9 fuzzy: gol8o5qz-ynga:i;wqghkto^1ithqenxm3oro||4 fuzzy: _\1ifr:ssplcdt9l\s{clv9zozgca^i67if/|t0t fuzzy: /fwl9ncurcemqr2sp3|xg9ui5y21k:r0ys1xiz/3 fuzzy: 8u[xk^e60jhgfltmygz:z;gn9ucxcueu@wv\oj7]
Comments
Post a Comment