Skip to content

Commit c3c73a4

Browse files
committed
Allow loading mapping for resources by resource path
1 parent a32e96d commit c3c73a4

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,42 @@ public WireMockContainer withMapping(String name, String json) {
142142
* @param resourceJson Mapping definition file
143143
* @return this instance
144144
*/
145+
public WireMockContainer withMappingFromResource(String name, Class<?> resource, String resourceJson) {
146+
return withMapping(name, resource.getSimpleName() + "/" + resourceJson);
147+
}
148+
149+
/**
150+
* @deprecated use {@link #withMappingFromResource(String, Class, String)}
151+
*/
152+
@Deprecated
145153
public WireMockContainer withMapping(String name, Class<?> resource, String resourceJson) {
154+
return withMappingFromResource(name, resource, resourceJson);
155+
}
156+
157+
/**
158+
* Loads mapping stub from the resource file
159+
* @param name Name of the mapping stub
160+
* @param resourceName Resource name and path
161+
* @return this instance
162+
*/
163+
public WireMockContainer withMappingFromResource(String name, String resourceName) {
164+
try {
165+
URL url = Resources.getResource(resourceName);
166+
String text = Resources.toString(url, StandardCharsets.UTF_8);
167+
return withMapping(name, text);
168+
} catch (IOException ex) {
169+
throw new IllegalArgumentException(ex);
170+
}
171+
}
172+
173+
/**
174+
* Loads mapping stub from the resource file
175+
* @param name Name of the mapping stub
176+
* @param url Resource file URL
177+
* @return this instance
178+
*/
179+
public WireMockContainer withMappingFromResource(String name, URL url) {
146180
try {
147-
URL url = Resources.getResource(resource, resource.getSimpleName() + "/" + resourceJson);
148181
String text = Resources.toString(url, StandardCharsets.UTF_8);
149182
return withMapping(name, text);
150183
} catch (IOException ex) {

0 commit comments

Comments
 (0)