An open API service indexing awesome lists of open source software.

https://github.com/jferard/doctest

A Maven "DocTest for Java" plugin proof of concept
https://github.com/jferard/doctest

doctest java maven-plugin proof-of-concept testing-tools

Last synced: 15 days ago
JSON representation

A Maven "DocTest for Java" plugin proof of concept

Awesome Lists containing this project

README

          

# JavaDocTest

Copyright (C) J. Férard 2018

A "Maven DocTest (see https://docs.python.org/3.7/library/doctest.html) for Java" plugin **proof of concept**.

**Not designed to be used in exploitation.**

**Uses the old com.sun.javadoc API**

## Installation

cd doctest
mvn clean install

## Quick Test

cd DocTestExample # or any project of your own with doctests.
mvn clean com.github.jferard:doctest-maven-plugin:0.0.1-SNAPSHOT:doctest test

Output:

...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.github.jferard.doctest.CalculatorTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

## POM configuration
To execute automatically the plugin:


...

com.github.jferard
doctest-maven-plugin
0.0.1-SNAPSHOT


doctest
generate-test-sources

doctest




Now, a:

mvn clean test

Will generate and execute the test.

*Note: I added an `example` profile with those lines to the POM of `DocTestExample`. You can check it using:*

mvn -P example clean test

## How does it work ?

The `DocTest` plugin:
* add a new test directory source for the generated test class files;
* starts the Maven Javadoc Plugin a custom doclet that extracts the test from the javadoc and builds a test class.

The example class `Calculator` contains the following lines:

/**
*


* {@code
* /// setUp
* Calculator c = new Calculator(0);
* }

*
*

* {@code
* /// imports: java.lang.Math
* /// 1
* Assert.assertEquals(-100, c.subtract(100), 0.01);
* Assert.assertEquals(100, c.negate(), 0.01);
* Assert.assertEquals(377, c.add(277), 0.01);
* Assert.assertEquals(Math.PI, c.divide(120), 0.01);
* }
*

*/
public class Calculator {

...

/**
*

{@code

* /// 2
* /// imports: java.util.Random
* Float f = new Random().nextFloat();
* Assert.assertEquals(f, c.add(f), 0.01);
* }

*
* @param n
* the term to add
* @return the new value
*/
public float add(float n) {
this.value += n;
return this.value;
}
...

The generated test class is:

package com.github.jferard.doctest;

import org.junit.*;
import java.lang.Math;
import java.util.Random;

public class CalculatorTest {
@Test
public void test2() {
Calculator c = new Calculator(0);
Float f = new Random().nextFloat();
Assert.assertEquals(f, c.add(f), 0.01);
}

@Test
public void test1() {
Calculator c = new Calculator(0);
Assert.assertEquals(-100, c.subtract(100), 0.01);
Assert.assertEquals(100, c.negate(), 0.01);
Assert.assertEquals(377, c.add(277), 0.01);
Assert.assertEquals(Math.PI, c.divide(120), 0.01);
}

}

## POM
To use automatically Since the heart of the Java DocTest is a simple doclet, you can add those plugins to the pom:



...

com.github.jferard
doctest-maven-plugin
0.0.1-SNAPSHOT


doctest
generate-test-sources

doctest