0

[在此处输入图片描述][1]我创建了以下柑橘测试用例来测试Rest 客户端和服务器之间的基本连接:

    @Test
    @CitrusTest
    fun httpActionTest() {

        variable("username", "user")
        variable("password","password")
        http().client("httpClient")
                .send()
                .post("/api/authenticate")
                .messageType(MessageType.JSON)
                .contentType("application/json")
                .payload("{ \"username\": \"\${username}\", \"password\": \"\${password}\"}");

        http().client("httpClient")
                .receive()
                .response(HttpStatus.OK)
                .validate("$.token","asasasasas")
    }

    @CitrusTest
    fun httpServerActionTest() {
        http().server("httpServer")
                .receive()
                .post("/api/authenticate")
                .payload("{ \"username\": \"\${username}\", \"password\": \"\${password}\"}")
                .contentType("application/json")
                .accept("application/json")
                .extractFromPayload("username", "username")
                .extractFromPayload("password", "password")
                .validate("$.username", "user")
                .validate("$.password","pass")
    http().server("httpServer")
            .send()
            .response(HttpStatus.OK)
            .payload("{\"token\": \"lsdkfjkh8sdfg98zsd\"}")
            .version("HTTP/1.1")
            .contentType("application/json")
}

我在citrux-context.xml中定义了服务器和客户端端点,如下所示:

<citrus-http:client id="httpClient"
                    request-url="http://localhost:8080"
                    request-method="GET"
                    content-type="application/json"
                    charset="UTF-8"
                    timeout="60000"/>
<citrus-http:server id="httpServer"
                    port="8080"
                    auto-start="true"
                    resource-base="src/test/resources"/>

通过 IntelliJ 执行时,观察到以下日志:

    INFO: Loading XML bean definitions from URL [file:/home/jass/intersales/jk-magento/magento2-auth-service/target/test-classes/citrus-context.xml]
    [main] INFO org.eclipse.jetty.util.log - Logging initialized @9851ms to org.eclipse.jetty.util.log.Slf4jLog
    [main] INFO org.eclipse.jetty.server.Server - jetty-9.4.6.v20170531
    [main] INFO org.eclipse.jetty.server.handler.ContextHandler.ROOT - Initializing Spring FrameworkServlet 'httpServer-servlet'
    Oct 23, 2017 8:49:45 AM com.consol.citrus.http.servlet.CitrusDispatcherServlet initServletBean
    INFO: FrameworkServlet 'httpServer-servlet': initialization started
    Oct 23, 2017 8:49:45 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    INFO: Refreshing WebApplicationContext for namespace 'httpServer-servlet-servlet': startup date [Mon Oct 23 08:49:45 CEST 2017]; root of context hierarchy
    Oct 23, 2017 8:49:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [com/consol/citrus/http/citrus-servlet-context.xml]
    Oct 23, 2017 8:49:46 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
    ...
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'httpServer-servlet-servlet': startup date [Mon Oct 23 08:49:45 CEST 2017]; root of context hierarchy
    Oct 23, 2017 8:49:47 AM com.consol.citrus.http.servlet.CitrusDispatcherServlet initServletBean
    INFO: FrameworkServlet 'httpServer-servlet': initialization completed in 1570 ms
    [main] INFO org.eclipse.jetty.server.handler.ContextHandler - Started o.e.j.s.ServletContextHandler@1bb1fde8{/,file:///home/jass/intersales/jk-magento/magento2-auth-service/src/test/resources/,AVAILABLE}
    [main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@1286528d{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
    [main] INFO org.eclipse.jetty.server.Server - Started @12166ms
    [main] INFO com.consol.citrus.http.server.HttpServer - Started server: httpServer
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus -        .__  __                       
    [main] INFO com.consol.citrus.Citrus -   ____ |__|/  |________ __ __  ______
    [main] INFO com.consol.citrus.Citrus - _/ ___\|  \   __\_  __ \  |  \/  ___/
    [main] INFO com.consol.citrus.Citrus - \  \___|  ||  |  |  | \/  |  /\___ \ 
    [main] INFO com.consol.citrus.Citrus -  \___  >__||__|  |__|  |____//____  >
    [main] INFO com.consol.citrus.Citrus -      \/                           \/
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - C I T R U S  T E S T S  2.7.2
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - 

    [main] INFO com.consol.citrus.Citrus - BEFORE TEST SUITE: SUCCESS
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.actions.EchoAction - Today is: 23.10.2017
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - TEST SUCCESS VerticleCitrusTest.echoToday (de.intersales.qbus2)
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 
    [qtp191568263-12] INFO com.consol.citrus.channel.ChannelSyncProducer - Message was sent to channel: 'httpServer.inbound'
    [qtp191568263-12] WARN com.consol.citrus.channel.ChannelEndpointAdapter - Reply timed out after 1000ms. Did not receive reply message on reply channel
    [main] INFO com.consol.citrus.http.client.HttpClient - HTTP message was sent to endpoint: 'http://localhost:8080/magento2/authenticate'

    [main] INFO com.consol.citrus.validation.xml.DomXmlMessageValidator - XML message validation successful: All values OK
    [main] INFO com.consol.citrus.validation.DefaultMessageHeaderValidator - Message header validation successful: All values OK

    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - TEST SUCCESS VerticleCitrusTest.httpActionTest (de.intersales.qbus2)
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 

    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - AFTER TEST SUITE: SUCCESS
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - CITRUS TEST RESULTS
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus -  VerticleCitrusTest.echoToday ................................... SUCCESS
    [main] INFO com.consol.citrus.Citrus -  VerticleCitrusTest.httpActionTest .............................. SUCCESS
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - TOTAL:   2
    [main] INFO com.consol.citrus.Citrus - FAILED:  0 (0.0%)
    [main] INFO com.consol.citrus.Citrus - SUCCESS: 2 (100.0%)
    [main] INFO com.consol.citrus.Citrus - 
    [main] INFO com.consol.citrus.Citrus - ------------------------------------------------------------------------
    [main] INFO com.consol.citrus.report.HtmlReporter - Generated HTML test report

但是通过mvn clean verify执行时出现错误,并出现以下错误:

    [main] ERROR com.consol.citrus.Citrus - TEST FAILED VerticleCitrusTest.httpActionTest <de.intersales.qbus2> Nested exception is: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'httpClient' available
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
       ...

`

非常感谢任何建议或帮助。

已编辑以下是我的项目结构[资源放置] [1]:https ://i.stack.imgur.com/aVabX.png

4

1 回答 1

0

我在您的代码和设置中发现了多个问题。首先,httpServerActionTest()缺少@Test注释。如果没有放在类级别,则需要在测试类中的每个方法上重复此注释。

其次,完整的测试结构对我来说没有多大意义。在httpActionTest()测试中,您向服务器发送客户端请求,而在httpServerActionTest()您收到与服务器完全相同的请求并使用 Citrus 验证其内容。您的测试同时是客户端和服务器。感觉我不对!特别是这个测试设置永远不会工作,因为 Http 本质上是一个同步协议,httpActionTest()如果不执行就无法成功httpServerActionTest()。然后,您将在客户端收到超时异常。这仅在两种方法彼此并行执行的情况下才有效。

关于 Maven 失败:citrux-context.xml拼写错误(citru x vs. citru s)。在我看来,该文件没有作为资源正确添加到 Maven 项目中。您是否保留了默认的 Maven 目录布局?

再一次,完整的测试设置并没有向我阐明其目的。

于 2017-10-23T11:17:58.770 回答