{"id":18141564,"url":"https://github.com/skjolber/mockito-rest-spring","last_synced_at":"2025-04-22T13:18:25.809Z","repository":{"id":17251515,"uuid":"80772322","full_name":"skjolber/mockito-rest-spring","owner":"skjolber","description":"REST web-service mocking utility for Spring","archived":false,"fork":false,"pushed_at":"2025-03-17T17:03:11.000Z","size":302,"stargazers_count":6,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-22T13:18:12.214Z","etag":null,"topics":["integration","integration-testing","json","junit-rule","mockito","rest","spring","test","webservices"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skjolber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-02T21:53:42.000Z","updated_at":"2025-01-11T15:58:16.000Z","dependencies_parsed_at":"2025-01-06T23:28:33.622Z","dependency_job_id":"795e55db-b961-47e0-be8c-233c6c6780ae","html_url":"https://github.com/skjolber/mockito-rest-spring","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skjolber%2Fmockito-rest-spring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skjolber%2Fmockito-rest-spring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skjolber%2Fmockito-rest-spring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skjolber%2Fmockito-rest-spring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skjolber","download_url":"https://codeload.github.com/skjolber/mockito-rest-spring/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250246728,"owners_count":21398919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["integration","integration-testing","json","junit-rule","mockito","rest","spring","test","webservices"],"created_at":"2024-11-01T17:07:37.499Z","updated_at":"2025-04-22T13:18:25.792Z","avatar_url":"https://github.com/skjolber.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Build Status](https://github.com/skjolber/mockito-rest-spring/actions/workflows/maven.yml/badge.svg) \n[![Maven Central](https://img.shields.io/maven-central/v/com.github.skjolber.mockito-rest-spring/core.svg)](https://mvnrepository.com/artifact/com.github.skjolber.mockito-rest-spring)\n\n# mockito-rest-spring\nThis utility supports __high-level unit testing__ for applications which consume external HTTP services which can be defined using Spring-flavoured REST. \n\nIn a nutshell, mocking external REST services becomes __as simple as mocking any other bean__ using [Mockito].\n\nUsers will benefit from\n\n  * full-stack __integration-style unit testing__ - over-the-wire mocking on local ports. \n  * [Mockito] support - i.e. full method/type safety\n  * simple setup using [JUnit 5](junit5) `@Extension`\n  * Tomcat, Jetty \u0026 Undertow support\n\nThe target API must be available either in the form of an annotated interface or a concrete implementation at compile time. \n\nWhen working with OpenAPI definitions this usually means running the code generator two times:\n\n * model + client (main scope), and\n * to-be-mocked server (test scope).\n\nBugs, feature suggestions and help requests can be filed with the [issue-tracker].\n\n## License\n[Apache 2.0]\n\n## Obtain\nThe project is built with [Maven] and is available on the central Maven repository. \n\n\u003cdetails\u003e\n  \u003csummary\u003eMaven coordinates\u003c/summary\u003e\n\nAdd the property\n```xml\n\u003cmockito-rest-spring.version\u003e2.0.x\u003c/mockito-rest-spring.version\u003e\n```\n\nthen add for Tomcat\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.skjolber.mockito-rest-spring\u003c/groupId\u003e\n    \u003cartifactId\u003ejunit5-tomcat\u003c/artifactId\u003e\n    \u003cversion\u003e${mockito-rest-spring.version}\u003c/version\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\nor Undertow\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.skjolber.mockito-rest-spring\u003c/groupId\u003e\n    \u003cartifactId\u003ejunit5-undertow\u003c/artifactId\u003e\n    \u003cversion\u003e${mockito-rest-spring.version}\u003c/version\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\nor Jetty\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.skjolber.mockito-rest-spring\u003c/groupId\u003e\n    \u003cartifactId\u003ejunit5-jetty\u003c/artifactId\u003e\n    \u003cversion\u003e${mockito-rest-spring.version}\u003c/version\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n\u003c/details\u003e\n\nor\n\n\u003cdetails\u003e\n  \u003csummary\u003eGradle coordinates\u003c/summary\u003e\n\nFor\n\n```groovy\next {\n  mockitoRestSpringVersion = '2.0.x'\n}\n```\n\nadd for Tomcat\n\n```groovy\napi(\"com.github.skjolber.mockito-rest-spring:junit5-tomcat:${mockitoRestSpringVersion}\")\n```\n\nor Undertow,\n\n```groovy\napi(\"com.github.skjolber.mockito-rest-spring:junit5-undertow:${mockitoRestSpringVersion}\")\n```\n\n\nor Jetty\n\n```groovy\napi(\"com.github.skjolber.mockito-rest-spring:junit5-jetty:${mockitoRestSpringVersion}\")\n```\n\u003c/details\u003e\n\n# Usage\nIf you prefer skipping to a full example, see \n\n * [REST service unit test](examples/demo/src/test/java/com/example/demo/DemoApplication1Test.java)\n * [GraphQL service unit test](examples/graphql-demo/src/test/java/com/example/demo/GraphQLTest.java) example, featuring [mockito-graphql-matchers](https://github.com/skjolber/mockito-graphql-matchers).\n\n# Basics\nIn your JUnit test, add a `MockitoEndpointExtension` extension:\n\n```java\n@ExtendWith(MockitoEndpointExtension.class)\n```\n\nbefore (above) any `SpringExtension` or `@SpringBootTest`. Then mock service endpoints by using\n\n```java\n@MockEndpoint\nprivate MyRestService myRestService;\n```\n\nwhere the `MyRestService` is either an interface or a concrete `@RestController` implementation. For a custom (or missing) class level [RequestMapping] add a `path` parameter:\n\n```java\n@MockEndpoint(path = \"/rest\")\nprivate MyRestService myRestService;\n```\n\nThe returned `serviceMock` instance is a normal [Mockito] mock(..) object. \n\n### Client configuration\nThe mock endpoint is started on a random free local port and saved to  `System` property `mockitoRestSpringServerPort`. \n\nConfigure your client to pick up this value, for example via regular properties in Spring:\n\n```\nmy.server.url=http://localhost:${mockitoRestSpringServerPort}/rest/pet\n```\n\n# Details\nCreate mock response via code\n\n```java\n// init response\nMyResponse expected = new MyResponse();\nexpected.setCode(0);\nexpected.setValue(\"abc\");\n```\n\nor from JSON\n\n```java\nMyResponse response = jsonUtil.readResource(\"/example/MyResponse1.xml\", MyResponse.class);\n```\n\nusing your favorite JSON utility. Then mock\n\n```java\nwhen(myRestService.method3(any(MyRequest.class)).thenReturn(expected);\n```\n\nor with a `ResponseEntity` wrapper (via OpenAPI `useResponseEntity` parameter):\n\n```java\nwhen(myRestService.method3(any(MyRequest.class)).thenReturn(new ResponseEntity\u003c\u003e(expected, HttpStatus.OK));\n```\n\nand apply standard Mockito test approach. \n\n## Verifing calls\nAfter triggering calls to the mock service, verify number of method calls\n\n```java\nArgumentCaptor\u003cMyRequest\u003e argument1 = ArgumentCaptor.forClass(MyRequest.class);\nverify(myRestService, times(1)).method3(argument1.capture());\n```\n\nand request details\n\n```java\nMyRequest request = argument1.getValue();\nassertThat(request.getCode(), is(1));\n```\n\n## Mocking GraphQL\nManually add the API interface\n\n```\nimport org.springframework.http.HttpStatus;\nimport org.springframework.web.bind.annotation.RequestBody;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RequestMethod;\nimport org.springframework.web.server.ResponseStatusException;\n\npublic interface MyGraphQLApi {\n\n    @RequestMapping(\n        method = RequestMethod.POST,\n        value = \"/graphql\",\n        produces = { \"application/json\" },\n        consumes = { \"application/json\"}\n    )\n    default String request(@RequestBody String graphQL) throws ResponseStatusException {\n    \tthrow new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED);\n    }\n}\n```\n\nand work with String request/response. Match requests using [mockito-graphql-matchers](https://github.com/skjolber/mockito-graphql-matchers) or equivalent.\n\n# Alternatives\nYou might supplement your testing using the following more low-level mocking projects: \n\n   * [Spring Mock MVC]\n   * [WireMock]\n   * [grpcmock]\n\n# History\n\n - 2.0.3: Dependency updates\n - 2.0.2: Dependency updates\n - 2.0.1: Fix Tomcat temporary folder \n - 2.0.0: Update to latest Spring, drop JUnit 4 support.\n - 1.0.3: JUnit 5 support for Tomcat, Jetty and Undertow.\n - 1.0.2: Improved JAXB helper, fix artifact id. \n - 1.0.1: Support for API interfaces, including [Swagger]-generated stubs. See [this unit test](src/test/java/com/github/skjolber/mockito/rest/spring/RestServiceRuleInterfaceTest.java).\n - 1.0.0: Initial version\n\n[Apache 2.0]:          \thttp://www.apache.org/licenses/LICENSE-2.0.html\n[issue-tracker]:       \thttps://github.com/skjolber/mockito-rest-spring/issues\n[Maven]:                http://maven.apache.org/\n[WireMock]:             http://wiremock.org/\n[Spring Mock MVC]:      https://docs.spring.io/spring-framework/reference/testing/spring-mvc-test-framework.html\n[Swagger]:\t\t\t\t          https://github.com/swagger-api/swagger-codegen\n[Mockito]:\t\t\t\t          https://github.com/mockito/mockito\n[RequestMapping]:\t\t     https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html\n[grpcmock]:             https://github.com/Fadelis/grpcmock\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskjolber%2Fmockito-rest-spring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskjolber%2Fmockito-rest-spring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskjolber%2Fmockito-rest-spring/lists"}