如何在maven中启动和停止mule

发布于 2024-12-19 18:24:32 字数 1476 浏览 0 评论 0原文

我需要在运行测试之前启动 Mule,并在测试完成后停止它。我不清楚如何改变我的 Maven pom 来实现这一点。到目前为止,我的 pom 中有以下内容:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>org.mule.MuleServer</argument>
                    <argument>-config</argument>
                    <argument>my-config.xml</argument>
                </arguments>
            </configuration>
        </plugin>

--- 更新 ---

在下面的一些回复之后,我决定添加一些其他详细信息。

我有几个扩展 Mules FunctionTestCase 类的单元测试:

public class SomeUnitTest extends FunctionalTestCase

我开始使用 JBehave 编写一些新的客户验收测试,这些测试在 Maven“集成测试”阶段运行。如果没有 mule 实例运行,这些测试就无法成功。加载和执行故事的主类已经扩展了 JUnitStories 类:

public class MyStories extends JUnitStories 

由于我无法从此类中继承FunctionalTestCase,因此我需要寻找替代方案来让 mule 在需要这些故事时运行和停止。

I need to start Mule before I run my tests, and stop it when the tests are finished. It is unclear to me how to alter my Maven pom to accomplish this. So far I have the following in my pom:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>org.mule.MuleServer</argument>
                    <argument>-config</argument>
                    <argument>my-config.xml</argument>
                </arguments>
            </configuration>
        </plugin>

--- Update ---

After some replies below I decided to add some additional details.

I have several unit tests extending Mules FunctionalTestCase class:

public class SomeUnitTest extends FunctionalTestCase

I started writing some new customer acceptance tests using JBehave which are wired to run during mavens "integration-test" phase. These tests cannot succeed without an instance of mule running. The main class that loads and executes the stories already extends the JUnitStories class:

public class MyStories extends JUnitStories 

Since I cannot inherit from FunctionalTestCase in this class, I need to look at alternatives to getting mule to run and stop when I need it for these stories.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月寒剑心 2024-12-26 18:24:32

为什么不使用Mule 的FunctionalTestCase 来代替呢?它会启动内存中的 Mule 并加载您的配置。不确定启动整个独立的 Mule 会带来什么好处。

Why don't you use Mule's FunctionalTestCase instead? It fires up an in-memory Mule and loads up your configs. Not sure what you gain from starting the whole standalone Mule.

萌辣 2024-12-26 18:24:32

您可能可以通过利用 Maven 的阶段来完成您想要的任务。您必须定义 2 个执行。一个启动骡子,一个停止它。您可以在预测试阶段之一(可能类似于流程测试类)启动 mule,然后在测试阶段后启动 stat mule。不过,这可能非常棘手。

另一种可能性是在 junit 中启动 mule,比如在 BeforeClass 设置函数中。这可能会容易得多。看:
http://junit.sourceforge.net/javadoc/org/junit/BeforeClass.html

class muleDriver {
    @BeforeClass
    public static startMule() {
        ....programmatically initialize Mule....
        ....You should be able to grab the config files from the classpath....
    }

    @Test
    public void testSomething() {
        ....Run Some Tests...
    }
}

我要问的另一个问题是为什么你想要启动整个 mule 框架进行测试。我所在的团队就是这样做的,测试最终在构建过程中花费了大量时间,因为他们在整个测试周期中多次启动骡​​子(并且其他团队复制了该模式。)

You could probably accomplish what you want by leveraging maven's phases. You'd have to define 2 executions. One to start mule and one to stop it. You'd start mule in one of the pre-test phases (maybe something like process-test-classes) and then stat mule after the test phase. It could be pretty tricky, though.

Another possibility is to fire mule up in junit, somewhere like in a BeforeClass setup function. This will probably be much easier. See:
http://junit.sourceforge.net/javadoc/org/junit/BeforeClass.html

class muleDriver {
    @BeforeClass
    public static startMule() {
        ....programmatically initialize Mule....
        ....You should be able to grab the config files from the classpath....
    }

    @Test
    public void testSomething() {
        ....Run Some Tests...
    }
}

The other question I'd ask is why you'd want to fire up the whole mule framework for tests. I was on a team that did this, and tests ended up taking a tremendous amount of time during builds because they were firing up mule many times throughout the testing cycle (and other teams copied the pattern.)

冰魂雪魄 2024-12-26 18:24:32

尝试使用 junit @ClassRule。副作用是单个测试用例无法再执行。

    @RunWith(Suite.class)
@Suite.SuiteClasses({MuleFlowTest.class})
    public class MuleSuite {


    @ClassRule
    public static ExternalResource testRule = new ExternalResource() {

        @Override
        protected void before() throws Throwable {

            final String CLI_OPTIONS[] = {"-config", FlowTestUtil.getURI() };

            MuleServer server = new MuleServer(CLI_OPTIONS);
            server.start(false, true);
            mc = server.getMuleContext();
            muleServer = server;

        };

        @Override
        protected void after() {
            System.out.println("Shutdown");
            muleServer.shutdown();
            muleServer = null;
        }
    };

    private static MuleServer muleServer;
    public static MuleContext mc;

}

public class MuleFlowTest {

    @Test
    public void flowTest() {
        assertNotNull(MuleSuite.mc);
    }
}

Try using junit @ClassRule. The side effect is that individual test cases can not be executed any more.

    @RunWith(Suite.class)
@Suite.SuiteClasses({MuleFlowTest.class})
    public class MuleSuite {


    @ClassRule
    public static ExternalResource testRule = new ExternalResource() {

        @Override
        protected void before() throws Throwable {

            final String CLI_OPTIONS[] = {"-config", FlowTestUtil.getURI() };

            MuleServer server = new MuleServer(CLI_OPTIONS);
            server.start(false, true);
            mc = server.getMuleContext();
            muleServer = server;

        };

        @Override
        protected void after() {
            System.out.println("Shutdown");
            muleServer.shutdown();
            muleServer = null;
        }
    };

    private static MuleServer muleServer;
    public static MuleContext mc;

}

public class MuleFlowTest {

    @Test
    public void flowTest() {
        assertNotNull(MuleSuite.mc);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文