Best Citrus code snippet using com.consol.citrus.context.TestContext.TestListeners
Source:TestContext.java
...23import com.consol.citrus.functions.FunctionRegistry;24import com.consol.citrus.functions.FunctionUtils;25import com.consol.citrus.message.*;26import com.consol.citrus.report.MessageListeners;27import com.consol.citrus.report.TestListeners;28import com.consol.citrus.util.TypeConversionUtils;29import com.consol.citrus.validation.MessageValidatorRegistry;30import com.consol.citrus.validation.interceptor.GlobalMessageConstructionInterceptors;31import com.consol.citrus.validation.matcher.ValidationMatcherRegistry;32import com.consol.citrus.variable.GlobalVariables;33import com.consol.citrus.variable.VariableUtils;34import com.consol.citrus.xml.namespace.NamespaceContextBuilder;35import org.slf4j.Logger;36import org.slf4j.LoggerFactory;37import org.springframework.context.ApplicationContext;38import org.springframework.util.*;39import java.lang.reflect.Field;40import java.util.*;41import java.util.Map.Entry;42import java.util.concurrent.ConcurrentHashMap;43/**44 * Class holding and managing test variables. The test context also provides utility methods45 * for replacing dynamic content(variables and functions) in message payloads and headers.46 * 47 * @author Christoph Deppisch48 */49public class TestContext {50 /**51 * Logger52 */53 private static Logger log = LoggerFactory.getLogger(TestContext.class);54 55 /** Local variables */56 protected Map<String, Object> variables;57 58 /** Global variables */59 private GlobalVariables globalVariables;60 /** Message store */61 private MessageStore messageStore = new DefaultMessageStore();62 63 /** Function registry holding all available functions */64 private FunctionRegistry functionRegistry = new FunctionRegistry();65 /** Endpoint factory creates endpoint instances */66 private EndpointFactory endpointFactory;67 /** Bean reference resolver */68 private ReferenceResolver referenceResolver;69 70 /** Registered message validators */71 private MessageValidatorRegistry messageValidatorRegistry = new MessageValidatorRegistry();72 73 /** Registered validation matchers */74 private ValidationMatcherRegistry validationMatcherRegistry = new ValidationMatcherRegistry();75 /** List of test listeners to be informed on test events */76 private TestListeners testListeners = new TestListeners();77 /** List of message listeners to be informed on inbound and outbound message exchange */78 private MessageListeners messageListeners = new MessageListeners();79 /** List of global message construction interceptors */80 private GlobalMessageConstructionInterceptors globalMessageConstructionInterceptors = new GlobalMessageConstructionInterceptors();81 /** Central namespace context builder */82 private NamespaceContextBuilder namespaceContextBuilder = new NamespaceContextBuilder();83 /** Spring bean application context */84 private ApplicationContext applicationContext;85 /** Timers registered in test context, that can be stopped */86 protected Map<String, StopTimer> timers = new ConcurrentHashMap<>();87 /** List of exceptions that actions raised during execution of forked operations */88 private List<CitrusRuntimeException> exceptions = new ArrayList<>();89 /**90 * Default constructor91 */92 public TestContext() {93 variables = new ConcurrentHashMap<>();94 }95 96 /**97 * Gets the value for the given variable expression. Expression usually is the 98 * simple variable name, with optional expression prefix/suffix.99 * 100 * In case variable is not known to the context throw runtime exception.101 * 102 * @param variableExpression expression to search for.103 * @throws CitrusRuntimeException104 * @return value of the variable105 */106 public String getVariable(final String variableExpression) {107 return getVariable(variableExpression, String.class);108 }109 /**110 * Gets typed variable value.111 * @param variableExpression112 * @param type113 * @param <T>114 * @return115 */116 public <T> T getVariable(String variableExpression, Class<T> type) {117 return TypeConversionUtils.convertIfNecessary(getVariableObject(variableExpression), type);118 }119 /**120 * Gets the value for the given variable as object representation.121 * Use this method if you seek for test objects stored in the context.122 * 123 * @param variableExpression expression to search for.124 * @throws CitrusRuntimeException125 * @return value of the variable as object126 */127 public Object getVariableObject(final String variableExpression) {128 String variableName = VariableUtils.cutOffVariablesPrefix(variableExpression);129 if (variableName.startsWith(Citrus.VARIABLE_ESCAPE) && variableName.endsWith(Citrus.VARIABLE_ESCAPE)) {130 return Citrus.VARIABLE_PREFIX + VariableUtils.cutOffVariablesEscaping(variableName) + Citrus.VARIABLE_SUFFIX;131 } else if (variables.containsKey(variableName)) {132 return variables.get(variableName);133 } else if (variableName.contains(".")) {134 String objectName = variableName.substring(0, variableName.indexOf("."));135 if (variables.containsKey(objectName)) {136 return getVariable(variables.get(objectName), variableName.substring(variableName.indexOf(".") + 1));137 }138 }139 throw new CitrusRuntimeException("Unknown variable '" + variableName + "'");140 }141 /**142 * Gets variable from path expression. Variable paths are translated to reflection fields on object instances.143 * Path separators are '.'. Each separator is handled as object hierarchy.144 * @param instance145 * @param pathExpression146 */147 private Object getVariable(Object instance, String pathExpression) {148 String leftOver = null;149 String fieldName;150 if (pathExpression.contains(".")) {151 fieldName = pathExpression.substring(0, pathExpression.indexOf("."));152 leftOver = pathExpression.substring(pathExpression.indexOf(".") + 1);153 } else {154 fieldName = pathExpression;155 }156 Field field = ReflectionUtils.findField(instance.getClass(), fieldName);157 if (field == null) {158 throw new CitrusRuntimeException(String.format("Failed to get variable - unknown field '%s' on type %s", fieldName, instance.getClass().getName()));159 }160 ReflectionUtils.makeAccessible(field);161 Object fieldValue = ReflectionUtils.getField(field, instance);162 if (StringUtils.hasText(leftOver)) {163 return getVariable(fieldValue, leftOver);164 }165 return fieldValue;166 }167 /**168 * Creates a new variable in this test context with the respective value. In case variable already exists 169 * variable is overwritten.170 * 171 * @param variableName the name of the new variable172 * @param value the new variable value173 * @throws CitrusRuntimeException174 * @return175 */176 public void setVariable(final String variableName, Object value) {177 if (!StringUtils.hasText(variableName) || VariableUtils.cutOffVariablesPrefix(variableName).length() == 0) {178 throw new CitrusRuntimeException("Can not create variable '"+ variableName + "', please define proper variable name");179 }180 if (value == null) {181 throw new VariableNullValueException("Trying to set variable: " + VariableUtils.cutOffVariablesPrefix(variableName) + ", but variable value is null");182 }183 if (log.isDebugEnabled()) {184 log.debug("Setting variable: " + VariableUtils.cutOffVariablesPrefix(variableName) + " with value: '" + value + "'");185 }186 variables.put(VariableUtils.cutOffVariablesPrefix(variableName), value);187 }188 /**189 * Add variables to context.190 * @param variableNames the variable names to set191 * @param variableValues the variable values to set192 */193 public void addVariables(String[] variableNames, Object[] variableValues) {194 if (variableNames.length != variableValues.length) {195 throw new CitrusRuntimeException(String.format(196 "Invalid context variable usage - received '%s' variables with '%s' values",197 variableNames.length,198 variableValues.length));199 }200 for (int i = 0; i < variableNames.length; i++) {201 if (variableValues[i] != null) {202 setVariable(variableNames[i], variableValues[i]);203 }204 }205 }206 207 /**208 * Add several new variables to test context. Existing variables will be 209 * overwritten.210 * 211 * @param variablesToSet the list of variables to set.212 */213 public void addVariables(Map<String, Object> variablesToSet) {214 for (Entry<String, Object> entry : variablesToSet.entrySet()) {215 if (entry.getValue() != null) {216 setVariable(entry.getKey(), entry.getValue());217 } else {218 setVariable(entry.getKey(), "");219 }220 }221 }222 /**223 * Replaces variables and functions inside a map with respective values and returns a new224 * map representation.225 *226 * @param map optionally having variable entries.227 * @return the constructed map without variable entries.228 */229 public <T> Map<String, T> resolveDynamicValuesInMap(final Map<String, T> map) {230 Map<String, T> target = new LinkedHashMap<>(map.size());231 for (Entry<String, T> entry : map.entrySet()) {232 String key = replaceDynamicContentInString(entry.getKey());233 T value = entry.getValue();234 if (value instanceof String) {235 //put value into target map, but check if value is variable or function first236 target.put(key, (T) replaceDynamicContentInString((String) value));237 } else {238 target.put(key, value);239 }240 }241 return target;242 }243 /**244 * Replaces variables and functions in a list with respective values and245 * returns the new list representation.246 *247 * @param list having optional variable entries.248 * @return the constructed list without variable entries.249 */250 public <T> List<T> resolveDynamicValuesInList(final List<T> list) {251 List<T> variableFreeList = new ArrayList<>(list.size());252 for (T value : list) {253 if (value instanceof String) {254 //add new value after check if it is variable or function255 variableFreeList.add((T) replaceDynamicContentInString((String) value));256 }257 }258 return variableFreeList;259 }260 /**261 * Replaces variables and functions in array with respective values and262 * returns the new array representation.263 *264 * @param array having optional variable entries.265 * @return the constructed list without variable entries.266 */267 public <T> T[] resolveDynamicValuesInArray(final T[] array) {268 return resolveDynamicValuesInList(Arrays.asList(array)).toArray(Arrays.copyOf(array, array.length));269 }270 271 /**272 * Clears variables in this test context. Initially adds all global variables.273 */274 public void clear() {275 variables.clear();276 variables.putAll(globalVariables.getVariables());277 }278 279 /**280 * Checks if variables are present right now.281 * @return boolean flag to mark existence282 */283 public boolean hasVariables() {284 return !CollectionUtils.isEmpty(variables);285 }286 287 /**288 * Method replacing variable declarations and place holders as well as 289 * function expressions in a string290 * 291 * @param str the string to parse.292 * @return resulting string without any variable place holders.293 */294 public String replaceDynamicContentInString(String str) {295 return replaceDynamicContentInString(str, false);296 }297 /**298 * Method replacing variable declarations and functions in a string, optionally 299 * the variable values get surrounded with single quotes.300 * 301 * @param str the string to parse for variable place holders.302 * @param enableQuoting flag marking surrounding quotes should be added or not.303 * @return resulting string without any variable place holders.304 */305 public String replaceDynamicContentInString(final String str, boolean enableQuoting) {306 String result = null;307 if (str != null) {308 result = VariableUtils.replaceVariablesInString(str, this, enableQuoting);309 result = FunctionUtils.replaceFunctionsInString(result, this, enableQuoting);310 }311 return result;312 }313 314 /**315 * Checks weather the given expression is a variable or function and resolves the value316 * accordingly317 * @param expression the expression to resolve318 * @return the resolved expression value319 */320 public String resolveDynamicValue(String expression) {321 if (VariableUtils.isVariableName(expression)) {322 return getVariable(expression);323 } else if (functionRegistry.isFunction(expression)) {324 return FunctionUtils.resolveFunction(expression, this);325 }326 return expression;327 }328 /**329 * Handles error creating a new CitrusRuntimeException and330 * informs test listeners.331 * @param testName332 * @param packageName333 * @param message334 * @param cause335 * @return336 */337 public CitrusRuntimeException handleError(String testName, String packageName, String message, Exception cause) {338 // Create empty dummy test case for logging purpose339 TestCase dummyTest = new TestCase();340 dummyTest.setName(testName);341 dummyTest.setPackageName(packageName);342 CitrusRuntimeException exception = new CitrusRuntimeException(message, cause);343 // inform test listeners with failed test344 testListeners.onTestStart(dummyTest);345 testListeners.onTestFailure(dummyTest, exception);346 testListeners.onTestFinish(dummyTest);347 return exception;348 }349 350 /**351 * Setter for test variables in this context.352 * @param variables353 */354 public void setVariables(Map<String, Object> variables) {355 this.variables = variables;356 }357 /**358 * Getter for test variables in this context.359 * @return test variables for this test context.360 */361 public Map<String, Object> getVariables() {362 return variables;363 }364 /**365 * Get global variables.366 * @param globalVariables367 */368 public void setGlobalVariables(GlobalVariables globalVariables) {369 this.globalVariables = globalVariables;370 371 variables.putAll(globalVariables.getVariables());372 }373 /**374 * Set global variables.375 * @return the globalVariables376 */377 public Map<String, Object> getGlobalVariables() {378 return globalVariables.getVariables();379 }380 /**381 * Sets the messageStore property.382 *383 * @param messageStore384 */385 public void setMessageStore(MessageStore messageStore) {386 this.messageStore = messageStore;387 }388 /**389 * Gets the value of the messageStore property.390 *391 * @return the messageStore392 */393 public MessageStore getMessageStore() {394 return messageStore;395 }396 /**397 * Get the current function registry.398 * @return the functionRegistry399 */400 public FunctionRegistry getFunctionRegistry() {401 return functionRegistry;402 }403 /**404 * Set the function registry.405 * @param functionRegistry the functionRegistry to set406 */407 public void setFunctionRegistry(FunctionRegistry functionRegistry) {408 this.functionRegistry = functionRegistry;409 }410 /**411 * Set the message validator registry.412 * @param messageValidatorRegistry the messageValidatorRegistry to set413 */414 public void setMessageValidatorRegistry(MessageValidatorRegistry messageValidatorRegistry) {415 this.messageValidatorRegistry = messageValidatorRegistry;416 }417 /**418 * Get the message validator registry.419 * @return the messageValidatorRegistry420 */421 public MessageValidatorRegistry getMessageValidatorRegistry() {422 return messageValidatorRegistry;423 }424 /**425 * Get the current validation matcher registry426 * @return427 */428 public ValidationMatcherRegistry getValidationMatcherRegistry() {429 return validationMatcherRegistry;430 }431 /**432 * Set the validation matcher registry433 * @param validationMatcherRegistry434 */435 public void setValidationMatcherRegistry(ValidationMatcherRegistry validationMatcherRegistry) {436 this.validationMatcherRegistry = validationMatcherRegistry;437 }438 /**439 * Gets the message listeners.440 * @return441 */442 public MessageListeners getMessageListeners() {443 return messageListeners;444 }445 /**446 * Set the message listeners.447 * @param messageListeners448 */449 public void setMessageListeners(MessageListeners messageListeners) {450 this.messageListeners = messageListeners;451 }452 /**453 * Gets the test listeners.454 * @return455 */456 public TestListeners getTestListeners() {457 return testListeners;458 }459 /**460 * Set the test listeners.461 * @param testListeners462 */463 public void setTestListeners(TestListeners testListeners) {464 this.testListeners = testListeners;465 }466 /**467 * Gets the global message construction interceptors.468 * @return469 */470 public GlobalMessageConstructionInterceptors getGlobalMessageConstructionInterceptors() {471 return globalMessageConstructionInterceptors;472 }473 /**474 * Sets the global messsage construction interceptors.475 * @param messageConstructionInterceptors476 */477 public void setGlobalMessageConstructionInterceptors(GlobalMessageConstructionInterceptors messageConstructionInterceptors) {...
Source:TestContextFactory.java
...17import com.consol.citrus.endpoint.DefaultEndpointFactory;18import com.consol.citrus.endpoint.EndpointFactory;19import com.consol.citrus.functions.FunctionRegistry;20import com.consol.citrus.report.MessageListeners;21import com.consol.citrus.report.TestListeners;22import com.consol.citrus.validation.MessageValidatorRegistry;23import com.consol.citrus.validation.interceptor.GlobalMessageConstructionInterceptors;24import com.consol.citrus.validation.matcher.ValidationMatcherRegistry;25import com.consol.citrus.variable.GlobalVariables;26import com.consol.citrus.xml.namespace.NamespaceContextBuilder;27import org.slf4j.Logger;28import org.slf4j.LoggerFactory;29import org.springframework.beans.BeansException;30import org.springframework.beans.factory.FactoryBean;31import org.springframework.beans.factory.annotation.Autowired;32import org.springframework.context.ApplicationContext;33import org.springframework.context.ApplicationContextAware;34import org.springframework.util.CollectionUtils;35/**36 * Factory bean implementation taking care of {@link FunctionRegistry} and {@link GlobalVariables}.37 * 38 * @author Christoph Deppisch39 */40public class TestContextFactory implements FactoryBean<TestContext>, ApplicationContextAware {41 42 @Autowired43 private FunctionRegistry functionRegistry;44 45 @Autowired46 private ValidationMatcherRegistry validationMatcherRegistry;47 48 @Autowired(required = false)49 private GlobalVariables globalVariables = new GlobalVariables();50 @Autowired51 private MessageValidatorRegistry messageValidatorRegistry;52 @Autowired53 private TestListeners testListeners;54 @Autowired55 private MessageListeners messageListeners;56 @Autowired57 private EndpointFactory endpointFactory;58 @Autowired59 private ReferenceResolver referenceResolver;60 @Autowired61 private GlobalMessageConstructionInterceptors globalMessageConstructionInterceptors;62 @Autowired(required=false)63 private NamespaceContextBuilder namespaceContextBuilder;64 /** Spring bean application context */65 private ApplicationContext applicationContext;66 67 /** Logger */68 private static Logger log = LoggerFactory.getLogger(TestContextFactory.class);69 /**70 * Create new empty instance that has71 * @return72 */73 public static TestContextFactory newInstance() {74 TestContextFactory factory = new TestContextFactory();75 factory.setFunctionRegistry(new FunctionRegistry());76 factory.setValidationMatcherRegistry(new ValidationMatcherRegistry());77 factory.setGlobalVariables(new GlobalVariables());78 factory.setMessageValidatorRegistry(new MessageValidatorRegistry());79 factory.setTestListeners(new TestListeners());80 factory.setMessageListeners(new MessageListeners());81 factory.setGlobalMessageConstructionInterceptors(new GlobalMessageConstructionInterceptors());82 factory.setEndpointFactory(new DefaultEndpointFactory());83 factory.setReferenceResolver(new SpringBeanReferenceResolver());84 factory.setNamespaceContextBuilder(new NamespaceContextBuilder());85 return factory;86 }87 /**88 * Construct new factory instance from application context.89 * @param applicationContext90 * @return91 */92 public static TestContextFactory newInstance(ApplicationContext applicationContext) {93 TestContextFactory factory = new TestContextFactory();94 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(FunctionRegistry.class))) {95 factory.setFunctionRegistry(applicationContext.getBean(FunctionRegistry.class));96 }97 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ValidationMatcherRegistry.class))) {98 factory.setValidationMatcherRegistry(applicationContext.getBean(ValidationMatcherRegistry.class));99 }100 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(GlobalVariables.class))) {101 factory.setGlobalVariables(applicationContext.getBean(GlobalVariables.class));102 }103 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(MessageValidatorRegistry.class))) {104 factory.setMessageValidatorRegistry(applicationContext.getBean(MessageValidatorRegistry.class));105 }106 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(TestListeners.class))) {107 factory.setTestListeners(applicationContext.getBean(TestListeners.class));108 }109 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(MessageListeners.class))) {110 factory.setMessageListeners(applicationContext.getBean(MessageListeners.class));111 }112 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(GlobalMessageConstructionInterceptors.class))) {113 factory.setGlobalMessageConstructionInterceptors(applicationContext.getBean(GlobalMessageConstructionInterceptors.class));114 }115 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(EndpointFactory.class))) {116 factory.setEndpointFactory(applicationContext.getBean(EndpointFactory.class));117 }118 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ReferenceResolver.class))) {119 factory.setReferenceResolver(applicationContext.getBean(ReferenceResolver.class));120 }121 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(NamespaceContextBuilder.class))) {122 factory.setNamespaceContextBuilder(applicationContext.getBean(NamespaceContextBuilder.class));123 }124 factory.setApplicationContext(applicationContext);125 return factory;126 }127 128 /**129 * @see org.springframework.beans.factory.FactoryBean#getObject()130 */131 public TestContext getObject() {132 TestContext context = new TestContext();133 context.setFunctionRegistry(functionRegistry);134 context.setValidationMatcherRegistry(validationMatcherRegistry);135 context.setGlobalVariables(globalVariables);136 context.setMessageValidatorRegistry(messageValidatorRegistry);137 context.setTestListeners(testListeners);138 context.setMessageListeners(messageListeners);139 context.setGlobalMessageConstructionInterceptors(globalMessageConstructionInterceptors);140 context.setEndpointFactory(endpointFactory);141 context.setReferenceResolver(referenceResolver);142 context.setApplicationContext(applicationContext);143 if (namespaceContextBuilder != null) {144 context.setNamespaceContextBuilder(namespaceContextBuilder);145 }146 if (log.isDebugEnabled()) {147 log.debug("Created new test context - using global variables: '"148 + context.getGlobalVariables() + "'");149 }150 151 return context;152 }153 /**154 * @see org.springframework.beans.factory.FactoryBean#getObjectType()155 */156 @SuppressWarnings({ "unchecked", "rawtypes" })157 public Class getObjectType() {158 return TestContext.class;159 }160 /**161 * @see org.springframework.beans.factory.FactoryBean#isSingleton()162 */163 public boolean isSingleton() {164 return false;165 }166 /**167 * @param functionRegistry the functionRegistry to set168 */169 public void setFunctionRegistry(FunctionRegistry functionRegistry) {170 this.functionRegistry = functionRegistry;171 }172 /**173 * @return the functionRegistry174 */175 public FunctionRegistry getFunctionRegistry() {176 return functionRegistry;177 }178 179 /**180 * @param validationMatcherRegistry the validationMatcherRegistry to set181 */182 public void setValidationMatcherRegistry(183 ValidationMatcherRegistry validationMatcherRegistry) {184 this.validationMatcherRegistry = validationMatcherRegistry;185 }186 /**187 * @return the validationMatcherRegistry188 */189 public ValidationMatcherRegistry getValidationMatcherRegistry() {190 return validationMatcherRegistry;191 }192 /**193 * @param globalVariables the globalVariables to set194 */195 public void setGlobalVariables(GlobalVariables globalVariables) {196 this.globalVariables = globalVariables;197 }198 /**199 * @return the globalVariables200 */201 public GlobalVariables getGlobalVariables() {202 return globalVariables;203 }204 /**205 * Gets the endpoint factory.206 * @return207 */208 public EndpointFactory getEndpointFactory() {209 return endpointFactory;210 }211 /**212 * Sets the endpoint factory.213 * @param endpointFactory214 */215 public void setEndpointFactory(EndpointFactory endpointFactory) {216 this.endpointFactory = endpointFactory;217 }218 /**219 * Gets the value of the referenceResolver property.220 *221 * @return the referenceResolver222 */223 public ReferenceResolver getReferenceResolver() {224 return referenceResolver;225 }226 /**227 * Sets the referenceResolver property.228 *229 * @param referenceResolver230 */231 public void setReferenceResolver(ReferenceResolver referenceResolver) {232 this.referenceResolver = referenceResolver;233 }234 /**235 * Sets the namespace context builder.236 * @param namespaceContextBuilder237 */238 public void setNamespaceContextBuilder(NamespaceContextBuilder namespaceContextBuilder) {239 this.namespaceContextBuilder = namespaceContextBuilder;240 }241 /**242 * Gets the namespace context builder.243 * @return244 */245 public NamespaceContextBuilder getNamespaceContextBuilder() {246 return namespaceContextBuilder;247 }248 /**249 * Sets the test listeners.250 * @param testListeners251 */252 public void setTestListeners(TestListeners testListeners) {253 this.testListeners = testListeners;254 }255 /**256 * Gets the test listeners.257 * @return258 */259 public TestListeners getTestListeners() {260 return testListeners;261 }262 /**263 * Sets the message validator registry.264 * @param messageValidatorRegistry265 */266 public void setMessageValidatorRegistry(MessageValidatorRegistry messageValidatorRegistry) {267 this.messageValidatorRegistry = messageValidatorRegistry;268 }269 /**270 * Gets the message validator registry.271 * @return272 */273 public MessageValidatorRegistry getMessageValidatorRegistry() {...
Source:TestSuiteTest.java
...44 when(applicationContextMock.getBean(TestContextFactory.class)).thenReturn(testContextFactory);45 when(applicationContextMock.getBeansOfType(SequenceAfterSuite.class)).thenReturn(Collections.singletonMap("afterActions", afterActions));46 when(applicationContextMock.getBeansOfType(SequenceBeforeSuite.class)).thenReturn(Collections.singletonMap("beforeActions", beforeActions));47 when(applicationContextMock.getBean(TestSuiteListeners.class)).thenReturn(testSuiteListeners);48 when(applicationContextMock.getBean(TestListeners.class)).thenReturn(new TestListeners());49 citrus = Citrus.newInstance(applicationContextMock);50 }51 @BeforeMethod52 public void resetMocks() {53 reset(testSuiteListener);54 }55 @Test56 public void testBeforeSuite() {57 beforeActions.getActions().clear();58 beforeActions.addTestAction(new EchoAction());59 citrus.beforeSuite("sample-suite");60 verify(testSuiteListener).onStart();61 verify(testSuiteListener).onStartSuccess();62 }...
TestListeners
Using AI Code Generation
1package com.consol.citrus;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.context.TestContextFactory;4import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;5import org.testng.annotations.Test;6public class TestListeners extends TestNGCitrusTestDesigner {7 public void testListeners() {8 TestContext context = TestContextFactory.create();9 context.addListener(new TestListener() {10 public void onTestSuccess(TestContext context, String name) {11 System.out.println("Test " + name + " has been successfull");12 }13 public void onTestFailure(TestContext context, String name, Throwable cause) {14 System.out.println("Test " + name + " has been failed");15 }16 });17 context.run(() -> {18 echo("Test step 1");19 echo("Test step 2");20 echo("Test step 3");21 });22 }23}24package com.consol.citrus;25import com.consol.citrus.context.TestContext;26import com.consol.citrus.context.TestContextFactory;27import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;28import org.testng.annotations.Test;29public class TestListeners extends TestNGCitrusTestDesigner {30 public void testListeners() {31 TestContext context = TestContextFactory.create();32 context.addListener(new TestListener() {33 public void onTestSuccess(TestContext context, String name) {34 System.out.println("Test " + name + " has been successfull");35 }36 public void onTestFailure(TestContext context, String name, Throwable cause) {37 System.out.println("Test " + name + " has been failed");38 }39 });40 context.run(() -> {41 echo("Test step 1");42 fail("Some error occured");43 echo("Test step 2");44 echo("Test step 3");45 });46 }47}
TestListeners
Using AI Code Generation
1package com.consol.citrus.dsl.testng;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import org.testng.annotations.Test;5public class TestNGTestRunnerTest extends TestNGCitrusTestRunner {6 public void test() {7 TestNGCitrusTestDesigner builder = new TestNGCitrusTestDesigner(this) {8 public void configure() {9 echo("Hello TestNG Citrus!");10 }11 };12 builder.run();13 }14}15package com.consol.citrus.dsl.testng;16import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;17import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;18import org.testng.annotations.Test;19public class TestNGTestRunnerTest extends TestNGCitrusTestRunner {20 public void test() {21 TestNGCitrusTestDesigner builder = new TestNGCitrusTestDesigner(this) {22 public void configure() {23 echo("Hello TestNG Citrus!");24 }25 };26 builder.run();27 }28}29package com.consol.citrus.dsl.testng;30import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;31import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;32import org.testng.annotations.Test;33public class TestNGTestRunnerTest extends TestNGCitrusTestRunner {34 public void test() {35 TestNGCitrusTestDesigner builder = new TestNGCitrusTestDesigner(this) {36 public void configure() {37 echo("Hello TestNG Citrus!");38 }39 };40 builder.run();41 }42}43package com.consol.citrus.dsl.testng;44import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;45import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;46import org.testng.annotations.Test;
TestListeners
Using AI Code Generation
1public class 4 {2 public static void main(String[] args) {3 TestContext context = new TestContext();4 TestListeners listeners = new TestListeners();5 listeners.addListener(new TestListener() {6 public void onTestStart(TestCase testCase) {7 System.out.println("Test started");8 }9 public void onTestFinish(TestCase testCase) {10 System.out.println("Test finished");11 }12 public void onTestFailure(TestCase testCase, Throwable throwable) {13 System.out.println("Test failed");14 }15 public void onTestSkipped(TestCase testCase) {16 System.out.println("Test skipped");17 }18 });19 context.setTestListeners(listeners);20 TestCase testCase = new TestCase();21 testCase.setName("testcase");22 testCase.setTestContext(context);23 testCase.run();24 }25}26public class 5 {27 public static void main(String[] args) {28 TestContext context = new TestContext();29 TestListeners listeners = new TestListeners();30 listeners.addListener(new TestListener() {31 public void onTestStart(TestCase testCase) {32 System.out.println("Test started");33 }34 public void onTestFinish(TestCase testCase) {35 System.out.println("Test finished");36 }37 public void onTestFailure(TestCase testCase, Throwable throwable) {38 System.out.println("Test failed");39 }40 public void onTestSkipped(TestCase testCase) {41 System.out.println("Test skipped");42 }43 });44 context.setTestListeners(listeners);45 TestCase testCase = new TestCase();46 testCase.setName("testcase");
TestListeners
Using AI Code Generation
1package com.consol.citrus;2import java.io.File;3import java.io.IOException;4import java.util.List;5import org.testng.annotations.Test;6import org.testng.annotations.BeforeTest;7import org.testng.annotations.AfterTest;8import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;9import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;10import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTest;11import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestBuilder;12import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder;13import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder.TestNGCitrusTestRunnerBuilderSupport;14import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder.TestNGCitrusTestRunnerBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImpl;15import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder.TestNGCitrusTestRunnerBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImpl.TestNGCitrusTestRunnerBuilderSupportImplBuilder;16import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder.TestNGCitrusTestRunnerBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImpl.TestNGCitrusTestRunnerBuilderSupportImplBuilder.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupport;17import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder.TestNGCitrusTestRunnerBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImpl.TestNGCitrusTestRunnerBuilderSupportImplBuilder.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupportImpl;18import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner.TestNGCitrusTestRunnerBuilder.TestNGCitrusTestRunnerBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImpl.TestNGCitrusTestRunnerBuilderSupportImplBuilder.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupport.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupportImpl.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupportImpl.TestNGCitrusTestRunnerBuilderSupportImplBuilderSupportImplBuilder;19import com.consol
TestListeners
Using AI Code Generation
1package com.consol.citrus;2import org.testng.annotations.Test;3public class TestListeners {4 public void test() {5 TestContext context = new TestContext();6 context.addTestListeners(new TestListener() {7 public void onTestStart(TestCase test) {8 System.out.println("Test started");9 }10 });11 }12}
TestListeners
Using AI Code Generation
1package com.consol.citrus;2import org.testng.annotations.Test;3public class TestListenersTest extends TestNGCitrusTestDesigner {4 public void testListeners() {5 echo("TestListeners");6 TestContext context = new TestContext();7 context.setVariable("testKey", "testValue");8 TestListeners testListeners = new TestListeners();9 testListeners.setTestContext(context);10 testListeners.onTestStart(null);11 testListeners.onTestSuccess(null);12 testListeners.onTestFailure(null);13 testListeners.onTestSkipped(null);14 testListeners.onTestFailedButWithinSuccessPercentage(null);15 }16}17package com.consol.citrus;18import org.testng.annotations.Test;19public class TestNGCitrusTestDesignerTest extends TestNGCitrusTestDesigner {20 public void testTestNGCitrusTestDesigner() {21 echo("TestNGCitrusTestDesigner");22 TestContext context = new TestContext();23 context.setVariable("testKey", "testValue");24 setTestContext(context);25 getTestContext().setVariable("testKey", "testValue");26 getTestContext().getVariable("testKey");27 getTestContext().getVariables();28 getTestContext().getVariables().get("testKey");29 }30}31package com.consol.citrus;32import org.testng.annotations.Test;33public class TestNGCitrusTestTest extends TestNGCitrusTest {34 public void testTestNGCitrusTest() {35 echo("TestNGCitrusTest");36 TestContext context = new TestContext();37 context.setVariable("testKey", "testValue");38 setTestContext(context);39 getTestContext().setVariable("testKey", "testValue");40 getTestContext().getVariable("testKey");41 getTestContext().getVariables();42 getTestContext().getVariables().get("testKey");43 }44}45package com.consol.citrus;46import org
TestListeners
Using AI Code Generation
1package org.citrusframework.demo;2import org.testng.annotations.Test;3import com.consol.citrus.context.TestContext;4import com.consol.citrus.testng.AbstractTestNGUnitTest;5import com.consol.citrus.testng.TestNGCitrusSupport;6public class TestListeners extends AbstractTestNGUnitTest {7 public void testListeners() {8 TestContext context = new TestContext();9 TestNGCitrusSupport citrus = new TestNGCitrusSupport();10 System.out.println("Listeners are: " + context.getTestListeners());11 }12}13package org.citrusframework.demo;14import org.testng.annotations.Test;15import com.consol.citrus.context.TestContext;16import com.consol.citrus.testng.AbstractTestNGUnitTest;17import com.consol.citrus.testng.TestNGCitrusSupport;18public class TestListeners extends AbstractTestNGUnitTest {19 public void testListeners() {20 TestContext context = new TestContext();21 TestNGCitrusSupport citrus = new TestNGCitrusSupport();22 System.out.println("Listeners are: " + context.getTestListeners());23 }24}25package org.citrusframework.demo;26import org.testng.annotations.Test;27import com.consol.citrus.context.TestContext;28import com.consol.citrus.testng.AbstractTestNGUnitTest;29import com.consol.citrus.testng.TestNGCitrusSupport;30public class TestListeners extends AbstractTestNGUnitTest {31 public void testListeners() {32 TestContext context = new TestContext();33 TestNGCitrusSupport citrus = new TestNGCitrusSupport();34 System.out.println("Listeners are: " + context.getTestListeners());35 }36}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!