Run VirtServer From Maven

Applies to VirtServer 3.23.0, last modified on November 22, 2024

If you use Maven in your development environment, you can start virtual services (virtual APIs) on VirtServer from Maven. To do this, you command Maven to download and use VirtServer Maven in your project.

Requirements and limitations

  • To start virtual services on a VirtServer by using Maven, you need a .pom file that references the VirtServer Maven plugin. Here is an example:

  • You do not need to configure your environment in a special way to run virtual services. Any required dependencies will be handled by Maven.

  • You cannot work with JMS virtual services from Maven.

Run tests from Maven

To use the plugin in a Maven build, connect it to the Maven build phase. For example, if you connect it to the -Pint-tests phase:

mvn -Pint-tests -DvirtServer.userName=John -DvirtServer.password=Smith -Dvirtserver.host=127.0.0.1 -Dvirtserver.port:8080 -DvirtPort=9090 clean install

Where:

‑Dvirtserver.userName The username. A user with this name must exist on VirtServer.
‑Dvirtserver.password The password of the user.
‑Dvirtserver.host VirtServer URL. This URL will be used for all virtual services you will start. You must specify the URL of the running VirtServer instance.
‑Dvirtserver.port The port used by VirtServer. This port is required to deploy virtual services, but not to work with them.
‑DvirtPort The port the virtual service will use.
You can also specify these properties in the pom.xml file.

.pom File

Maven reads details about the build from the pom.xml file. You need to configure it to get the VirtServer Maven plugin from the repository and add a ReadyAPI test to the test phase of the build lifecycle.

You can find a sample Maven configuration below. If you already have your own configuration, copy the necessary elements to it.

Sample Maven functional test

Below is a sample pom.xml file that runs the Sample-SOAP-project.xml project. You can find this project in the <ReadyAPI installation directory>\tutorials folder. To use the sample:

  • Create a new pom.xml file and copy the sample code to it. Make sure to specify the same version as the ReadyAPI version you have.

  • Place the Sample-SOAP-project.xml project file in the folder where the pom.xml file is located.

  • Make sure VirtServer is running on localhost and uses port 8080.

  • Run the Maven build by using the command described above.

If you already have a pom.xml file, copy the elements to it to include your test in the build.

<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/maven-v4_0_0.xsd">

   <!--Add the SmartBear ReadyAPI plugin repository.-->
   <!--Maven will download the plugin from the specified URL.-->
   <pluginRepositories>
      <pluginRepository>
         <id>SmartBearPluginRepository</id>
         <url>http://smartbearsoftware.com/repository/maven2</url>
      </pluginRepository>
   </pluginRepositories>

    <!--Specify the connection properties-->
    <properties>
        <virtserver.host>localhost</virtserver.host>
        <virtserver.port>9090</virtserver.port>
        <virt.port>8080</virt.port>
        <virtserver.userName>John</virtserver.userName>
        <virtserver.password>Smith</virtserver.password>
    </properties>
    
    <!--Configure the profile-->
    <profiles>
        <profile>
            <id>int-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <!--The plugin groupID used to uniquely identify the project with the plugin.-->
                        <groupId>com.smartbear</groupId>
                         <!--The plugin artifactId used to find the plugin in the project.-->
                        <artifactId>virtserver-maven-plugin</artifactId>
                        <!--Specifies your VirtServer version. Maven will use the appropriate plugin version.-->
                        <!-- IMPORTANT: Must be the same as your VirtServer version.-->
                        <version>1</version>
                        <configuration>
                            <projectFile>
                                Sample-SOAP-project.xml
                            </projectFile>
                            <!--The name of the virtual service in the project.-->
                            <virtName>ServiceSoapBinding MockService</virtName>
                            <!--The full path to the VirtServer instance.-->
                            <serverEndpoint>${virtserver.host}:${virtserver.port}</serverEndpoint>
                            <!--The number of the port the virtual service will use.-->
                            <port>${virt.port}</port>
                            <!--If true, VirtServer will start the virtual service immediately. Otherwise, you will need to start the service later.-->
                            <autoStart>false</autoStart>
                            <!--If true, the existing virtual service with the same name will be replaced.-->
                            <!--Otherwise, the old vert will stay on VirtServer.-->
                            <replaceExistingVirt>true</replaceExistingVirt>
                        </configuration>
                        <executions>
                            <!--Add an execution to deploy and start the virtual service before the integration test phase.-->
                            <execution>
                                <id>deployAndStartVirt</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>deployVirt</goal>
                                    <goal>startVirt</goal>
                                    <!--goal>listVirts</goal-->
                                </goals>
                            </execution>
                            <!--Add an execution to stop and undeploy a virtual service after the integration test phase.-->
                            <execution>
                                <id>stopAndUndeployVirt</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stopVirt</goal>
                                    <goal>undeployVirt</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--Use the groovy-maven-plugin to check that the virtual service has been deployed successfully.-->
                    <plugin>
                        <groupId>org.codehaus.gmaven</groupId>
                        <artifactId>groovy-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <source>import org.apache.maven.plugin.MojoExecutionException
                                    String urlString = 'http://${virtserver.host}:${virt.port}/accountcreation'
                                    if (new URL(urlString).text.contains('Welcome!')) {
                                        println 'Successfully contacted deployed virtual service'
                                    } else {
                                        throw new MojoExecutionException("Couldn't contact deployed virtual service on $urlString")
                                    }
                                    </source>
                                </configuration>

                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

See Also

Different Ways of Running VirtServer

Highlight search results