Search code examples
javaspring-mvcspring-testasciidoctorspring-restdocs

Setting values of attributes in asciidoctor during Spring's MockMvc building


I'm using Spring MVC and Spring Test suite(Spring REST Docs) and I'm wondering is there a way to set values of attributes in the asciidoctor adoc file during building the MockMvc with MockMvcBuilders ?

For example I have in index.adoc

:myAttribute:

and want to dynamically set it during MockMvc building ?

:myAttribute: myValue

I see in the asciidoctor documentation:

By default, the attribute assignment precedence, from highest to lowest, is as follows:

Attribute passed to the API or CLI

Attribute defined in the document

Default value

And I wonder how to pass it to the API :-)


Solution

  • There are two ways I can think of that you could do this. In both cases you'd write the attribute key-value pairs out into a file when you run your tests. You then need to get them into Asciidoctor.

    One way to get them into Asciidoctor would be to update your build configuration to read in the file and then set the attributes. I suspect this will only be possible if you're using Gradle. You could write a little bit of Groovy to read in the key-value pairs and add them to the attributes map of the asciidoctor task.

    Another way would be to write an AsciidoctorJ extension that reads in the file containing the key-value pairs and sets the attributes. This approach would work with Maven or Gradle. There's an example in the documentation that shows how to use a Preprocessor to set an attribute.