Skip to content

Commit

Permalink
feat(configFactory): be able to access junit-context during config of…
Browse files Browse the repository at this point in the history
… the server

Closes #9
  • Loading branch information
davinkevin committed Aug 13, 2019
1 parent 23036a3 commit 253f708
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import org.junit.jupiter.api.extension.ExtensionContext;
import ru.lanwen.wiremock.ext.WiremockResolver;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
Expand All @@ -20,18 +21,18 @@ public interface WiremockConfigFactory {
*
* @return config for wiremock
*/
WireMockConfiguration create();
default WireMockConfiguration create() {
return options().dynamicPort().notifier(new Slf4jNotifier(true));
}

default WireMockConfiguration create(ExtensionContext context) {
return create();
}

/**
* By default creates config with dynamic port only and notifier.
*/
class DefaultWiremockConfigFactory implements WiremockConfigFactory {

@Override
public WireMockConfiguration create() {
return options().dynamicPort().notifier(new Slf4jNotifier(true));
}
}
class DefaultWiremockConfigFactory implements WiremockConfigFactory {}

/**
* By default creates config with dynamic port only, notifier and Templating Response enabled.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ru/lanwen/wiremock/ext/WiremockFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.lanwen.wiremock.ext;

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import ru.lanwen.wiremock.config.CustomizationContext;
import ru.lanwen.wiremock.config.CustomizationContext.CustomizationContextBuilder;
Expand All @@ -14,9 +15,9 @@
*/
class WiremockFactory {

public WireMockServer createServer(final Wiremock mockedServer) {
public WireMockServer createServer(final Wiremock mockedServer, ExtensionContext extensionContext) {
try {
return new WireMockServer(mockedServer.factory().newInstance().create());
return new WireMockServer(mockedServer.factory().newInstance().create(extensionContext));
} catch (ReflectiveOperationException e) {
throw new ParameterResolutionException(
format("Can't create config with given factory %s", mockedServer.factory()),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/lanwen/wiremock/ext/WiremockResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte

Wiremock mockedServer = parameterContext.getParameter().getAnnotation(Wiremock.class);

server = wiremockFactory.createServer(mockedServer);
server = wiremockFactory.createServer(mockedServer, extensionContext);
server.start();

CustomizationContext customizationContext = wiremockFactory.createContextBuilder().
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/ru/lanwen/wiremock/ext/WiremockFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void setup() {

@Test
public void createServer() {
WireMockServer srv1 = factory.createServer(mockedServer);
WireMockServer srv2 = factory.createServer(mockedServer);
WireMockServer srv1 = factory.createServer(mockedServer, null);
WireMockServer srv2 = factory.createServer(mockedServer, null);
assertNotNull(srv1);
assertNotNull(srv2);
assertSame(OPTIONS, srv1.getOptions());
Expand All @@ -74,7 +74,7 @@ public void createServer() {
public void configFactoryCouldNotBeInstantiated() {
when(mockedServer.factory()).thenReturn((Class) PrivateClassNotAllowed.class);
assertThrows(ParameterResolutionException.class,
() -> factory.createServer(mockedServer),
() -> factory.createServer(mockedServer, null),
"Can't create config with given factory class ru.lanwen.wiremock.ext.WiremockFactoryTest$PrivateClassNotAllowed");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void supportsParameter() throws Exception {

@Test
public void resolveParameterFailed() throws Exception {
when(wiremockFactory.createServer(mockedServer)).thenReturn(server);
when(wiremockFactory.createServer(mockedServer, extensionContext)).thenReturn(server);
when(wiremockFactory.createContextBuilder()).thenReturn(customizationContextBuilder);
when(wiremockFactory.createCustomizer(mockedServer)).thenReturn(customizer);
when(customizationContextBuilder.extensionContext(extensionContext)).thenReturn(customizationContextBuilder);
Expand Down

0 comments on commit 253f708

Please sign in to comment.