Mockserver Maven插件:从文件初始化Mockserver期望

时间:2019-04-17 21:28:01

标签: maven-failsafe-plugin integration-testing mockserver

我正在使用Mockserver maven插件模拟一些集成测试请求。

我的pom.xml看起来像:

        ...

        <plugin>
            <groupId>org.mock-server</groupId>
            <artifactId>mockserver-maven-plugin</artifactId>
            <version>5.5.1</version>
            <configuration>
                <serverPort>1080</serverPort>
                <logLevel>DEBUG</logLevel>
                <initializationClass>com.mycompany.ExampleInitializationClass</initializationClass>
            </configuration>
            <executions>
                <execution>
                    <id>run-mockserver</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-mockserver</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        ...

这里的问题是我必须使用类(com.mycompany.ExampleInitializationClass)来提供期望,并且我想使用如下所述的JSON文件来提供期望:

http://www.mock-server.com/mock_server/initializing_expectations.html

我在插件配置中没有找到任何方法来使用属性初始化Mockserver:

-Dmockserver.initializationJsonPath

有什么方法可以实现?预先感谢。

1 个答案:

答案 0 :(得分:1)

您只需要定义initializationJson属性,即可指定具有期望的JSON文件的路径:

<plugin>
    <groupId>org.mock-server</groupId>
    <artifactId>mockserver-maven-plugin</artifactId>
    <version>5.5.1</version>
    <configuration>
        <serverPort>1080</serverPort>
        <logLevel>DEBUG</logLevel>
        <initializationJson>expectations.json</initializationJson>
    </configuration>
    <executions>
        ...
    </executions>
</plugin>

这里的问题是文件路径相对于testClasspath目录(例如${project.basedir}/target/test-classes/),因此您必须在其中复制期望文件。您可以使用例如为此的maven-antrun-plugin(如下)或 maven-resources-plugin。

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <copy file="your/expectations.json" todir="${project.basedir}/target/test-classes/"/>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>