question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document how to use Spring REST Docs with the MockMVC Kotlin DSL

See original GitHub issue

Was trying to implement RestDocs MockMvc in our Kotlin project However noticed that when using the MockMvc Kotlin DSL snippets were not generated e.g.

    fun shouldReturnDefaultMessageKotlinDSL() {
        mockMvc.get("/hello")
        .andExpect {
            status { isOk }
            content {
                string(containsString("Hello, world!"))
            }
        }.andDo {
            print()
            document("home")
        }
    }

However using the java format did correctly generate e.g.

fun shouldReturnDefaultMessage() {
this.mockMvc.perform(get("/hello"))
.andDo(print())
.andExpect(status().isOk)
.andExpect(content().string(containsString("Hello, world!")))
.andDo(document("home"))
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Apr 6, 2020

Thanks for the sample. When using the Kotlin DSL for MockMvc, you need to pass the result handler that is returned from document(…) to handle(ResultHandler):

@Test
fun shouldReturnDefaultMessageKotlinDSL() {
    mockMvc.get("/hello")
    .andExpect {
       status { isOk }
       content {
            string(containsString("Hello, World"))
        }
    }.andDo {
        print()
        handle(document("home"))
    }
}

It’s a shame that for this one piece, the Kotlin DSL isn’t quite as elegant as the Java equivalent. I’m not sure how it could be improved while keeping the existing more elegant access to the built-in result handlers such as print(). @sdeleuze any suggestions here?

I’ll keep this issue open as I think it’s worth documenting the above.

0reactions
sdeleuzecommented, Apr 2, 2021

Sorry for the delay of my response.

It’s a shame that for this one piece, the Kotlin DSL isn’t quite as elegant as the Java equivalent. I’m not sure how it could be improved while keeping the existing more elegant access to the built-in result handlers such as print(). @sdeleuze any suggestions here?

You could probably extend MockMvc DSL in Spring REST Docs with something like:

fun MockMvcResultHandlersDsl.document(identifier: String, vararg snippets: Snippet) {
      handle(MockMvcRestDocumentation.document(identifier, *snippets))
  }

@countableSet Yeah, that’s not great. Unfortunately, the Kotlin DSL hardcodes the use of MockMvcRequestBuilders so that may be the best that can be done at the moment.

@sdeleuze, do you have any recommendations here please?

Let’s maybe discuss that in https://github.com/spring-projects/spring-restdocs/issues/547#issuecomment-812611417.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Document how to use Spring REST Docs with the MockMVC ...
Document how to use Spring REST Docs with the MockMVC Kotlin DSL.
Read more >
spring-projects/spring-restdocs - Gitter
My use case is the following: I'm using swagger2markup to generate the documentation and REST Docs to generate the code snippets; I want...
Read more >
MockMvc Kotlin DSL - Baeldung
In this quick tutorial, we'll have a look at the new Kotlin-specific MockMvc support available in Spring Framework 5.2+.
Read more >
Spring REST Docs
The MockMvc instance is configured by using a MockMvcRestDocumentationConfigurer . You can obtain an instance of this class from the static ...
Read more >
Spring Auto REST Docs - Scalable Capital Engineering
In Spring REST Docs you have to add the documentation for your JSON with a DSL in your test. We moved this documentation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found