Skip to content

Commit 74b9a73

Browse files
authored
[Proposal] generate mocked clients from feign interfaces (OpenFeign#1092)
* First attempt at test stub code generation * Using templating framework * Cleanup dependencies * Annotate generated classes as experimental
1 parent 73164e3 commit 74b9a73

11 files changed

Lines changed: 718 additions & 4 deletions

File tree

apt-test-generator/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Feign APT test generator
2+
This module generates mock clients for tests based on feign interfaces
3+
4+
## Usage
5+
6+
Just need to add this module to dependency list and Java [Annotation Processing Tool](https://docs.oracle.com/javase/7/docs/technotes/guides/apt/GettingStarted.html) will automatically pick up the jar and generate test clients.
7+
8+
There are 2 main alternatives to include this to a project:
9+
10+
1. Just add to classpath and java compiler should automaticaly detect and run code generation. On maven this is done like this:
11+
12+
```xml
13+
<dependency>
14+
<groupId>io.github.openfeign.experimental</groupId>
15+
<artifactId>feign-apt-test-generator</artifactId>
16+
<version>${feign.version}</version>
17+
<scope>test</scope>
18+
</dependency>
19+
```
20+
21+
1. Use a purpose build tool that allow to pick output location and don't mix dependencies onto classpath
22+
23+
```xml
24+
<plugin>
25+
<groupId>com.mysema.maven</groupId>
26+
<artifactId>apt-maven-plugin</artifactId>
27+
<version>1.1.3</version>
28+
<executions>
29+
<execution>
30+
<goals>
31+
<goal>process</goal>
32+
</goals>
33+
<configuration>
34+
<outputDirectory>target/generated-test-sources/feign</outputDirectory>
35+
<processor>feign.apttestgenerator.GenerateTestStubAPT</processor>
36+
</configuration>
37+
</execution>
38+
</executions>
39+
<dependencies>
40+
<dependency>
41+
<groupId>io.github.openfeign.experimental</groupId>
42+
<artifactId>feign-apt-test-generator</artifactId>
43+
<version>${feign.version}</version>
44+
</dependency>
45+
</dependencies>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.codehaus.mojo</groupId>
49+
<artifactId>build-helper-maven-plugin</artifactId>
50+
<version>3.0.0</version>
51+
<executions>
52+
<execution>
53+
<id>feign-stubs-source</id>
54+
<phase>generate-test-sources</phase>
55+
<goals>
56+
<goal>add-test-source</goal>
57+
</goals>
58+
<configuration>
59+
<sources>
60+
<source>target/generated-test-sources/feign</source>
61+
</sources>
62+
</configuration>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
```

apt-test-generator/pom.xml

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2019 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>10.5.2-SNAPSHOT</version>
24+
</parent>
25+
26+
<groupId>io.github.openfeign.experimental</groupId>
27+
<artifactId>feign-apt-test-generator</artifactId>
28+
<name>Feign APT test generator</name>
29+
<description>Feign code generation tool for mocked clients</description>
30+
31+
<properties>
32+
<main.basedir>${project.basedir}/..</main.basedir>
33+
</properties>
34+
35+
<dependencyManagement>
36+
<dependencies>
37+
<dependency>
38+
<groupId>io.github.openfeign</groupId>
39+
<artifactId>feign-bom</artifactId>
40+
<version>${project.version}</version>
41+
<type>pom</type>
42+
<scope>import</scope>
43+
</dependency>
44+
</dependencies>
45+
</dependencyManagement>
46+
47+
<dependencies>
48+
<dependency>
49+
<groupId>com.github.jknack</groupId>
50+
<artifactId>handlebars</artifactId>
51+
<version>4.1.2</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>io.github.openfeign</groupId>
56+
<artifactId>feign-example-github</artifactId>
57+
<version>${project.version}</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>com.google.testing.compile</groupId>
62+
<artifactId>compile-testing</artifactId>
63+
<version>0.18</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.guava</groupId>
68+
<artifactId>guava</artifactId>
69+
<version>28.0-jre</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.google.auto.service</groupId>
73+
<artifactId>auto-service</artifactId>
74+
<version>1.0-rc5</version>
75+
<scope>provided</scope>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<resources>
81+
<resource>
82+
<targetPath>docker</targetPath>
83+
<filtering>true</filtering>
84+
<!-- Replace maven properties in the docker file so we can get artifacts etc -->
85+
<directory>${project.basedir}/docker</directory>
86+
</resource>
87+
<!-- need to manually specify the resources to copy because we have a manual setting above -->
88+
<resource>
89+
<directory>${basedir}/src/main/resources</directory>
90+
</resource>
91+
<resource>
92+
<directory>src/main/java</directory>
93+
<includes>
94+
<include>**/*.java</include>
95+
</includes>
96+
</resource>
97+
</resources>
98+
99+
<plugins>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-shade-plugin</artifactId>
103+
<version>2.4.3</version>
104+
<executions>
105+
<execution>
106+
<phase>package</phase>
107+
<goals>
108+
<goal>shade</goal>
109+
</goals>
110+
<configuration>
111+
<transformers>
112+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
113+
<mainClass>feign.aptgenerator.github.GitHubFactoryExample</mainClass>
114+
</transformer>
115+
</transformers>
116+
<createDependencyReducedPom>false</createDependencyReducedPom>
117+
</configuration>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
<plugin>
122+
<groupId>org.skife.maven</groupId>
123+
<artifactId>really-executable-jar-maven-plugin</artifactId>
124+
<version>1.5.0</version>
125+
<configuration>
126+
<programFile>github</programFile>
127+
</configuration>
128+
<executions>
129+
<execution>
130+
<phase>package</phase>
131+
<goals>
132+
<goal>really-executable-jar</goal>
133+
</goals>
134+
</execution>
135+
</executions>
136+
</plugin>
137+
<plugin>
138+
<groupId>org.apache.maven.plugins</groupId>
139+
<artifactId>maven-failsafe-plugin</artifactId>
140+
<version>${maven-surefire-plugin.version}</version>
141+
<executions>
142+
<execution>
143+
<goals>
144+
<goal>integration-test</goal>
145+
<goal>verify</goal>
146+
</goals>
147+
</execution>
148+
</executions>
149+
</plugin>
150+
151+
<plugin>
152+
<!-- used to create docker images -->
153+
<groupId>com.spotify</groupId>
154+
<artifactId>docker-maven-plugin</artifactId>
155+
<configuration>
156+
<!-- docker file copied here after maven replaces properties -->
157+
<dockerDirectory>${project.build.directory}/classes/docker/</dockerDirectory>
158+
159+
<!-- Pull image before build, otherwise end up with image not found if it was never downloaded before -->
160+
<pullOnBuild>true</pullOnBuild>
161+
162+
<serverId>docker-hub</serverId>
163+
<registryUrl>https://index.docker.io/v1/</registryUrl>
164+
<imageName>feign-apt-generator/test</imageName>
165+
<resources>
166+
<resource>
167+
<targetPath>/</targetPath>
168+
<directory>${project.build.directory}</directory>
169+
<include>${project.artifactId}-${project.version}.jar</include>
170+
</resource>
171+
</resources>
172+
</configuration>
173+
<executions>
174+
<!-- see definition of how this runs above -->
175+
<execution>
176+
<phase>post-integration-test</phase>
177+
<goals>
178+
<goal>build</goal>
179+
</goals>
180+
</execution>
181+
</executions>
182+
</plugin>
183+
</plugins>
184+
</build>
185+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2012-2019 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.apttestgenerator;
15+
16+
public class ArgumentDefinition {
17+
18+
public final String name;
19+
public final String type;
20+
21+
public ArgumentDefinition(String name, String type) {
22+
super();
23+
this.name = name;
24+
this.type = type;
25+
}
26+
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2012-2019 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.apttestgenerator;
15+
16+
public class ClientDefinition {
17+
18+
public final String jpackage;
19+
public final String className;
20+
public final String fullQualifiedName;
21+
22+
public ClientDefinition(String jpackage, String className, String fullQualifiedName) {
23+
super();
24+
this.jpackage = jpackage;
25+
this.className = className;
26+
this.fullQualifiedName = fullQualifiedName;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)