Search code examples
javaintegration-testingcitrus-framework

how to run citrus test from a main application


I'm trying to use Citrus to build a configurable mockup that I'd like to run from the command line passing different parameters to the test each time I run it.

I tried to use this as a reference to get started https://github.com/citrusframework/citrus/issues/325 but could not get it to work in my case.

I've got a running test running with one test case inside and I can run it like that

mvn clean verify -Dit.test=myTest#myTestCase

But when trying to run the following app:

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!");
        }
    }
}

It fails with the following error

22:08:21,691 DEBUG        citrus.Citrus| Loading Citrus application properties
22:08:21,696 DEBUG        citrus.Citrus| Setting application property citrus.spring.java.config=com.grge.citrus.VCenterActorConfigSSL
22:08:22,025 DEBUG BeanDefinitionReader| Loaded 0 bean definitions from location pattern [classpath*:citrus-context.xml]
22:08:23,059 DEBUG    server.HttpServer| Starting server: vCenterServer ...
22:08:23,217 DEBUG CachingServletFilter| Initializing filter 'request-caching-filter'
22:08:23,220 DEBUG CachingServletFilter| Filter 'request-caching-filter' configured successfully
22:08:23,220 DEBUG et.GzipServletFilter| Initializing filter 'gzip-filter'
22:08:23,220 DEBUG et.GzipServletFilter| Filter 'gzip-filter' configured successfully
22:08:23,222 DEBUG rusDispatcherServlet| Initializing servlet 'vCenterServer-servlet'
22:08:23,241 INFO  rusDispatcherServlet| FrameworkServlet 'vCenterServer-servlet': initialization started
22:08:23,250 DEBUG rusDispatcherServlet| Servlet with name 'vCenterServer-servlet' will try to create custom WebApplicationContext context of class 'org.springframework.web.context.support.XmlWebApplicationContext', using parent context [null]
22:08:23,865 DEBUG rusDispatcherServlet| Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling provided
22:08:23,869 DEBUG rusDispatcherServlet| Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@16fb356]
22:08:23,873 DEBUG rusDispatcherServlet| Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@1095f122]
22:08:23,894 DEBUG rusDispatcherServlet| No HandlerExceptionResolvers found in servlet 'vCenterServer-servlet': using default
22:08:23,896 DEBUG rusDispatcherServlet| Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@733c423e]
22:08:23,910 DEBUG rusDispatcherServlet| No ViewResolvers found in servlet 'vCenterServer-servlet': using default
22:08:23,915 DEBUG rusDispatcherServlet| Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@681aad3b]
22:08:23,922 DEBUG rusDispatcherServlet| Published WebApplicationContext of servlet 'vCenterServer-servlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.vCenterServer-servlet]
22:08:23,922 INFO  rusDispatcherServlet| FrameworkServlet 'vCenterServer-servlet': initialization completed in 678 ms
22:08:23,923 DEBUG rusDispatcherServlet| Servlet 'vCenterServer-servlet' configured successfully
22:08:54,266 INFO     server.HttpServer| Started server: vCenterServer
22:08:54,362 INFO  port.LoggingReporter| 
22:08:54,362 INFO  port.LoggingReporter| ------------------------------------------------------------------------
22:08:54,362 INFO  port.LoggingReporter|        .__  __                       
22:08:54,362 INFO  port.LoggingReporter|   ____ |__|/  |________ __ __  ______
22:08:54,362 INFO  port.LoggingReporter| _/ ___\|  \   __\_  __ \  |  \/  ___/
22:08:54,362 INFO  port.LoggingReporter| \  \___|  ||  |  |  | \/  |  /\___ \ 
22:08:54,362 INFO  port.LoggingReporter|  \___  >__||__|  |__|  |____//____  >
22:08:54,362 INFO  port.LoggingReporter|      \/                           \/
22:08:54,362 INFO  port.LoggingReporter| 
22:08:54,363 INFO  port.LoggingReporter| C I T R U S  T E S T S  2.7.8
22:08:54,363 INFO  port.LoggingReporter| 
22:08:54,363 INFO  port.LoggingReporter| ------------------------------------------------------------------------
22:08:54,363 DEBUG port.LoggingReporter| BEFORE TEST SUITE
22:08:54,363 INFO  port.LoggingReporter| 
22:08:54,363 INFO  port.LoggingReporter| 
22:08:54,363 INFO  port.LoggingReporter| BEFORE TEST SUITE: SUCCESS
22:08:54,363 INFO  port.LoggingReporter| ------------------------------------------------------------------------
22:08:54,363 INFO  port.LoggingReporter| 
22:08:54,370 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}'
22:08:54,370 INFO  port.LoggingReporter| 
22:08:54,370 INFO  port.LoggingReporter| ------------------------------------------------------------------------
22:08:54,370 DEBUG port.LoggingReporter| STARTING TEST SampleJavaDslTest <com.grge.citrus>
22:08:54,370 INFO  port.LoggingReporter| 
22:08:54,370 DEBUG      citrus.TestCase| Initializing test case
22:08:54,372 DEBUG  context.TestContext| Setting variable: citrus.test.name with value: 'SampleJavaDslTest'
22:08:54,372 DEBUG  context.TestContext| Setting variable: citrus.test.package with value: 'com.grge.citrus'
22:08:54,372 DEBUG      citrus.TestCase| Test variables:
22:08:54,372 DEBUG      citrus.TestCase| citrus.test.name = SampleJavaDslTest
22:08:54,372 DEBUG      citrus.TestCase| citrus.test.package = com.grge.citrus
22:08:54,373 INFO    actions.EchoAction| Hello from Java DSL!
22:08:54,493 INFO  port.LoggingReporter| 
22:08:54,493 INFO  port.LoggingReporter| TEST SUCCESS SampleJavaDslTest (com.grge.citrus)
22:08:54,493 INFO  port.LoggingReporter| ------------------------------------------------------------------------
22:08:54,493 INFO  port.LoggingReporter| 
Exception in thread "main" java.lang.NullPointerException
    at com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner.getTestCase(TestNGCitrusTestDesigner.java:113)
    at com.grge.citrus.App.main(App.java:25)

Effectively using the debugger I can see that when entering the try section, myTest has:

  • applicationContext
  • testDesigner

and many other attributes set to null and so the testDesigner.getTestCase() raise an Exception

Thx


Solution

  • Since Citrus 2.7 the framework provides a main CLI class that does exactly what you want:

    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