如何从主应用程序运行柑橘测试

时间:2019-02-07 21:20:13

标签: java integration-testing citrus-framework

我正在尝试使用Citrus构建可配置的模型,我希望从命令行运行,每次运行它时都将不同的参数传递给测试。

我尝试以此为开始https://github.com/citrusframework/citrus/issues/325的参考,但在我的情况下无法正常工作。

我有一个正在运行的测试,其中包含一个测试用例,我可以像这样运行它

console.log([0, 1].reverse());

但是在尝试运行以下应用程序时:

mvn clean verify -Dit.test=myTest#myTestCase

它失败并显示以下错误

package com.grge.citrus;

import com.consol.citrus.dsl.design.DefaultTestDesigner;
import com.consol.citrus.Citrus;
import com.consol.citrus.context.TestContext;

import org.springframework.context.ConfigurableApplicationContext;
import com.grge.citrus.*;
import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;

public class App {
    public static void main(String[] args) {
        String suiteName = "mySuite";

        Citrus citrus = Citrus.newInstance();
        // my test with one testcase inside
        TestNGCitrusTestDesigner myTest  = new MyTest();

        try {
            citrus.beforeSuite(suiteName);

              // This works it come from the sample
              citrus.run(new SampleJavaDslTest().getTestCase());
            // fails null pointer exception  
            citrus.run(myTest.getTestCase());

            citrus.afterSuite(suiteName);
        } finally {
            ((ConfigurableApplicationContext) citrus.getApplicationContext()).close();
        }
    }

    private static class SampleJavaDslTest extends DefaultTestDesigner {
        public SampleJavaDslTest() {
            super();
            echo("Hello from Java DSL!");
        }
    }
}

有效地使用调试器,我看到进入try部分时,myTest具有:

  • applicationContext
  • testDesigner

以及许多其他属性设置为null,因此testDesigner.getTestCase()引发异常

Thx

1 个答案:

答案 0 :(得分:0)

从Citrus 2.7开始,该框架提供了一个主要的CLI类,可以完全满足您的需求:

https://github.com/citrusframework/citrus/blob/10d3954fd6c051d32e9cd974f685322c6454c779/modules/citrus-core/src/main/java/com/consol/citrus/main/CitrusApp.java#L38

Citrus application option usage:
  -h or --help = Displays cli option usage
  -d or --duration = Maximum time in milliseconds the server should be up and running - server will terminate automatically when time exceeds
  -c or --config = Custom Spring configuration class
  -s or --skipTests = Skip test execution
  -p or --package = Test package to execute
  -D or --properties = Default system properties to set
  -e or --exit = Force system exit when finished
  -t or --test = Test class/method to execute
  -j or --jar = External test jar to load tests from