W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
當您想使用Spring REST Docs包含API的請求和響應時,如果使用MockMvc和RestAssuredMockMvc,則只需對設(shè)置進行一些細微更改。如果還沒有,只需包括以下依賴項。
Maven.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-contract-verifier</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.restdocs</groupId> <artifactId>spring-restdocs-mockmvc</artifactId> <optional>true</optional> </dependency>
Gradle.
testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier' testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
接下來,您需要對基類進行一些更改,例如以下示例。
package com.example.fraud; import io.restassured.module.mockmvc.RestAssuredMockMvc; import org.junit.Before; import org.junit.Rule; import org.junit.rules.TestName; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.restdocs.JUnitRestDocumentation; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public abstract class FraudBaseWithWebAppSetup { private static final String OUTPUT = "target/generated-snippets"; @Rule public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(OUTPUT); @Rule public TestName testName = new TestName(); @Autowired private WebApplicationContext context; @Before public void setup() { RestAssuredMockMvc.mockMvc(MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)) .alwaysDo(document( getClass().getSimpleName() + "_" + testName.getMethodName())) .build()); } protected void assertThatRejectionReasonIsNull(Object rejectionReason) { assert rejectionReason == null; } }
如果您使用獨立安裝程序,則可以這樣設(shè)置RestAssuredMockMvc:
package com.example.fraud; import io.restassured.module.mockmvc.RestAssuredMockMvc; import org.junit.Before; import org.junit.Rule; import org.junit.rules.TestName; import org.springframework.restdocs.JUnitRestDocumentation; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; public abstract class FraudBaseWithStandaloneSetup { private static final String OUTPUT = "target/generated-snippets"; @Rule public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(OUTPUT); @Rule public TestName testName = new TestName(); @Before public void setup() { RestAssuredMockMvc.standaloneSetup(MockMvcBuilders .standaloneSetup(new FraudDetectionController()) .apply(documentationConfiguration(this.restDocumentation)) .alwaysDo(document( getClass().getSimpleName() + "_" + testName.getMethodName()))); } }
從Spring REST Docs的1.2.0.RELEASE版本開始,您無需為生成的代碼片段指定輸出目錄。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: