是否可以在 JAVA DSL Runner 中应用 xml 模板

Is it possible to apply xml Templates in JAVA DSL Runner

我们的项目中有很多旧的柑橘 xml 测试用例和模板。升级到较新版本后,我决定切换到 Java DSL。是否可以继续使用旧模板?如果我尝试这样做,我会得到一个 "No bean named .. is defined" 异常。

我尝试通过@ImportResource 导入模板文件,但没有成功。

您可以编写一个简单的自定义测试操作来加载模板并使用当前测试上下文执行它:

templates/hello-template.xml

中给出以下模板
<spring:beans xmlns="http://www.citrusframework.org/schema/testcase"
          xmlns:spring="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.citrusframework.org/schema/testcase http://www.citrusframework.org/schema/testcase/citrus-testcase.xsd">
  <template name="helloTemplate">
    <echo>
      <message>Hello ${user}</message>
    </echo>
  </template>

</spring:beans>

您可以编写自定义测试操作来加载该模板:

public class TemplateTest extends TestNGCitrusTestRunner {

    @Test
    @CitrusTest
    public void test() {
        run(new CallTemplateAction("templates/hello-template.xml", "helloTemplate"));
    }

    private class CallTemplateAction extends AbstractTestAction {
        private final String templateName;
        private final String templateLocation;

        public CallTemplateAction(String templateLocation, String templateName) {
            this.templateLocation = templateLocation;
            this.templateName = templateName;
        }

        @Override
        public void doExecute(TestContext testContext) {
            Template template = new ClassPathXmlApplicationContext(new String[] { templateLocation }, 
                                         testContext.getApplicationContext())
                                        .getBean(templateName, Template.class);

            template.getParameter().put("user", "foo");
            template.execute(testContext);
        }
    }
}

您可能应该缓存模板实例 and/or 完成操作后关闭应用程序上下文。