Menu

Quick Start

Romain Delamare
Attachments

Quick Start

In this tutorial, we will see how to configure Easy Coverage with JUnit. First you need to configure your project to use Easy Coverage. For Maven you can simply add it to your dependencies:

<dependency>
    <groupId>com.tocea.easycoverage</groupId>
    <artifactId>easyCoverage4J-framework</artifactId>
    <version>0.2.2</version>
</dependency>
<dependency>
    <groupId>com.tocea.easycoverage</groupId>
    <artifactId>easyCoverage4J-junit</artifactId>
    <version>0.2.2</version>
</dependency>

If you are using other dependency management tools (like Apache Ivy) you can look for the configuration on Maven Central for EasyCoverage4J-framework and EasyCoverage4J-junit. Alternatively you can download the jars from the files section. Be sure to add the jar for slf4j, Logback, and JUnit in your classpath as Easycoverage depends on them.

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:

@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.

    JUnitTestSuiteProvider testSuiteProvider = new JUnitTestSuiteProvider();

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.

    testSuiteProvider.addClass(ClassUnderTest.class);
    testSuiteProvider.addClass(AnotherClassUnderTest.class);

Or

    final ClassScanner scanner = new ClassScanner();
    scanner.scanFolder(new File("path/to/src/folder"));
    testSuiteProvider.setClasses(scanner.getClasses());

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.

    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:

    return testSuiteProvider.getTestSuite("Easy Coverage Quick Start Test Suite");

The string parameter is the name of the created JUnit suite. It is optional, and a default name will be provided if ommited.

Your method `suite()̀ should look like this:

@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.


Related

Wiki: Checkers
Wiki: Home
Wiki: Instance Providers

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.