Yes, you guessed it, yet another Java test code generator!
This goes a little further than just generating the JUnit framework template and method stubs though. It will also generate code to apply different values to the method under test. Objects can also be defined in a global property file so that you can easily provide valid and invalid creations of your own objects.
What on earth prompted me to write my own generator? Well, I used a test code generator some time ago to create some test code. No big suprises there ... then I had to fill in the content which I found boring and repetitive. What was going on that writing test code was boring? It was because I was bombarding all the public methods in an API with valid and invalid values to create tests that would show up a number of errors that had caused problems inside a system due to incorrect input and/or configuration. These were quite straight forward errors to pick up on and, due to the reflection api, quite easy to generate code for so that you only have to fill in the actual test data.
So, I ended up writing a simple, straight forward generator. Now, it's nothing special to look at ... it is a work in progress. Direct lines of thought, no layers for the sake of having layers ... basically I'm saying that I just coded it without trying to follow any pattern, but creating (hopefully) plain, understandable Java code.
<taskdef name="jtcg" classname="net.sourceforge.jtcg.JUnitTCGTask" classpath="c:/my/location/jtcg.jar"/>
Parameters
Attribute | Description | Required |
propertyFile | The property file to be used that contains the object definitions. Eg: c:/my/location/myprops.properties | Yes |
classname | The qualified name of the class that you want to create test code for. Eg: com.foo.bar.MyClass | Yes |
classpath | A path that contains the class given in classname | Yes |
destination | Where to put the generated test code. The code will be generated to the same package structure as given for classname. Eg: classname="com.foo.bar.MyClass" destination="${project.root}/test" would lead to ${project.root}/test/com/foo/bar/MyClassTest.java | Yes |
Example
<target name="Generate.Test.Code"> <jtcg propertyFile="c:/my/location/myprops.properties" classname="com.foo.bar.MyClass" classpath="${classes.dir}" destination="${test.source.dir}"/> </target>
Property file
The properties file is a Java property file and is a list of how to instantiate each object. Eg:
Come on! This can be taken further: