Reputation: 663
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}"));
}
}
Upvotes: 0
Views: 1264
Reputation: 116111
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);
}
}
Upvotes: 3
Reputation: 10717
There are many products for mocking/virtualizing services. Including SoapUI and Parasoft Virtualize.
Upvotes: 1