mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.57k stars 1.07k forks source link

Start up standalone mockserver with a pre-defined set of expectations #274

Closed rahulkanchi closed 7 years ago

rahulkanchi commented 8 years ago

Atm, standalone mockserver always starts up with empty expectations and we will have to setup expectations via java/Ruby/Javascript clients or via direct curl calls. This issue is raised to add a feature in standalone mockserver, that enables it to load a pre-defined set of expectations located in a configured folder. These expectations will be in json format and each file will have one expectation.

jamesdbloom commented 7 years ago

That feature already exists if you use the initializationClass property of the Maven plugin. This is also described at http://www.mock-server.com/mock_server/running_mock_server.html#maven_plugin. Even if you don't have a maven build it is possible to run the plugin standalone as follows:

mvn -Dmockserver.serverPort=1080 -Dmockserver.proxyPort=1090 -Dmockserver.logLevel=INFO -Dmockserver.initializationClass="org.mockserver.maven.ExampleInitializationClass" org.mock-server:mockserver-maven-plugin:3.10.4:runForked

In the example above I have assumed you have an initialisation class called org.mockserver.maven.ExampleInitializationClass.

dimarassin commented 6 years ago

Please consider the following scenario: Before my integration test starts, a docker-compose with the entire test environment is being started. It essentially contains all required services for the test. I want to use mock-server for mocking some services that I don't need during my test. The problem is that some of the services make a call to the "mocked" service during the startup, so it will fail since the mock-server still doesn't have expectations. It will be very convenient to have an ability to run mock-server in docker with predefined expectations from the file system, environment, etc.

PedroHReis22 commented 4 years ago

Is there any workaround for the scenario described by @dimarassin ? I am running into the same situation.

jamesdbloom commented 4 years ago

This is already possible, see: http://mock-server.com/mock_server/initializing_expectations.html

If your using Kubernetes the documentation describes how to inject the expectation initialiser JSON file (or any other configuration) http://mock-server.com/where/kubernetes.html

shabloel commented 1 year ago

Hi James, I tried your suggestion to run MockServer standalone to replace an API that is not up and running yet. I created a class in my Springboot project accroding to : https://mock-server.com/mock_server/initializing_expectations.html where I declare my pre-defined expecations.

Then I added a application-local.properties file mockserver.initializationClass=my.class.path.ExampleInitializationClass

Then I run below command like you suggested. mvn -Dmockserver.serverPort=1080 -Dmockserver.proxyPort=1090 -Dmockserver.logLevel=INFO -Dmockserver.initializationClass="my.class.path.ExampleInitializationClass" org.mock-server:mockserver-maven-plugin:3.10.4:runForked

But it complains it cannot find the ExpectationsInitializer, no matter what I try. I mockserver not meant to run as standalone to replace an API, and only for testing purposes?