2

I have written a Doclet using Java 9+. I would like to test it using JUnit framework without mocking the Doclet API. Any good way of proceeding?

Abbadon
  • 2,513
  • 3
  • 25
  • 33
  • Split your code into *units* which can be tested individually. That’s the point of unit testing. It’s a tenacious myth that unit testing requires mocking. In fact, unit testing is much older than the mocking hype. – Holger Apr 25 '19 at 14:24
  • Yes. I did that already :). What I wanted was to make a test which uses the real Doclet API. – Abbadon Apr 26 '19 at 17:32
  • Since the tool API does have an abstraction for everything, input, output, filesystem, logging/reporting, options, etc., there’s still no need for mocking, you just need to implement to particular artifacts you want to redirect. – Holger Apr 29 '19 at 06:40

1 Answers1

3

The answer is simple when you know what to use. Here is an example of manual invocation which can be use in a JUnit test.

DocumentationTool systemDocumentationTool = ToolProvider.getSystemDocumentationTool();
        String[] args = new String[] {
          "-sourcepath",
          "./src/test/java",
          "-subpackages",
          "<whatever-package-in-test-sources>",
          "<whatever-package-in-test-sources>",
          "-d",
          "whatever not used just to show compatibility",
          "-author",
          "whatever not used just to show compatibility",
          "-doctitle",
          "whatever not used just to show compatibility",
          "-windowtitle",
          "whatever not used just to show compatibility",
          "-classdir",
          BUILD_PROPERTY_FILE_LOCATION
        };
        DocumentationTool.DocumentationTask task = systemDocumentationTool.getTask(null, null, null,
          <MyDocletClass>.class, Arrays.asList(args), null);

        task.call();
        // Your checks after invocation

Abbadon
  • 2,513
  • 3
  • 25
  • 33