Skip to content

Commit ecd928a

Browse files
authored
Donate 'feign-mock' to 'feign' (OpenFeign#692)
* Copy of feign-mock from git@github.com:velo/feign-mock.git * Fix license headers, remove .tool files and make code java 6 compatible * Remove all badges * Update feign GitHub link * Remove link to orignal project
1 parent 94ce071 commit ecd928a

13 files changed

Lines changed: 1883 additions & 0 deletions

‎mock/README.asciidoc‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# feign-mock
2+
3+
An easy way to test https://github.com/OpenFeign/feign. Since feign stores most of the logic in annotations, this helps to check if the annotations are correct.
4+
5+
The original article is available https://velo.github.io/2016/06/05/Testing-feign-clients.html[here]
6+
7+
If mocking feign clients is easy, testing the logic written in annotations is not!
8+
9+
To check if you are parsing the request/response properly, the only way is firing a real request. Well, that doesn't seem to be a good path to unit (or even integration) test remote services. Any IO change will affect test stability.
10+
11+
With feign-mock you can use pre-loaded JSON strings or streams as content for your responses. It also allows you to verify mocked invocations and feign-mock will hit your annotations to make sure everything works.
12+
13+
##### Example
14+
15+
```
16+
private GitHub github;
17+
private MockClient mockClient;
18+
19+
@Before
20+
public void setup() throws IOException {
21+
mockClient = new MockClient()
22+
.noContent(HttpMethod.PATCH, "/repos/velo/feign-mock/contributors");
23+
24+
github = Feign.builder()
25+
.decoder(new GsonDecoder())
26+
.client(mockClient)
27+
.target(new MockTarget<>(GitHub.class));
28+
}
29+
30+
@After
31+
public void tearDown() {
32+
mockClient.verifyStatus();
33+
}
34+
35+
@Test
36+
public void missHttpMethod() {
37+
List<Contributor> result = github.patchContributors("velo", "feign-mock");
38+
assertThat(result, nullValue());
39+
mockClient.verifyOne(HttpMethod.PATCH, "/repos/velo/feign-mock/contributors");
40+
}
41+
```
42+
43+
This simple test returns no content and verifies that the URL was truly invoked.
44+
45+
On the mocked client, you can include all URLs and methods you want to mock.
46+
47+
For more comprehensive examples take a look at https://github.com/OpenFeign/feign/blob/master/mock/src/test/java/feign/mock/MockClientTest.java[MockClientTest].

‎mock/pom.xml‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2018 The Feign Authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7+
in compliance with the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software distributed under the License
12+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
or implied. See the License for the specific language governing permissions and limitations under
14+
the License.
15+
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
20+
<parent>
21+
<groupId>io.github.openfeign</groupId>
22+
<artifactId>parent</artifactId>
23+
<version>9.7.0-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>feign-mock</artifactId>
27+
<name>Feign Mock</name>
28+
<description>Feign Mock</description>
29+
30+
<properties>
31+
<main.basedir>${project.basedir}/..</main.basedir>
32+
33+
<hamcrest.version>1.3</hamcrest.version>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>${project.groupId}</groupId>
39+
<artifactId>feign-core</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>${project.groupId}</groupId>
44+
<artifactId>feign-gson</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.hamcrest</groupId>
49+
<artifactId>hamcrest-core</artifactId>
50+
<version>${hamcrest.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.hamcrest</groupId>
55+
<artifactId>hamcrest-library</artifactId>
56+
<version>${hamcrest.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2012-2018 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.mock;
15+
16+
public enum HttpMethod {
17+
18+
GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
19+
20+
}

0 commit comments

Comments
 (0)