0

I am able to generate restdocs for rest services which is created by me, but unable to generate docs for services which i am consuming.

Is there any way to test and generate docs for third party API.

Sample code which i am using to generate to docs for local services.

@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = RestdocApplication.class)
public class CountryDocumentation {
private static final Logger logger = 
LoggerFactory.getLogger(CountryDocumentation.class);

private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;

@Rule
public final JUnitRestDocumentation restDocumentation = new 
JUnitRestDocumentation("target/generated-snippets");

@Mock
private CountryService countryService;

@Mock
private RestTemplate restTemplate;

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)
            .uris().withHost("X.X.X.X").withPort(9090).and().operationPreprocessors()
            .withResponseDefaults(prettyPrint())
            .withRequestDefaults(prettyPrint())).defaultRequest(get("/")).build();
}

@Test
public void getCountryDefinition() throws Exception {
    this.mockMvc.perform(get("/"))
            .andExpect(status().is(200))
            .andDo(document("{ClassName}/{methodName}"));
}
}
Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
Vikash
  • 643
  • 7
  • 13
  • This is something relates my requirement but not giving my solutions. https://github.com/spring-projects/spring-restdocs/issues/293 – Vikash Aug 16 '18 at 14:59

2 Answers2

3

You've said in a comment that you want to mock the actual call to the remote service. I would argue that makes the documentation pointless. If your tests that generate the documentation are calling a mocked service, you're documenting the mock not the service. If you want the benefits of REST Docs' test-driven approach to documentation generation, your tests need to call the service that is being documented. If the service is only remotely accessible then you'll need to make HTTP calls to document it.

You can use Spring REST Docs with REST Assured or WebTestClient to document any service that's accessible via HTTP. Here's an example with REST Assured that documents part of Stack Exchange's API:

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.springframework.restdocs.JUnitRestDocumentation;

import static io.restassured.RestAssured.given;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.documentationConfiguration;

public class RestAssuredExampleTests {

    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();

    private RequestSpecification documentationSpec;

    @Before
    public void setUp() {
        this.documentationSpec = new RequestSpecBuilder()
                .addFilter(documentationConfiguration(this.restDocumentation))
                .setBaseUri("https://api.stackexchange.com/2.2").build();
    }

    @Test
    public void answers() throws Exception {
        given(this.documentationSpec).accept(ContentType.JSON).filter(document("answers"))
                .when().get("answers?order=desc&sort=activity&site=stackoverflow").then()
                .assertThat().statusCode(200);
    }

}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • I am agree with your answer, and also i have used the same (REST Assured) to document the remote service. But i just wanted to know is there any way to mock remote service when we are testing the remote service let's say in production environment. – Vikash Aug 17 '18 at 06:45
1

There are many products for mocking/virtualizing services. Including SoapUI and Parasoft Virtualize.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • I know many others tools like swagger, RAML etc, but can do the same through restdocs. Please suggest – Vikash Aug 16 '18 at 13:45
  • Neither swagger nor RAML are virtualizing tools. – Andres Aug 16 '18 at 13:47
  • Neither is REST docs. This tool is for documenting services, not for creating a virtual service. – Andres Aug 16 '18 at 13:48
  • Exactly, i want to document the services, not to create virtual services but i want to mock the actual call. – Vikash Aug 16 '18 at 14:51
  • This is something relates my requirement but not giving my solutions. https://github.com/spring-projects/spring-restdocs/issues/293 – Vikash Aug 16 '18 at 14:56