I use mockserver (mock-server) on my java project and now I want to start it with a persistent Expectation Initializers with the mvn command. I tested it with a json file and it work correctly, but when I use or rather start it with an Expectation Initializers (Java file) the configuration is not load on startup from the mock-server, because the file was not found.
The command used is:
mvn -Dmockserver.initializationClass="mockserver.ExpectationInitializer" mockserver:run
The structure of my example project is:
And my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>java-example</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java-no-dependencies</artifactId>
<version>5.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-maven-plugin</artifactId>
<version>5.13.2</version>
<configuration>
<serverPort>1080</serverPort>
</configuration>
</plugin>
</plugins>
</build>
The following error, which I get when I start the command:
$ mvn -Dmockserver.initializationClass="mockserver.ExampleInitializationClass" mockserver:run
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.example:java-example >----------------------
[INFO] Building java-example 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- mockserver-maven-plugin:5.13.2:run (default-cli) @ java-example ---
[INFO] mockserver:run about to start MockServer on: serverPort [1080]
[ERROR] Exception loading class expectation initializer
java.lang.ClassNotFoundException: mockserver.ExampleInitializationClass
at java.net.URLClassLoader.findClass (URLClassLoader.java:471)
at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
at java.net.FactoryURLClassLoader.loadClass (URLClassLoader.java:897)
at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
at org.mockserver.maven.MockServerAbstractMojo.createInitializerClass (MockServerAbstractMojo.java:169)
at org.mockserver.maven.MockServerRunAndWaitMojo.execute (MockServerRunAndWaitMojo.java:40)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[INFO] loading class initialization file:
mockserver.ExampleInitializationClass
[WARNING] exception while loading JSON initialization class, ignoring class:
mockserver.ExampleInitializationClass
java.lang.ClassNotFoundException: mockserver.ExampleInitializationClass
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass (SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass (ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:239)
at org.mockserver.server.initialize.ExpectationInitializerLoader.retrieveExpectationsFromInitializerClass (ExpectationInitializerLoader.java:82)
at org.mockserver.server.initialize.ExpectationInitializerLoader.addExpectationsFromInitializer (ExpectationInitializerLoader.java:61)
at org.mockserver.server.initialize.ExpectationInitializerLoader.<init> (ExpectationInitializerLoader.java:43)
at org.mockserver.mock.HttpState.<init> (HttpState.java:126)
at org.mockserver.lifecycle.LifeCycle.<init> (LifeCycle.java:55)
at org.mockserver.netty.MockServer.<init> (MockServer.java:77)
at org.mockserver.netty.MockServer.<init> (MockServer.java:48)
at org.mockserver.maven.InstanceHolder.start (InstanceHolder.java:56)
at org.mockserver.maven.MockServerRunAndWaitMojo.execute (MockServerRunAndWaitMojo.java:50)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[INFO] 1080 started on port: 1080
Thank you for your help :)
Your code is in the test package. In my case, I'm using these dependencies.
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>${mock-sever-netty-version}</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>${mock-sever-netty-version}</version>
</dependency>
with this properties
<properties>
<mock-sever-netty-version>5.13.2</mock-sever-netty-version>
<apche-http-version>4.4.1</apche-http-version>
</properties>
Then I have a principal class that sets the initializer class
public class Application {
public static void main(String[] args) {
System.setProperty("mockserver.initializationClass",
ExpectationInitializerMock.class.getName());
ClientAndServer mockServer = startClientAndServer(1080);
}
}