Skip to content

Commit a7a5937

Browse files
[JAVA][FEIGN]Implement unit tests for java-feign client (#8484)
* Implement unit tests for feign client Implement tests Migrate to junit 5 * Default feign client does not support PATCH verb Default feign client does not support PATCH verb * Remove test for GET endpoint with request body * Configure junit in gradle build * Configure logback for unit tests * Add missing dependencies to sbt * Fix gradle dependency * Add logback to gradle unit test * Regenerate samples * Make junit test classes package private * Make junit test classes package private * Update samples * Organize imports * Organize imports
1 parent 90ed129 commit a7a5937

69 files changed

Lines changed: 1351 additions & 1074 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/Java/libraries/feign/ApiClient.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.util.logging.Logger;
88
{{#threetenbp}}
99
import org.threeten.bp.*;
1010
{{/threetenbp}}
11+
import feign.okhttp.OkHttpClient;
1112

1213
import com.fasterxml.jackson.databind.DeserializationFeature;
1314
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -51,6 +52,7 @@ public class ApiClient {
5152
objectMapper = createObjectMapper();
5253
apiAuthorizations = new LinkedHashMap<String, RequestInterceptor>();
5354
feignBuilder = Feign.builder()
55+
.client(new OkHttpClient())
5456
.encoder(new FormEncoder(new JacksonEncoder(objectMapper)))
5557
.decoder(new JacksonDecoder(objectMapper))
5658
.logger(new Slf4jLogger());

modules/openapi-generator/src/main/resources/Java/libraries/feign/api_test.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package {{package}};
33
import {{invokerPackage}}.ApiClient;
44
{{#imports}}import {{import}};
55
{{/imports}}
6-
import org.junit.Before;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.BeforeEach;
88

99
{{^fullJavaUtil}}
1010
import java.util.ArrayList;
@@ -16,11 +16,11 @@ import java.util.Map;
1616
/**
1717
* API tests for {{classname}}
1818
*/
19-
public class {{classname}}Test {
19+
class {{classname}}Test {
2020
2121
private {{classname}} api;
2222

23-
@Before
23+
@BeforeEach
2424
public void setup() {
2525
api = new ApiClient().buildClient({{classname}}.class);
2626
}
@@ -32,7 +32,7 @@ public class {{classname}}Test {
3232
* {{notes}}
3333
*/
3434
@Test
35-
public void {{operationId}}Test() {
35+
void {{operationId}}Test() {
3636
{{#allParams}}
3737
{{{dataType}}} {{paramName}} = null;
3838
{{/allParams}}
@@ -51,7 +51,7 @@ public class {{classname}}Test {
5151
* listing them out individually.
5252
*/
5353
@Test
54-
public void {{operationId}}TestQueryMap() {
54+
void {{operationId}}TestQueryMap() {
5555
{{#allParams}}
5656
{{^isQueryParam}}
5757
{{{dataType}}} {{paramName}} = null;

modules/openapi-generator/src/main/resources/Java/libraries/feign/build.gradle.mustache

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ if(hasProperty('target') && target == 'android') {
9494
}
9595
}
9696

97+
test {
98+
useJUnitPlatform()
99+
}
100+
97101
ext {
98102
swagger_annotations_version = "1.5.24"
99103
jackson_version = "2.10.3"
@@ -106,7 +110,7 @@ ext {
106110
{{/threetenbp}}
107111
feign_version = "10.11"
108112
feign_form_version = "3.8.0"
109-
junit_version = "4.13.1"
113+
junit_version = "5.7.0"
110114
scribejava_version = "8.0.0"
111115
}
112116

@@ -116,6 +120,7 @@ dependencies {
116120
implementation "io.github.openfeign:feign-core:$feign_version"
117121
implementation "io.github.openfeign:feign-jackson:$feign_version"
118122
implementation "io.github.openfeign:feign-slf4j:$feign_version"
123+
implementation "io.github.openfeign:feign-okhttp:$feign_version"
119124
implementation "io.github.openfeign.form:feign-form:$feign_form_version"
120125
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
121126
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
@@ -136,5 +141,11 @@ dependencies {
136141
implementation "com.github.scribejava:scribejava-core:$scribejava_version"
137142
implementation "com.brsanthu:migbase64:2.2"
138143
implementation 'javax.annotation:javax.annotation-api:1.3.2'
139-
testImplementation "junit:junit:$junit_version"
144+
testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
145+
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
146+
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"
147+
testImplementation "com.github.tomakehurst:wiremock-jre8:2.27.2"
148+
testImplementation "org.hamcrest:hamcrest:2.2"
149+
testImplementation "commons-io:commons-io:2.8.0"
150+
testImplementation "ch.qos.logback:logback-classic:1.2.3"
140151
}

modules/openapi-generator/src/main/resources/Java/libraries/feign/build.sbt.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ lazy val root = (project in file(".")).
1010
resolvers += Resolver.mavenLocal,
1111
libraryDependencies ++= Seq(
1212
"io.swagger" % "swagger-annotations" % "1.5.24" % "compile",
13+
"com.google.code.findbugs" % "jsr305" % "3.0.2" % "compile",
1314
"io.github.openfeign" % "feign-core" % "10.11" % "compile",
1415
"io.github.openfeign" % "feign-jackson" % "10.11" % "compile",
1516
"io.github.openfeign" % "feign-slf4j" % "10.11" % "compile",
1617
"io.github.openfeign.form" % "feign-form" % "3.8.0" % "compile",
18+
"io.github.openfeign" % "feign-okhttp" % "10.11" % "compile",
1719
"com.fasterxml.jackson.core" % "jackson-core" % "2.10.3" % "compile",
1820
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.10.3" % "compile",
1921
"com.fasterxml.jackson.core" % "jackson-databind" % "2.10.3" % "compile",
@@ -22,7 +24,11 @@ lazy val root = (project in file(".")).
2224
"com.github.scribejava" % "scribejava-core" % "8.0.0" % "compile",
2325
"com.brsanthu" % "migbase64" % "2.2" % "compile",
2426
"javax.annotation" % "javax.annotation-api" % "1.3.2" % "compile",
25-
"junit" % "junit" % "4.13.1" % "test",
27+
"org.junit.jupiter" % "junit-jupiter" % "5.7.0" % "test",
28+
"org.junit.jupiter" % "junit-jupiter-params" % "5.7.0" % "test",
29+
"com.github.tomakehurst" % "wiremock-jre8" % "2.27.2" % "test",
30+
"org.hamcrest" % "hamcrest" % "2.2" % "test",
31+
"commons-io" % "commons-io" % "2.8.0" % "test",
2632
"com.novocode" % "junit-interface" % "0.10" % "test"
2733
)
2834
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{{>licenseInfo}}
2+
3+
package {{package}};
4+
5+
{{#imports}}import {{import}};
6+
{{/imports}}
7+
import org.junit.jupiter.api.Test;
8+
9+
{{#fullJavaUtil}}
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
{{/fullJavaUtil}}
15+
16+
/**
17+
* Model tests for {{classname}}
18+
*/
19+
class {{classname}}Test {
20+
{{#models}}
21+
{{#model}}
22+
{{^vendorExtensions.x-is-one-of-interface}}
23+
{{^isEnum}}
24+
private final {{classname}} model = new {{classname}}();
25+
26+
{{/isEnum}}
27+
/**
28+
* Model tests for {{classname}}
29+
*/
30+
@Test
31+
void test{{classname}}() {
32+
// TODO: test {{classname}}
33+
}
34+
35+
{{#allVars}}
36+
/**
37+
* Test the property '{{name}}'
38+
*/
39+
@Test
40+
void {{name}}Test() {
41+
// TODO: test {{name}}
42+
}
43+
44+
{{/allVars}}
45+
{{/vendorExtensions.x-is-one-of-interface}}
46+
{{/model}}
47+
{{/models}}
48+
}

modules/openapi-generator/src/main/resources/Java/libraries/feign/pom.mustache

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@
247247
<artifactId>feign-form</artifactId>
248248
<version>${feign-form-version}</version>
249249
</dependency>
250+
<dependency>
251+
<groupId>io.github.openfeign</groupId>
252+
<artifactId>feign-okhttp</artifactId>
253+
<version>${feign-version}</version>
254+
</dependency>
250255

251256
<!-- JSON processing: jackson -->
252257
<dependency>
@@ -314,21 +319,39 @@
314319

315320
<!-- test dependencies -->
316321
<dependency>
317-
<groupId>junit</groupId>
318-
<artifactId>junit</artifactId>
322+
<groupId>ch.qos.logback</groupId>
323+
<artifactId>logback-classic</artifactId>
324+
<version>1.2.3</version>
325+
<scope>test</scope>
326+
</dependency>
327+
<dependency>
328+
<groupId>org.junit.jupiter</groupId>
329+
<artifactId>junit-jupiter</artifactId>
319330
<version>${junit-version}</version>
320331
<scope>test</scope>
321332
</dependency>
322333
<dependency>
323-
<groupId>com.squareup.okhttp3</groupId>
324-
<artifactId>mockwebserver</artifactId>
325-
<version>3.6.0</version>
334+
<groupId>org.junit.jupiter</groupId>
335+
<artifactId>junit-jupiter-params</artifactId>
336+
<version>${junit-version}</version>
337+
<scope>test</scope>
338+
</dependency>
339+
<dependency>
340+
<groupId>org.hamcrest</groupId>
341+
<artifactId>hamcrest</artifactId>
342+
<version>2.2</version>
343+
<scope>test</scope>
344+
</dependency>
345+
<dependency>
346+
<groupId>com.github.tomakehurst</groupId>
347+
<artifactId>wiremock-jre8</artifactId>
348+
<version>2.27.2</version>
326349
<scope>test</scope>
327350
</dependency>
328351
<dependency>
329-
<groupId>org.assertj</groupId>
330-
<artifactId>assertj-core</artifactId>
331-
<version>1.7.1</version>
352+
<groupId>commons-io</groupId>
353+
<artifactId>commons-io</artifactId>
354+
<version>2.8.0</version>
332355
<scope>test</scope>
333356
</dependency>
334357
</dependencies>
@@ -349,7 +372,7 @@
349372
<jackson-threetenbp-version>2.9.10</jackson-threetenbp-version>
350373
{{/threetenbp}}
351374
<javax-annotation-version>1.3.2</javax-annotation-version>
352-
<junit-version>4.13.1</junit-version>
375+
<junit-version>5.7.0</junit-version>
353376
<maven-plugin-version>1.0.0</maven-plugin-version>
354377
<scribejava-version>8.0.0</scribejava-version>
355378
</properties>

samples/client/petstore/java/feign-no-nullable/build.gradle

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,18 @@ if(hasProperty('target') && target == 'android') {
9494
}
9595
}
9696

97+
test {
98+
useJUnitPlatform()
99+
}
100+
97101
ext {
98102
swagger_annotations_version = "1.5.24"
99103
jackson_version = "2.10.3"
100104
jackson_databind_version = "2.10.3"
101105
jackson_threetenbp_version = "2.9.10"
102106
feign_version = "10.11"
103107
feign_form_version = "3.8.0"
104-
junit_version = "4.13.1"
108+
junit_version = "5.7.0"
105109
scribejava_version = "8.0.0"
106110
}
107111

@@ -111,6 +115,7 @@ dependencies {
111115
implementation "io.github.openfeign:feign-core:$feign_version"
112116
implementation "io.github.openfeign:feign-jackson:$feign_version"
113117
implementation "io.github.openfeign:feign-slf4j:$feign_version"
118+
implementation "io.github.openfeign:feign-okhttp:$feign_version"
114119
implementation "io.github.openfeign.form:feign-form:$feign_form_version"
115120
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
116121
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
@@ -120,5 +125,11 @@ dependencies {
120125
implementation "com.github.scribejava:scribejava-core:$scribejava_version"
121126
implementation "com.brsanthu:migbase64:2.2"
122127
implementation 'javax.annotation:javax.annotation-api:1.3.2'
123-
testImplementation "junit:junit:$junit_version"
128+
testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
129+
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
130+
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"
131+
testImplementation "com.github.tomakehurst:wiremock-jre8:2.27.2"
132+
testImplementation "org.hamcrest:hamcrest:2.2"
133+
testImplementation "commons-io:commons-io:2.8.0"
134+
testImplementation "ch.qos.logback:logback-classic:1.2.3"
124135
}

samples/client/petstore/java/feign-no-nullable/build.sbt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ lazy val root = (project in file(".")).
1010
resolvers += Resolver.mavenLocal,
1111
libraryDependencies ++= Seq(
1212
"io.swagger" % "swagger-annotations" % "1.5.24" % "compile",
13+
"com.google.code.findbugs" % "jsr305" % "3.0.2" % "compile",
1314
"io.github.openfeign" % "feign-core" % "10.11" % "compile",
1415
"io.github.openfeign" % "feign-jackson" % "10.11" % "compile",
1516
"io.github.openfeign" % "feign-slf4j" % "10.11" % "compile",
1617
"io.github.openfeign.form" % "feign-form" % "3.8.0" % "compile",
18+
"io.github.openfeign" % "feign-okhttp" % "10.11" % "compile",
1719
"com.fasterxml.jackson.core" % "jackson-core" % "2.10.3" % "compile",
1820
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.10.3" % "compile",
1921
"com.fasterxml.jackson.core" % "jackson-databind" % "2.10.3" % "compile",
@@ -22,7 +24,11 @@ lazy val root = (project in file(".")).
2224
"com.github.scribejava" % "scribejava-core" % "8.0.0" % "compile",
2325
"com.brsanthu" % "migbase64" % "2.2" % "compile",
2426
"javax.annotation" % "javax.annotation-api" % "1.3.2" % "compile",
25-
"junit" % "junit" % "4.13.1" % "test",
27+
"org.junit.jupiter" % "junit-jupiter" % "5.7.0" % "test",
28+
"org.junit.jupiter" % "junit-jupiter-params" % "5.7.0" % "test",
29+
"com.github.tomakehurst" % "wiremock-jre8" % "2.27.2" % "test",
30+
"org.hamcrest" % "hamcrest" % "2.2" % "test",
31+
"commons-io" % "commons-io" % "2.8.0" % "test",
2632
"com.novocode" % "junit-interface" % "0.10" % "test"
2733
)
2834
)

samples/client/petstore/java/feign-no-nullable/pom.xml

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@
240240
<artifactId>feign-form</artifactId>
241241
<version>${feign-form-version}</version>
242242
</dependency>
243+
<dependency>
244+
<groupId>io.github.openfeign</groupId>
245+
<artifactId>feign-okhttp</artifactId>
246+
<version>${feign-version}</version>
247+
</dependency>
243248

244249
<!-- JSON processing: jackson -->
245250
<dependency>
@@ -276,21 +281,39 @@
276281

277282
<!-- test dependencies -->
278283
<dependency>
279-
<groupId>junit</groupId>
280-
<artifactId>junit</artifactId>
284+
<groupId>ch.qos.logback</groupId>
285+
<artifactId>logback-classic</artifactId>
286+
<version>1.2.3</version>
287+
<scope>test</scope>
288+
</dependency>
289+
<dependency>
290+
<groupId>org.junit.jupiter</groupId>
291+
<artifactId>junit-jupiter</artifactId>
281292
<version>${junit-version}</version>
282293
<scope>test</scope>
283294
</dependency>
284295
<dependency>
285-
<groupId>com.squareup.okhttp3</groupId>
286-
<artifactId>mockwebserver</artifactId>
287-
<version>3.6.0</version>
296+
<groupId>org.junit.jupiter</groupId>
297+
<artifactId>junit-jupiter-params</artifactId>
298+
<version>${junit-version}</version>
299+
<scope>test</scope>
300+
</dependency>
301+
<dependency>
302+
<groupId>org.hamcrest</groupId>
303+
<artifactId>hamcrest</artifactId>
304+
<version>2.2</version>
305+
<scope>test</scope>
306+
</dependency>
307+
<dependency>
308+
<groupId>com.github.tomakehurst</groupId>
309+
<artifactId>wiremock-jre8</artifactId>
310+
<version>2.27.2</version>
288311
<scope>test</scope>
289312
</dependency>
290313
<dependency>
291-
<groupId>org.assertj</groupId>
292-
<artifactId>assertj-core</artifactId>
293-
<version>1.7.1</version>
314+
<groupId>commons-io</groupId>
315+
<artifactId>commons-io</artifactId>
316+
<version>2.8.0</version>
294317
<scope>test</scope>
295318
</dependency>
296319
</dependencies>
@@ -306,7 +329,7 @@
306329
<jackson-databind-version>2.10.3</jackson-databind-version>
307330
<jackson-threetenbp-version>2.9.10</jackson-threetenbp-version>
308331
<javax-annotation-version>1.3.2</javax-annotation-version>
309-
<junit-version>4.13.1</junit-version>
332+
<junit-version>5.7.0</junit-version>
310333
<maven-plugin-version>1.0.0</maven-plugin-version>
311334
<scribejava-version>8.0.0</scribejava-version>
312335
</properties>

samples/client/petstore/java/feign-no-nullable/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.logging.Logger;
77

88
import org.threeten.bp.*;
9+
import feign.okhttp.OkHttpClient;
910

1011
import com.fasterxml.jackson.databind.DeserializationFeature;
1112
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -36,6 +37,7 @@ public ApiClient() {
3637
objectMapper = createObjectMapper();
3738
apiAuthorizations = new LinkedHashMap<String, RequestInterceptor>();
3839
feignBuilder = Feign.builder()
40+
.client(new OkHttpClient())
3941
.encoder(new FormEncoder(new JacksonEncoder(objectMapper)))
4042
.decoder(new JacksonDecoder(objectMapper))
4143
.logger(new Slf4jLogger());

0 commit comments

Comments
 (0)