Posted by Romain Delamare in Scertify™ EasyCoverage on December 17, 2012 with 2 Comments
Unit tests often require repetitive and tedious basic verifications. This is error-prone and not time-efficient, as the developer also need to concentrate on the logic of the classes under test. At Tocea, we have developed a tool to help developers with these simple verifications: EasyCoverage can dynamically generate unit tests.
EasyCoverage offers several benefits. It is fully automated and does not generate any source code. Test cases are generated at runtime and are only stored in memory. It is configurable and extensible. You can easily specify which classes or methods should be tested, and you can have several test suites, each with its own configuration. You can add your own checkers to improve the performed verifications.
Download it :
Maven central and Maven repository
POM dependencies :
|
1 2 3 4 5 6 7 8 9 10 |
<dependency>
<groupId>com.tocea.easycoverage</groupId>
<artifactId>easyCoverage4J-framework</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.tocea.easycoverage</groupId>
<artifactId>easyCoverage4J-junit</artifactId>
<version>0.2.0</version>
</dependency> |
Checkers perform verification, either on a method or an a class. You can implement your own, or use some of the implemented ones. You can read the checkers documentation for more information on how to write one. Here are a quick description of the default checkers.
Class checkers will make verifications that are related to the class or can be done only once per class.
Method checkers will make verifications on a method and can thus be executed several times per class.
The @SkipAutomaticTests annotation can be attached to a class or a method. Checkers will not be run on an element with this annotation. It is useful if you want to automatically compute a list of classes (for instance wiht ClassScanner), but some of the classes should not be tested. It is also useful to skip a few methods in a class, but still test the other methods.
In this tutorial, we will see how to configure Easy Coverage with JUnit. First you need to configure your project to use Easy Coverage. If you are using Maven, just add dependencies to easyCoverage4J-framework and easyCoverage4J-junit (both in group com.tocea.easycoverage). Otherwise, just download easycoverage-framework.jar and easycoverage-junit.jar and put them in your classpath. Easy Coverage depends on slf4j, Logback, and of course JUnit, so be sure to include these too. Now you are ready to configure your first Easy Coverage test suite. Create a new class with a unique static method suite that returns a TestSuite:
|
1 2 3 4 |
@Test
public static TestSuite suite() {
...
} |
To create a test suite you are going to use the helper class JUnitTestSuiteProvider. This class will hold the configuration of the test suite, such as the classes under test, the checkers to use, etc. Create an instance of JUnitTestSuiteProvider. The constructor has an optional String argument, which is the name given to the test suite (if no name is given, a default name is used).
|
3 4 |
JUnitTestSuiteProvider testSuiteProvider =
new JUnitTestSuiteProvider("Easy Coverage Quick Start Test Suite"); |
There are two ways of specifying the classes under test. You can add the classes manually using JUnitTestSuiteProvider.addClass(Class<?>), or you can use the helper class ClassScanner, which can scan a folder and return a set of classes (in this case use the JUnitTestSuiteProvider.setClasses(Set<Class<?>>)method.
|
6 7 |
testSuiteProvider.addClass(ClassUnderTest.class);
testSuiteProvider.addClass(AnotherClassUnderTest.class); |
Now you need to specify how the instances of the classes under test will be created. Easy Coverage relies on instance providers. A default one is used if none is given. You can find more information on instance providers, and how to create your own, in the instance provider documentation. In this example we will use the default one, thus we do not need to do anything. The last configuration step is to specify the checkers to use. There are two kind of checkers, class checkers and method checkers. For more information, and a list of implemented checkers, see the checkers documentation.
|
9 10 11 12 13 14 15 16 17 18 19 |
testSuiteProvider.addClassChecker(ToStringNotNullChecker.class);
testSuiteProvider.addClassChecker(BijectiveCompareToChecker.class);
testSuiteProvider.addClassChecker(BijectiveEqualsChecker.class);
testSuiteProvider.addClassChecker(CloneChecker.class);
testSuiteProvider.addClassChecker(NPEConstructorChecker.class);
testSuiteProvider.addClassChecker(NullValueEqualsChecker.class);
testSuiteProvider.addMethodChecker(NPEMethodChecker.class);
testSuiteProvider.addMethodChecker(GetterNotNullChecker.class);
testSuiteProvider.addMethodChecker(SetterChecker.class);
testSuiteProvider.addMethodChecker(ArrayIndexOutOfBoundExceptionChecker.class); |
You can specify on optional timeout (in ms), with the JUnitTestSuiteProvider.setTimeout(int)method. By default there is no timeout (the value is 0), but you can set one if need be (for instance if a bad configuration causes the method under test to freeze of loop indefinitely). Finally, you just need to return the test suite created by the provider:
|
21 |
return testSuiteProvider.getTestSuite(); |
Your method suite() should look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@Test
public static TestSuite suite() {
JUnitTestSuiteProvider testSuiteProvider =
new JUnitTestSuiteProvider("Easy Coverage Quick Start Test Suite");
testSuiteProvider.addClass(ClassUnderTest.class);
testSuiteProvider.addClass(AnotherClassUnderTest.class);
testSuiteProvider.addClassChecker(ToStringNotNullChecker.class);
testSuiteProvider.addClassChecker(BijectiveCompareToChecker.class);
testSuiteProvider.addClassChecker(BijectiveEqualsChecker.class);
testSuiteProvider.addClassChecker(CloneChecker.class);
testSuiteProvider.addClassChecker(NPEConstructorChecker.class);
testSuiteProvider.addClassChecker(NullValueEqualsChecker.class);
testSuiteProvider.addMethodChecker(NPEMethodChecker.class);
testSuiteProvider.addMethodChecker(GetterNotNullChecker.class);
testSuiteProvider.addMethodChecker(SetterChecker.class);
testSuiteProvider.addMethodChecker(ArrayIndexOutOfBoundExceptionChecker.class);
return testSuiteProvider.getTestSuite();
} |
Just launch the test suite with your favorite JUnit runner, and you will see a list of test cases dynamically created. Here is an example of JUnit report (in Eclipse) produced by running Easy Coverage on itself:
This is the coverage result (using EclEmma):
Comments are closed.
http://tocea.comTomek Kaczanowski on December 18, 2012
“for every controllers of a class” -> “for every constructor of a class”