Specmock Save

SpecMock provides Mock Server for various specs, offering a lightweight, fast, and easy-to-use experience.

Project README

SpecMock

SpecMock provides Mock Server for various specs, offering a lightweight, fast, and easy-to-use experience.

Maven Central CI codecov License Twitter

Dependency

// build.gradle
testImplementation 'io.specmock:specmock:${specMockVersion}'
        
// build.gradle.kts
testImplementation("io.specmock:specmock:${specMockVersion}")

// pom.xml
<dependency>
    <groupId>io.specmock</groupId>
    <artifactId>specmock</artifactId>
    <version>${specMockVersion}</version>
</dependency>

How to use

SpringWebBind

The following example demonstrates the creation of a mock server based on the @RestController or @FeignClient with Spring Web Bind annotations.

@RestController OR @FeignClient
public class OR interface ExampleApi {
    @RequestMapping(method = RequestMethod.POST, value = "/example", consumes = MediaType.APPLICATION_JSON_VALUE)
    ExampleResponse examplePost(@RequestBody ExampleRequest request);
}

It automatically maps the Path and Method. You simply need to set the data you want to send in the request and the data you expect to receive in response.

@BeforeEach
void setUp() {
    specServer = HttpSpecServer.builder()
                               .port(18080)
                               .spec(HttpSpec.springWebBuilder()
                                    .springWebBind(ExampleApi.class) // Target Spring-Web-Bind class
                                    .exchanges(
                                         HttpExchange.builder()
                                                     .requestObject(new ExampleRequest("REQ")) // Setting request data
                                                     .responseObject(new ExampleResponse("RES")) // Setting response data
                                                     .build()
                                    )
                                    .build()
                               )
                               .build();
    specServer.start();
}

@Test
void example1_POST_requestBody() throws Exception {
    RestTemplate restTemplate = new RestTemplate();
    ExampleResponse response = restTemplate.postForObject("http://localhost:18080/example", new ExampleRequest("REQ"), ExampleResponse.class);

    // Asserting that the response received from the mock server
    // has the expected string value 'RES'
    assertThat(response.getStringValue()).isEqualTo("RES");
}

@AfterEach
void tearDown() {
    specServer.terminate();
}
Open Source Agenda is not affiliated with "Specmock" Project. README Source: specmock/specmock

Open Source Agenda Badge

Open Source Agenda Rating