-
Notifications
You must be signed in to change notification settings - Fork 2
EasyTest Codegen : Getting Started
In order to get up an running quickly, please follow the step-by-step instructions provided in this page. Our assumption is you are using maven build process, if not please follow equivalent steps for ant build. Please refer to [Easytest Codegen Introduction] (github.com/EaseTech/easytest-codegen/wiki/EasyTest-Codegen-:-Introduction)
-
Add dependencies to your project. easytest-core, easytest-codegen are main dependencies to be added to your project. these jar files depends on other jars like junit-4.10, slf4j, apache-poi, javacsv, cglib etc..
-
Add javadoc task to maven ant run plugin in your project pom.xml, check its arguments like source directory, package name, output directory, template properties file name, seed data properties.
-
Make sure you have added template properties file and seed data properties file in your source main resources folder.
-
Add your preferences in template properties file. like filters, overwrite test data file, overwrite converters etc..
-
In seed data file, add your domain parameters and values. if data is not provided there, then log will be created with missing parameters data information as explained in the introduction page.
-
Optional step, you can use any slf4j binding for logging. if you have decided to use log4j binding then add log4j.properties file in your resources directory and make sure you have enable org.easetech logging.
-
Check your project .classpath file and ensure build paths are proper. The test data file will be created in src test resources folder so this path has to be in build path.
-
That’s it. you are ready to go. once generation of files completed you might see compilation errors for converts as your beans/VO/DTOs might not have setter methods for few fields. either you can add setter methods to your beans or correct converters so that the parameters are properly populated.
Please find the detailed samples/examples shown below.
-
Dependencies: You need to have EasyTest and EasyTest-Codegen as JAR dependency in your project. Please check if these jar files are available in your repository, if not install them.
<dependency> <groupId>org.easetech</groupId> <artifactId>easytest-core</artifactId> <version>0.8-SNAPSHOT</version> </dependency> <dependency> <groupId>org.easetech</groupId> <artifactId>easytest-codegen</artifactId> <version>0.8-SNAPSHOT</version> </dependency>` -
EasyTest-Codegen depends on other jar files, these should have been automatically installed to your maven repository. if not install them manually.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>net.sourceforge.javacsv</groupId> <artifactId>javacsv</artifactId> <scope>compile</scope> <version>2.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-examples</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics-runtime</artifactId> <version>0.6.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> -
Add javadoc task to maven ant run plugin as shown below in your project pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>generate-test-sources</phase> <goals> <goal>run</goal> </goals> </execution> </executions> <configuration> <tasks> <echo> Generate JUnit Testcases as for EasyTest template.</echo> <!--your source directory --> <property name="src.main.dir" value="src/main/java"/> <!-- your package name --> <property name="package.name" value="com.abc.delegate"/> <!--your output directory --> <property name="output.dir" value="src/test/java"/> <mkdir dir="${output.dir}"/> <javadoc packagenames = "${package.name}" sourcepath = "${src.main.dir}" defaultexcludes = "yes" failonerror = "yes" doclet = "org.easetech.easytest.codegen.JUnitDoclet" docletpathref = "maven.compile.classpath" additionalparam = "-d ${output.dir} -properties junit4-abc.properties -seedData seed_data.properties -buildall"> <!-- make sure you keep your properties file in src.main.resources--> <classpath refid = "maven.compile.classpath" /> </javadoc> </tasks> </configuration> </plugin>` -
Sample template file. Please refer to actual file in easytest-codegen source jar file.
# # This file is part of EasyTest CodeGen, a project to generate # JUnit test cases from source code in EasyTest Template format and helping to keep them in sync # during refactoring. # EasyTest CodeGen, a tool provided by # EaseTech Organization Under Apache License 2.0 # http://www.apache.org/licenses/LICENSE-2.0.txt # #### # implementation of a test case #### template.testcase.default.010=package ${testcase.package.name}; template.testcase.default.020= template.testcase.default.030=import org.junit.Test; template.testcase.default.040=import org.junit.After; template.testcase.default.050=import org.junit.Before; template.testcase.default.060=import org.junit.AfterClass; template.testcase.default.070=import org.junit.BeforeClass; template.testcase.default.080=import static junit.framework.Assert.assertEquals; template.testcase.default.081=import org.junit.runner.RunWith; template.testcase.default.082=import org.easetech.easytest.annotation.DataLoader; template.testcase.default.083=import org.easetech.easytest.annotation.Param; template.testcase.default.084=import org.easetech.easytest.loader.LoaderType; template.testcase.default.085=import org.easetech.easytest.converter.ConverterManager; template.testcase.default.086=import java.beans.PropertyEditorManager; template.testcase.default.090=${marker.import.begin} template.testcase.default.100= ${testcase.imports} template.testcase.default.110=${marker.import.end} template.testcase.default.120= template.testcase.default.130=${license} template.testcase.default.140= template.testcase.default.141= @RunWith(org.easetech.easytest.runner.DataDrivenTestRunner.class) template.testcase.default.142= @DataLoader(filePaths = {"${testcase.data.file.path}" }, loaderType = LoaderType.EXCEL) template.testcase.default.150=public class ${testcase.class.name} template.testcase.default.160= ${marker.extends_implements.begin} template.testcase.default.170= ${marker.extends_implements.end} template.testcase.default.180={ template.testcase.default.190= ${marker.class.begin} template.testcase.default.200= ${testcase.instance.type} ${testcase.instance.name} = null; template.testcase.default.210= ${marker.class.end} template.testcase.default.220= template.testcase.default.230= public ${testcase.class.name}() { template.testcase.default.240= ${marker.method.begin} ${testcase.class.name} template.testcase.default.250= ${marker.method.end} ${testcase.class.name} template.testcase.default.260= } template.testcase.default.270= template.testcase.default.280= public ${testcase.instance.type} createInstance() throws Exception { template.testcase.default.290= ${marker.method.begin} testcase.createInstance template.testcase.default.300= return new ${testcase.instance.type}(); template.testcase.default.310= ${marker.method.end} testcase.createInstance template.testcase.default.320= } template.testcase.default.330= template.testcase.default.340= @Before public void before() throws Exception { template.testcase.default.350= ${marker.method.begin} testcase.before template.testcase.default.360= ${testcase.instance.name} = createInstance(); template.testcase.default.370= ${marker.method.end} testcase.before template.testcase.default.380= } template.testcase.default.390= template.testcase.default.400= @After public void after() throws Exception { template.testcase.default.410= ${marker.method.begin} testcase.after template.testcase.default.420= ${testcase.instance.name} = null; template.testcase.default.430= ${marker.method.end} testcase.after template.testcase.default.440= } template.testcase.default.450= template.testcase.default.460= @BeforeClass public static void beforeClass() throws Exception { template.testcase.default.470= ${marker.method.begin} testcase.beforeClass #template.testcase.default.471= <You can add your environment setup here, note the line no 471, #template.testcase.default.472= you can increment it upto 480 if your code span across multiple lines> template.testcase.default.480= ${testcase.register.editors} template.testcase.default.481= ${testcase.register.converters} template.testcase.default.490= ${marker.method.end} testcase.beforeClass template.testcase.default.500= } template.testcase.default.510= template.testcase.default.520= @AfterClass public static void afterClass() throws Exception { template.testcase.default.530= ${marker.method.begin} testcase.afterClass template.testcase.default.540= template.testcase.default.550= ${marker.method.end} testcase.afterClass template.testcase.default.560= } template.testcase.default.570= template.testcase.default.580= ${testcase.testmethods} template.testcase.default.590= template.testcase.default.600= /** template.testcase.default.610= * JUnitDoclet moves marker to this method, if there is not match template.testcase.default.620= * for them in the regenerated code and if the marker is not empty. template.testcase.default.630= * This way, no test gets lost when regenerating after renaming. template.testcase.default.640= * Method ${testcase.method.unmatched} is supposed to be empty. template.testcase.default.650= * @throws Exception Any exception the enclosed code may throw. template.testcase.default.660= */ template.testcase.default.670= @Test public void ${testcase.method.unmatched}() throws Exception { template.testcase.default.680= ${marker.method.begin} testcase.${testcase.method.unmatched} template.testcase.default.690= ${marker.method.end} testcase.${testcase.method.unmatched} template.testcase.default.700= } template.testcase.default.710=} #### # template for test methods in general (only accessors are handled differently) #### template.testmethod.easytest.010= @Test template.testmethod.easytest.011= public ${method.returntype} ${testmethod.name}(${method.signature}) throws Exception { template.testmethod.easytest.020= ${marker.method.begin} ${method.name} template.testmethod.easytest.021= ${method.returntype} returnValue = ${testcase.instance.name}.${method.name}(${method.parameter.values}); template.testmethod.easytest.022= return returnValue; template.testmethod.easytest.030= ${marker.method.end} ${method.name} template.testmethod.easytest.040= } template.testmethod.easytest.050= #### # template for test methods with void in general (only accessors are handled differently) #### template.testmethodvoid.easytest.010= @Test template.testmethodvoid.easytest.011= public void ${testmethod.name}(${method.signature}) throws Exception { template.testmethodvoid.easytest.020= ${marker.method.begin} ${method.name} template.testmethodvoid.easytest.021= ${testcase.instance.name}.${method.name}(${method.parameter.values}); template.testmethodvoid.easytest.030= ${marker.method.end} ${method.name} template.testmethodvoid.easytest.040= } template.testmethodvoid.easytest.050= #### template.licence.010=/** template.licence.020= * Generated by EasyTest CodeGen, a tool provided by template.licence.030= * EaseTech org Under Apache License 2.0 template.licence.040= * http://www.apache.org/licenses/LICENSE-2.0.txt template.licence.050= */ #### # implementation of a converter for complex type(DTO, class etc..) parameters #### template.converter.default.010=package ${testcase.package.name}; template.converter.default.020= template.converter.default.030=import org.easetech.easytest.converter.AbstractConverter; template.converter.default.040=import java.util.Map; template.converter.default.050=import org.easetech.easytest.util.GeneralUtil; template.converter.default.090=${marker.import.begin} template.converter.default.100= ${converter.imports} template.converter.default.110=${marker.import.end} template.converter.default.120= template.converter.default.130=${license} template.converter.default.140= template.converter.default.150=public class ${converter.class.name} extends AbstractConverter<${converter.instance.type}> { template.converter.default.170= template.converter.default.180= //@Override template.converter.default.190= public ${converter.instance.type} convert(Map<String, Object> convertFrom) { template.converter.default.200= ${converter.instance.type} ${converter.instance.name} = null; template.converter.default.210= template.converter.default.220= if (convertFrom != null) { template.converter.default.230= ${converter.instance.name} = new ${converter.instance.type}(); template.converter.default.240= ${converter.setters} template.converter.default.250= } template.converter.default.260= return ${converter.instance.name}; template.converter.default.270= } template.converter.default.280=} #### # template for converters setters for String types #### template.converter.setmethodstring.010= ${converter.instance.name}.${converter.instance.attribute.setter.name}((${converter.instance.attribute.type}) convertFrom.get("${converter.instance.attribute.name}")); #### # template for converters setters for other than String types #### template.converter.setmethodother.010= if(convertFrom.get("${converter.instance.attribute.name}") != null) { template.converter.setmethodother.020= ${converter.instance.name}.${converter.instance.attribute.setter.name}(GeneralUtil.${converter.instance.attribute.converterutilmethod}(convertFrom.get("${converter.instance.attribute.name}"))); template.converter.setmethodother.030= } #### # template for converters setters for custom editor for enum,timestamp,date,time etc.. types #### template.converter.setmethodeditor.010= if(convertFrom.get("${converter.instance.attribute.name}") != null) { template.converter.setmethodeditor.020= ${editor.class.name} editor = new ${editor.class.name}(); template.converter.setmethodeditor.030= editor.setAsText((String)convertFrom.get("${converter.instance.attribute.name}")); template.converter.setmethodeditor.040= ${converter.instance.name}.${converter.instance.attribute.setter.name}((${converter.instance.attribute.type})editor.getValue()); template.converter.setmethodeditor.050= } #### # template for converters setters for complex field types where converter will be created #### template.converter.setmethodconverter.010= ${converter.instance.name}.${converter.instance.attribute.setter.name}(new ${converter.instance.attribute.converter}().convert(convertFrom)); #### # implementation of a editor for enum,timestamp,date,time etc.. parameters #### template.editor.default.010=package ${testcase.package.name}; template.editor.default.020= template.editor.default.030=import java.beans.PropertyEditorSupport; template.editor.default.040=import ${editor.instance.type.qualifiedname}; template.editor.default.090=${marker.import.begin} #template.editor.default.100= // make sure to add your own imports here template.editor.default.110=${marker.import.end} template.editor.default.120= template.editor.default.130=${license} template.editor.default.140= template.editor.default.150=public class ${editor.class.name} extends PropertyEditorSupport { template.editor.default.170= template.editor.default.180= @Override template.editor.default.190= public void setAsText(String s) { template.editor.default.240= ${editor.setvalue} template.editor.default.270= } template.editor.default.280=} #### # template for editor setvalue for enum types #### template.editor.setvalueenum.010= setValue(${editor.instance.type}.get${editor.instance.type}(s)); #### # template for editor setvalue for enum types #### template.editor.setvaluejodadatetime.010= setValue(new ${editor.instance.type}(s)); #### # template for registering converters #### template.converter.register.010= ConverterManager.registerConverter(${converter.class.name}.class); #### # template for registering PropertyEditors #### template.editor.register.010= PropertyEditorManager.registerEditor(${editor.instance.type}.class, ${editor.class.name}.class); #### # template for importing parameter types if they are not simple type #### template.class.import.010= import ${param.class.type}; #### # Filter include or exclude properties, filter exclude will have priority # so, keep only one property either include or exclude # exclude : Method source will be checked for these exclude strings, if found then test method generation will not happen for that method # include : Method source will be checked for these include strings, test method generation will happen only when one of these include strings exist. #### #filter.exclude = filter.include = criteriaBuilder.createQuery #### # Overwrite existing test data # If this value is YES, then it removes the existing test data and add newly generated test data # If this value is NO, then it appends the existing test data to newly generated test data # Default value is NO. Even if property doesn not exist then value will be treated as NO #### overwrite.existing.test.data = NO #### # Overwrite converters. this feature is useful if converters are modified by users and they don't want to overwrite them later. # If this value is YES, then it replaces the existing converters with newly generated converters # If this value is NO, then it keeps the existing converters. # Default value is YES. Even if property doesn not exist then value will be treated as YES #### overwrite.existing.converters = YES #### # Customised test case extension # if not provided default value is "Test" #### testcase.extension = Test #### # Customised test suite extension # if not provided default value is "Suite" #### testsuite.extension = Suite -
In above sample file, please note the @BeforeClass you can add your environment setup there. also note filter.include/exclude, overwrite.existing.test.data, overwrite.existing.converters documentation and give your preference at these property entries.
-
Sample seed data file, you can add your domain parameters and values. if data is not provided here, then log will be created as explained in the introduction page.
# # This file is part of EasyTest CodeGen, a project to generate # JUnit test cases from source code in EasyTest Template format and helping to keep them in sync # during refactoring. # EasyTest CodeGen, a tool provided by # EaseTech Organization Under Apache License 2.0 # http://www.apache.org/licenses/LICENSE-2.0.txt # #### # Seed Data: used to generate test data, EasyTest codegen uses below property names, and checks if it matches with parameter name # if it matches, then it takes the comma seperated values into a list and add it to test data file. # In case property names are seperated by comma then it takes all those names as property names and repeat above logic for each property name. #### ItemType,ItemSubType = BOOK,LAPTOP,NOTEPAD
-
To enable debug logging, you can add log4j.properties file in src.main.resources folder and below parameter
log4j.logger.org.easetech=DEBUG
-
Check your project’s .classpath entries because test data files and log file are created in src.test.resources folder. make sure these entries are correct
<classpathentry kind="src" output="target/classes" path="src/main/java"/> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"/> <classpathentry excluding="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/>
-
That’s it. You are ready to go. You can see for each class file in the source package following files are generated,
-
Test case file, with suffix ‘Test’ to class file name in output directory src/test/java
-
Test data file with same name as test case file in output directory src/test/resources.
-
Log file with missing test data information in output directory src/test/resources.
-