citrusframework / citrus-samples

Citrus sample projects using Java DSL tests and Spring Java configuration
https://citrusframework.org/samples/
71 stars 125 forks source link

Autowire doesn't work #35

Closed jayasree44 closed 6 years ago

jayasree44 commented 6 years ago

Hi I've created an application similar to it. But autowire doesn't work in my application. And also when I use citrusReporter as a plugin I'm getting exception.

My definitions look as below

@RunWith(Cucumber.class)
@CucumberOptions(
        strict = true,
        features = "classpath:Features",
        plugin = {/*"com.consol.citrus.cucumber.CitrusReporter",*/"pretty", "junit:target/cucumber.xml", "json:target/cucumber.json" },
        glue = {"com.consol.citrus.cucumber.step.designer.core","com.consol.citrus.cucumber.step.designer.http","com.................cucumber.stepDefinition"})
public class CucumberClientIT {
}

@SuppressWarnings("SpringJavaAutowiringInspection")
@ContextConfiguration(classes = CitrusSpringConfig.class)
public class Steps {

    @CitrusResource
    private TestDesigner designer;

    @Autowired
    //@CitrusEndpoint  -->WHEN I use this instead autowiring it gives me null pointer when I make a call at designer.http
    private HttpClient todoListClient;

@And("^I make a \"([^\"]*)\" request to the \"([^\"]*)\"$")
    public void i_make_a_request_to_the(String requestType, String URL) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        designer.http()
            .client(todoListClient)
            .send()
            .get("/demo/?name=jc&city=laramie&stateProvince=WY");   
    }
}
@Configuration
@PropertySource("citrus.properties")
public class EndpointConfig {

    @Bean
    public GlobalVariables globalVariables() {
        GlobalVariables variables = new GlobalVariables();
        variables.getVariables().put("project.name", "Citrus Integration Tests");
        return variables;
    }

    @Bean
    public HttpClient todoListClient() {
        return CitrusEndpoints.http()
                .client()
                .requestUrl("http://localhost:8080")
                .build();
    }
}
Cucumber.properties:
cucumber.api.java.ObjectFactory=cucumber.runtime.java.spring.CitrusSpringObjectFactory

https://github.com/citrusframework/citrus-samples/blob/master/samples-cucumber/sample-cucumber-spring/java-dsl/src/test/java/todo/TodoSteps.java

cucumber.runtime.CucumberException: Error creating bean with name 'com.............cucumber.stepDefinition.Steps': Unsatisfied dependency expressed through field 'todoListClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.consol.citrus.http.client.HttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at cucumber.runtime.java.spring.SpringFactory.getInstance(SpringFactory.java:251)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:299)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:93)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:37)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:98)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.esrx.eds.location.cucumber.stepDefinition.Steps': Unsatisfied dependency expressed through field 'todoListClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.consol.citrus.http.client.HttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:351)
    at cucumber.runtime.java.spring.GlueCodeScope.get(GlueCodeScope.java:14)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:346)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at cucumber.runtime.java.spring.SpringFactory.getInstance(SpringFactory.java:249)
    ... 31 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.consol.citrus.http.client.HttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 44 more
jayasree44 commented 6 years ago

If I remove the autowiring and create instance of HttpClient in my class and when I make designer.http().client(todoListClient().send().get(/demo)) call. It throws me null pointer exception.

java.lang.NullPointerException at com.esrx.eds.location.cucumber.stepDefinition.Steps.i_make_a_request_to_the(Steps.java:81) at ✽.And I make a "GET" request to the "http://localhost:8080"(Features/LatLong.feature:10)

This is occurring due to designer.http()

svettwer commented 6 years ago

Hi!

Have you added a citrus-application.properties file to your resource folder containing the classpath to the Java configuration file? E.g. citrus.spring.java.config=com.consol.citrus.samples.todolist.EndpointConfig

BR, Sven

svettwer commented 6 years ago

BTW:

designer.http().client(todoListClient().send().get(/demo))

This is not how the API is designed. This shouldn't even compile, because /demo is not a string and the client message accepts a HttpClient and not a HttpClientRequestActionBuilder that would be returned from get.

Try this one

        designer.http()
                .client(todoListClient())
                .send()
                .get("/demo");

with

       private HttpClient todoListClient() {
        return CitrusEndpoints.http()
                .client()
                .requestUrl("http://foo.bar")
                .build();
    }

or simply inline the url, if you just want to test around.

        designer.http()
                .client("http://foo.bar")
                .send()
                .get("/demo");
svettwer commented 6 years ago

Closed because of inactivity.