How to use CitrusSpringObjectFactory class of cucumber.runtime.java.spring package

Best Citrus code snippet using cucumber.runtime.java.spring.CitrusSpringObjectFactory

Source:CitrusBackend.java Github

copy

Full Screen

...24import cucumber.api.java.*;25import cucumber.runtime.*;26import cucumber.runtime.io.ResourceLoader;27import cucumber.runtime.io.ResourceLoaderClassFinder;28import cucumber.runtime.java.spring.CitrusSpringObjectFactory;29import cucumber.runtime.snippets.FunctionNameGenerator;30import gherkin.pickles.PickleStep;31import io.cucumber.stepexpression.TypeRegistry;32import org.slf4j.Logger;33import org.slf4j.LoggerFactory;34import org.springframework.context.ApplicationContext;35import org.springframework.context.ConfigurableApplicationContext;36import org.springframework.context.support.ClassPathXmlApplicationContext;37import java.lang.reflect.Method;38import java.util.List;39import java.util.Map;40/**41 * @author Christoph Deppisch42 * @since 2.643 */44public class CitrusBackend implements Backend {45 /** Citrus instance used by all scenarios */46 private static Citrus citrus;47 /** Logger */48 private static Logger log = LoggerFactory.getLogger(CitrusBackend.class);49 /** Basic resource loader */50 private ResourceLoader resourceLoader;51 private TypeRegistry typeRegistry;52 /**53 * Constructor using resource loader.54 * @param resourceLoader55 */56 public CitrusBackend(ResourceLoader resourceLoader, TypeRegistry typeRegistry) {57 this.resourceLoader = resourceLoader;58 this.typeRegistry = typeRegistry;59 Citrus.CitrusInstanceManager.addInstanceProcessor(instance -> instance.beforeSuite(CitrusReporter.SUITE_NAME));60 }61 @Override62 public void loadGlue(Glue glue, List<String> gluePaths) {63 try {64 Citrus.CitrusInstanceManager.addInstanceProcessor(new XmlStepInstanceProcessor(glue, gluePaths, getObjectFactory(), typeRegistry));65 } catch (IllegalAccessException e) {66 throw new CitrusRuntimeException("Failed to add XML step definition", e);67 }68 try {69 if (!gluePaths.contains(CitrusLifecycleHooks.class.getPackage().getName()) && getObjectFactory().addClass(CitrusLifecycleHooks.class)) {70 Method beforeMethod = CitrusLifecycleHooks.class.getMethod("before", Scenario.class);71 Before beforeAnnotation = beforeMethod.getAnnotation(Before.class);72 glue.addBeforeHook(new JavaHookDefinition(beforeMethod, beforeAnnotation.value(), beforeAnnotation.order(), beforeAnnotation.timeout(), getObjectFactory()));73 Method afterMethod = CitrusLifecycleHooks.class.getMethod("after", Scenario.class);74 After afterAnnotation = afterMethod.getAnnotation(After.class);75 glue.addAfterHook(new JavaHookDefinition(afterMethod, afterAnnotation.value(), afterAnnotation.order(), afterAnnotation.timeout(), getObjectFactory()));76 }77 } catch (NoSuchMethodException | IllegalAccessException e) {78 throw new CucumberException("Unable to add Citrus lifecylce hooks");79 }80 }81 @Override82 public void buildWorld() {83 }84 @Override85 public void disposeWorld() {86 }87 @Override88 public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {89 return "";90 }91 /**92 * Gets the object factory instance that is configured in environment.93 * @return94 */95 private ObjectFactory getObjectFactory() throws IllegalAccessException {96 if (Env.INSTANCE.get(ObjectFactory.class.getName()).equals(CitrusObjectFactory.class.getName())) {97 return CitrusObjectFactory.instance();98 } else if (Env.INSTANCE.get(ObjectFactory.class.getName()).equals(CitrusSpringObjectFactory.class.getName())) {99 return CitrusSpringObjectFactory.instance();100 }101 return ObjectFactoryLoader.loadObjectFactory(new ResourceLoaderClassFinder(resourceLoader, Thread.currentThread().getContextClassLoader()),102 Env.INSTANCE.get(ObjectFactory.class.getName()));103 }104 /**105 * Provide access to the Citrus instance.106 * @return107 */108 public static Citrus getCitrus() {109 if (citrus == null) {110 citrus = Citrus.newInstance();111 }112 return citrus;113 }...

Full Screen

Full Screen

Source:CitrusSpringObjectFactory.java Github

copy

Full Screen

...29/**30 * @author Christoph Deppisch31 * @since 2.632 */33public class CitrusSpringObjectFactory extends SpringFactory {34 /** Logger */35 private static Logger log = LoggerFactory.getLogger(CitrusSpringObjectFactory.class);36 /** Test designer */37 private TestDesigner designer;38 /** Test runner */39 private TestRunner runner;40 /** Test context */41 private TestContext context;42 /** Static self reference */43 private static CitrusSpringObjectFactory selfReference;44 /** Mode to use for injection */45 private InjectionMode mode = null;46 /**47 * Default constructor with static self reference initialization.48 */49 public CitrusSpringObjectFactory() {50 selfReference = this;51 }52 @Override53 public boolean addClass(Class<?> clazz) {54 InjectionMode fallback;55 if (mode == null) {56 log.info("Initializing injection mode for Citrus " + Citrus.getVersion());57 fallback = InjectionMode.valueOf(System.getProperty(CitrusObjectFactory.INJECTION_MODE_PROPERTY, System.getenv(CitrusObjectFactory.INJECTION_MODE_ENV) != null ?58 System.getenv(CitrusObjectFactory.INJECTION_MODE_ENV) : InjectionMode.DESIGNER.name()));59 } else {60 fallback = mode;61 }62 InjectionMode requiredMode = InjectionMode.analyseMode(clazz, fallback);63 if (mode == null) {64 mode = requiredMode;65 } else if (!mode.equals(requiredMode)) {66 log.warn(String.format("Ignoring class of injection type '%s' as current injection mode is '%s'", requiredMode, mode));67 return false;68 }69 return super.addClass(clazz);70 }71 @Override72 public void start() {73 super.start();74 context = getInstance(TestContext.class);75 if (mode == null) {76 mode = InjectionMode.valueOf(System.getProperty(CitrusObjectFactory.INJECTION_MODE_PROPERTY, System.getenv(CitrusObjectFactory.INJECTION_MODE_ENV) != null ?77 System.getenv(CitrusObjectFactory.INJECTION_MODE_ENV) : InjectionMode.DESIGNER.name()));78 }79 if (InjectionMode.DESIGNER.equals(mode)) {80 designer = new DefaultTestDesigner(CitrusBackend.getCitrus().getApplicationContext(), context);81 }82 if (InjectionMode.RUNNER.equals(mode)) {83 runner = new DefaultTestRunner(CitrusBackend.getCitrus().getApplicationContext(), context);84 }85 }86 @Override87 public <T> T getInstance(Class<T> type) {88 if (context == null) {89 try {90 context = super.getInstance(TestContext.class);91 CitrusBackend.initializeCitrus(context.getApplicationContext());92 } catch (CucumberException e) {93 log.warn("Failed to get proper TestContext from Cucumber Spring application context: " + e.getMessage());94 context = CitrusBackend.getCitrus().createTestContext();95 }96 }97 if (TestContext.class.isAssignableFrom(type)) {98 return (T) context;99 }100 if (CitrusSpringObjectFactory.class.isAssignableFrom(type)) {101 return (T) this;102 }103 T instance = super.getInstance(type);104 CitrusAnnotations.injectAll(instance, CitrusBackend.getCitrus());105 if (InjectionMode.DESIGNER.equals(mode)) {106 CitrusDslAnnotations.injectTestDesigner(instance, designer);107 }108 if (InjectionMode.RUNNER.equals(mode)) {109 CitrusDslAnnotations.injectTestRunner(instance, runner);110 }111 return instance;112 }113 /**114 * Static access to self reference.115 * @return116 */117 public static CitrusSpringObjectFactory instance() throws IllegalAccessException {118 if (selfReference == null) {119 throw new IllegalAccessException("Illegal access to self reference - not available yet");120 }121 return selfReference;122 }123}...

Full Screen

Full Screen

Source:CitrusSpringObjectFactoryTest.java Github

copy

Full Screen

...20/**21 * @author Christoph Deppisch22 * @since 2.623 */24public class CitrusSpringObjectFactoryTest {25 @AfterMethod(alwaysRun = true)26 public void resetCitrus() {27 CitrusBackend.resetCitrus();28 }29 @Test30 public void testDesignerInject() throws Exception {31 CitrusSpringObjectFactory factory = new CitrusSpringObjectFactory();32 factory.addClass(SpringDesignerSteps.class);33 // Scenario 134 factory.start();35 final SpringDesignerSteps steps = factory.getInstance(SpringDesignerSteps.class);36 Assert.assertNotNull(steps.getTestDesigner());37 factory.stop();38 }39 @Test40 public void testDesignerInjectWithDefaultContext() throws Exception {41 CitrusSpringObjectFactory factory = new CitrusSpringObjectFactory();42 factory.addClass(DefaultSpringDesignerSteps.class);43 // Scenario 144 factory.start();45 final DefaultSpringDesignerSteps steps = factory.getInstance(DefaultSpringDesignerSteps.class);46 Assert.assertNotNull(steps.getTestDesigner());47 factory.stop();48 }49 @Test50 public void testRunnerInject() throws Exception {51 CitrusSpringObjectFactory factory = new CitrusSpringObjectFactory();52 factory.addClass(SpringRunnerSteps.class);53 // Scenario 154 factory.start();55 final SpringRunnerSteps steps = factory.getInstance(SpringRunnerSteps.class);56 Assert.assertNotNull(steps.getTestRunner());57 factory.stop();58 }59 @Test60 public void testRunnerInjectWithDefaultContext() throws Exception {61 CitrusSpringObjectFactory factory = new CitrusSpringObjectFactory();62 factory.addClass(DefaultSpringRunnerSteps.class);63 // Scenario 164 factory.start();65 final DefaultSpringRunnerSteps steps = factory.getInstance(DefaultSpringRunnerSteps.class);66 Assert.assertNotNull(steps.getTestRunner());67 factory.stop();68 }69}...

Full Screen

Full Screen

CitrusSpringObjectFactory

Using AI Code Generation

copy

Full Screen

1import cucumber.api.java.en.Given;2import cucumber.api.java.en.Then;3import cucumber.api.java.en.When;4import org.springframework.beans.factory.annotation.Autowired;5import org.springframework.test.context.ContextConfiguration;6import org.springframework.test.context.TestContextManager;7import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;8import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;9import org.testng.annotations.BeforeClass;10import org.testng.annotations.Listeners;11import org.testng.annotations.Test;12@ContextConfiguration(locations = "/applicationContext.xml")13@Listeners({DependencyInjectionTestExecutionListener.class})14public class 3 extends AbstractTestNGSpringContextTests {15 public void setUpClass() throws Exception {16 new TestContextManager(getClass()).prepareTestInstance(this);17 }18 private StepDefs stepDefs;19 public void test() throws Throwable {20 stepDefs.given_I_have_100_in_my_account();21 stepDefs.when_I_withdraw_50();22 stepDefs.then_I_should_have_50_in_my_account();23 }24}25import cucumber.api.java.en.Given;26import cucumber.api.java.en.Then;27import cucumber.api.java.en.When;28import org.springframework.beans.factory.annotation.Autowired;29import org.springframework.test.context.ContextConfiguration;30import org.springframework.test.context.TestContextManager;31import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;32import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;33import org.testng.annotations.BeforeClass;34import org.testng.annotations.Listeners;35import org.testng.annotations.Test;36@ContextConfiguration(locations = "/applicationContext.xml")37@Listeners({DependencyInjectionTestExecutionListener.class})38public class 4 extends AbstractTestNGSpringContextTests {39 public void setUpClass() throws Exception {40 new TestContextManager(getClass()).prepareTestInstance(this);41 }42 private StepDefs stepDefs;43 public void test() throws Throwable {44 stepDefs.given_I_have_100_in_my_account();45 stepDefs.when_I_withdraw_50();46 stepDefs.then_I_should_have_50_in_my_account();47 }48}49import cucumber.api.java.en.Given;50import cucumber.api.java.en.Then;51import cucumber.api.java.en.When;52import org.springframework.beans.factory.annotation.Autowired;53import org.springframework.test.context.ContextConfiguration;54import org.springframework.test.context.TestContextManager;55import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

Full Screen

Full Screen

CitrusSpringObjectFactory

Using AI Code Generation

copy

Full Screen

1package com.cucumber.test;2import cucumber.api.CucumberOptions;3import cucumber.api.junit.Cucumber;4import org.junit.runner.RunWith;5@RunWith(Cucumber.class)6@CucumberOptions(7 plugin = {"pretty", "html:target/cucumber-html-report"}8public class RunCukesTest {9}10package com.cucumber.test.stepdefs;11import cucumber.api.java.en.Given;12import org.springframework.beans.factory.annotation.Autowired;13import org.springframework.beans.factory.annotation.Value;14public class StepDefs {15 @Value("${test}")16 private String test;17 private SpringBean springBean;18 @Given("^3$")19 public void three() {20 System.out.println(test);21 springBean.doSomething();22 }23}24package com.cucumber.test.stepdefs;25import org.springframework.stereotype.Component;26public class SpringBean {27 public void doSomething() {28 System.out.println("SpringBean.doSomething");29 }30}

Full Screen

Full Screen

CitrusSpringObjectFactory

Using AI Code Generation

copy

Full Screen

1package com.test.cucumber;2import cucumber.api.CucumberOptions;3import cucumber.api.junit.Cucumber;4import org.junit.runner.RunWith;5@RunWith(Cucumber.class)6@CucumberOptions(7 plugin = {"pretty", "html:target/cucumber-html-report", "json:target/cucumber.json"},8 tags = {"@test"}9public class RunnerTest {10}11package com.test.cucumber;12import cucumber.api.CucumberOptions;13import cucumber.api.junit.Cucumber;14import org.junit.runner.RunWith;15@RunWith(Cucumber.class)16@CucumberOptions(17 plugin = {"pretty", "html:target/cucumber-html-report", "json:target/cucumber.json"},18 tags = {"@test"}19public class RunnerTest {20}21package com.test.cucumber;22import cucumber.api.CucumberOptions;23import cucumber.api.junit.Cucumber;24import org.junit.runner.RunWith;25@RunWith(Cucumber.class)26@CucumberOptions(27 plugin = {"pretty", "html:target/cucumber-html-report", "json:target/cucumber.json"},28 tags = {"@test"}29public class RunnerTest {30}31package com.test.cucumber;32import cucumber.api.CucumberOptions;33import cucumber.api.junit.Cucumber;34import org.junit.runner.RunWith;35@RunWith(Cucumber.class)36@CucumberOptions(37 plugin = {"pretty", "html:target/cucumber-html-report", "json:target/cucumber.json"},38 tags = {"@test"}39public class RunnerTest {40}41package com.test.cucumber;42import cucumber.api.CucumberOptions;43import cucumber.api.junit.Cucumber;44import org.junit.runner.RunWith;45@RunWith(Cucumber.class)46@CucumberOptions(

Full Screen

Full Screen

CitrusSpringObjectFactory

Using AI Code Generation

copy

Full Screen

1package com.cucumber.stepDefinitions;2import cucumber.api.java.en.Given;3import cucumber.api.java.en.Then;4import cucumber.api.java.en.When;5import cucumber.runtime.java.spring.CitrusSpringObjectFactory;6public class StepDefinition {7 @Given("^I want to write a step with precondition$")8 public void i_want_to_write_a_step_with_precondition() throws Throwable {9 CitrusSpringObjectFactory obj = new CitrusSpringObjectFactory();10 System.out.println(obj);11 }12 @Given("^some other precondition$")13 public void some_other_precondition() throws Throwable {14 }15 @When("^I complete action$")16 public void i_complete_action() throws Throwable {17 }18 @When("^some other action$")19 public void some_other_action() throws Throwable {20 }21 @When("^yet another action$")22 public void yet_another_action() throws Throwable {23 }24 @Then("^I validate the outcomes$")25 public void i_validate_the_outcomes() throws Throwable {26 }27 @Then("^check more outcomes$")28 public void check_more_outcomes() throws Throwable {29 }30}31package com.cucumber.stepDefinitions;32import cucumber.api.java.en.Given;33import cucumber.api.java.en.Then;34import cucumber.api.java.en.When;35import cucumber.runtime.java.spring.CitrusSpringObjectFactory;36public class StepDefinition {37 @Given("^I want to write a step with precondition$")38 public void i_want_to_write_a_step_with_precondition() throws Throwable {39 CitrusSpringObjectFactory obj = new CitrusSpringObjectFactory();40 System.out.println(obj);41 }42 @Given("^some other precondition$")

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in CitrusSpringObjectFactory

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful