@@ -89,6 +89,7 @@ public abstract class AbstractAppTest {
89
89
private HttpHeaders expectedResponseHeaders ;
90
90
private Map <String , String > headerValues ;
91
91
private String testDirectoryPath ;
92
+ private String testCaseName ;
92
93
93
94
@ Autowired
94
95
protected TestRestTemplate restTemplate ;
@@ -114,32 +115,39 @@ public AbstractAppTest reset() {
114
115
this .expectedResponseHeaders = null ;
115
116
this .headerValues = null ;
116
117
this .testDirectoryPath = null ;
118
+ this .testCaseName = null ;
117
119
118
120
return this ;
119
121
}
120
122
121
- public AbstractAppTest setupCall () {
122
-
123
- reset ();
124
- initializeJsonAssert ();
123
+ public AbstractAppTest setupPaths () {
125
124
126
125
// Fetch test case name.
127
- final var testCaseName = getTestMethodName ();
126
+ this . testCaseName = getTestMethodName ();
128
127
129
128
this .mappingPath = wiremock .getOptions ().filesRoot ().getPath ();
130
129
if (!this .mappingPath .endsWith ("/" )) {
131
130
this .mappingPath += "/" ;
132
131
}
133
132
133
+ this .testDirectoryPath = "classpath:" + this .mappingPath + FILES_DIR + getTestMethodName () + System .getProperty ("file.separator" );
134
+
135
+ return this ;
136
+ }
137
+
138
+ public AbstractAppTest setupCall () {
139
+
140
+ reset ();
141
+ initializeJsonAssert ();
142
+ setupPaths ();
143
+
134
144
this .wiremock .loadMappingsUsing (new JsonFileMappingsSource (
135
145
new ClasspathFileSource (this .mappingPath + FILES_DIR + COMMON_MAPPING_DIR + MAPPING_DIRECTORY )));
136
- if (nonNull (testCaseName )) {
146
+ if (nonNull (this . testCaseName )) {
137
147
this .wiremock .loadMappingsUsing (new JsonFileMappingsSource (
138
- new ClasspathFileSource (this .mappingPath + FILES_DIR + testCaseName + MAPPING_DIRECTORY )));
148
+ new ClasspathFileSource (this .mappingPath + FILES_DIR + this . testCaseName + MAPPING_DIRECTORY )));
139
149
}
140
150
141
- this .testDirectoryPath = "classpath:" + this .mappingPath + FILES_DIR + getTestMethodName () + System .getProperty ("file.separator" );
142
-
143
151
return this ;
144
152
}
145
153
@@ -274,6 +282,19 @@ public AbstractAppTest withRequest(final String request) {
274
282
return this ;
275
283
}
276
284
285
+ /**
286
+ * Method takes a file that will be added to the multipart body.
287
+ *
288
+ * @param fileName to be added to the request as a multipart, the method will look for the file in the
289
+ * current
290
+ * test-case directory.
291
+ * @return
292
+ * @throws FileNotFoundException
293
+ */
294
+ public AbstractAppTest withRequestFile (final String parameterName , final String fileName ) throws FileNotFoundException {
295
+ return withRequestFile (parameterName , getFile (this .testDirectoryPath + fileName ));
296
+ }
297
+
277
298
/**
278
299
* Method takes a file that will be added to the multipart body.
279
300
*
@@ -285,23 +306,25 @@ public AbstractAppTest withRequestFile(final String parameterName, final File fi
285
306
this .multipartBody = new LinkedMultiValueMap <>();
286
307
}
287
308
288
- multipartBody .add (parameterName , new FileSystemResource (file ));
309
+ this . multipartBody .add (parameterName , new FileSystemResource (file ));
289
310
290
311
return this ;
291
312
}
292
313
293
314
/**
294
- * Method takes a file that will be added to the multipart body.
315
+ * Method takes a MultiValueMap that will set the multipart body.
316
+ * If you have added any parts to the multipartbody before a call to this method, these parts will be lost.
295
317
*
296
- * @param fileName to be added to the request as a multipart, the method will look for the file in the
297
- * current
298
- * test-case directory.
318
+ * @see org.springframework.http.client.MultipartBodyBuilder for instruction on how to create a
319
+ * suitable MultiValueMap.
320
+ *
321
+ * @param multiPartBody the multipartbody (as a MultiValueMap).
299
322
* @return
300
- * @throws FileNotFoundException
301
323
*/
302
- public AbstractAppTest withRequestFile (final String parameterName , final String fileName ) throws FileNotFoundException {
303
- final var file = getFile (this .testDirectoryPath + fileName );
304
- return withRequestFile (parameterName , file );
324
+ @ SuppressWarnings ("unchecked" )
325
+ public AbstractAppTest withRequest (final MultiValueMap <?, ?> multiPartBody ) {
326
+ this .multipartBody = (MultiValueMap <String , Object >) multiPartBody ;
327
+ return this ;
305
328
}
306
329
307
330
/**
@@ -435,6 +458,10 @@ public AbstractAppTest andVerifyThat(final Callable<Boolean> conditionIsMet) {
435
458
return this ;
436
459
}
437
460
461
+ public String getTestDirectoryPath () {
462
+ return this .testDirectoryPath ;
463
+ }
464
+
438
465
private HttpEntity <Object > restTemplateRequest (final MediaType mediaType , Object body ) {
439
466
final var httpHeaders = new HttpHeaders ();
440
467
httpHeaders .setContentType (mediaType );
0 commit comments