diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java index 1eb0c80ea7..455a4aff0d 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java @@ -231,7 +231,7 @@ class StubRunnerExecutor implements StubFinder { final List mappings = repository.getProjectDescriptors(); final Collection contracts = repository.contracts; Integer port = stubRunnerOptions.port(stubConfiguration); - if (!contracts.isEmpty() && !hasRequest(contracts)) { + if (!hasRequest(contracts) && mappings.isEmpty()) { if (log.isDebugEnabled()) { log.debug("There are no HTTP related contracts. Won't start any servers"); } @@ -239,7 +239,7 @@ class StubRunnerExecutor implements StubFinder { return; } if (port != null && port >= 0) { - this.stubServer = new StubServer(stubConfiguration, mappings, contracts, new WireMockHttpServerStub(port)); + this.stubServer = new StubServer(stubConfiguration, mappings, contracts, new WireMockHttpServerStub(port)).start(); } else { this.stubServer = this.portScanner.tryToExecuteWithFreePort(new PortCallback() { @@ -248,12 +248,14 @@ class StubRunnerExecutor implements StubFinder { return new StubServer(stubConfiguration, mappings, contracts, new WireMockHttpServerStub(availablePort)); } - }); + }).start(); } - this.stubServer = this.stubServer.start(); } private boolean hasRequest(Collection contracts) { + if (contracts.isEmpty()) { + return false; + } for (Contract contract : contracts) { if (contract.getRequest() != null) { return true; diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy index 4ca3d6c9ed..466ac0e531 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy @@ -131,6 +131,19 @@ class StubRunnerExecutorSpec extends Specification { executor.shutdown() } + def 'should not start http server if no contracts or mappings are found'() { + given: + def stubConf = new StubConfiguration('asd', 'asd', 'asd', '') + StubRunnerExecutor executor = new StubRunnerExecutor(portScanner) + when: + RunningStubs stubs = executor.runStubs(stubRunnerOptions, + new StubRepository(new File('src/test/resources/emptyrepo')), stubConf) + then: + stubs.getPort('asd') == -1 + cleanup: + executor.shutdown() + } + Map stubIdsWithPortsFromString(String stubIdsToPortMapping) { return stubIdsToPortMapping.split(',').collectEntries { String entry -> return StubsParser.fromStringWithPort(entry)