3

I've a REST service that has an access limit per resource. Let's say that I've the following service:

GET /record/{recordId}

An access count per record is defined and when the limit is reached, it returns an exception.

I wrote an integration test that creates the record with an access limit of 5 and then creates a loop which sends requests via mockMvc.

When I look at the examples in Spring REST Docs, I see that .andDo(...) is added right after the test which creates the snippets for the test. I'm afraid that it'd overwrite the same test's snippets. I want to document that the resource has access limits per resourceId and provide an example when the resource is accessible and when the access limit is reached.

Should I use 2 document ID's for these cases (see below)? Is this the right approach?

@Test
public void testWithLimit(final String recordId, final String value, final int limit) throws Exception {
    for (int i = 0; i < limit; i++) {
        final ResultActions test = mockMvc.perform(get("/record/" + recordId));

        if (i < limit) {
            test.andExpect(status().isFound())
                .andExpect(jsonValue("$.value").exists())
                .andDo(document("resource-accessible"));
        } else {
            test.andExpect(status().isGone())
                .andExpect(validateException(RecordLimitExceededException.class))
                .andDo(document("resource-access-limit-reached"));
        }
    }
}
Alper Kanat
  • 376
  • 1
  • 2
  • 16

1 Answers1

1

Yes, I’d definitely use two different document IDs for these two cases so that you get a set of snippets for both.

It’s not necessary, but you may also want to consider only calling document twice in the loop: once for the first call that will succeed and then once for the final call when you expect the limit to have been reached.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Hi Andy, I'm not able to document the error response as this is a mock mvc environment. I tried with RANDOM_PORT and using TestRestTemplate but in that case document() simply doesn't generate anything. Looks like it requires a mockMvcResult. Any ideas? – Alper Kanat Nov 29 '17 at 17:13
  • You could consider using Spring REST Docs with REST Assured. That allows you to document an API by making HTTP requests to an actual server. – Andy Wilkinson Nov 29 '17 at 21:04
  • How is REST Assured different than using TestRestTemplate? I thought TestRestTemplate was also making requests as well. – Alper Kanat Dec 02 '17 at 19:05
  • REST Docs has support for REST Assured. It does not have support for TestRestTemplate. – Andy Wilkinson Dec 02 '17 at 23:03