简体   繁体   English

如何模拟公共API(第三方API)以生成spring restdocs

[英]How to mock public API (Third Party API) to generate spring restdocs

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. 我能够为我创建的rest服务生成restdocs,但是无法为我正在使用的服务生成docs。

Is there any way to test and generate docs for third party API. 有什么方法可以测试和生成第三方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}"));
}
}

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. 如果您希望使用REST Docs的测试驱动方法来生成文档,则您的测试需要调用正在记录的服务。 If the service is only remotely accessible then you'll need to make HTTP calls to document it. 如果该服务仅可远程访问,则需要进行HTTP调用以对其进行记录。

You can use Spring REST Docs with REST Assured or WebTestClient to document any service that's accessible via HTTP. 您可以将Spring REST Docs与REST Assured或WebTestClient一起使用,以记录可通过HTTP访问的任何服务。 Here's an example with REST Assured that documents part of Stack Exchange's API: 这是REST Assured的示例,该示例记录了Stack Exchange 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);
    }

}

There are many products for mocking/virtualizing services. 有许多用于模拟/虚拟化服务的产品。 Including SoapUI and Parasoft Virtualize . 包括SoapUIParasoft Virtualize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM