cjstehno / ersatz

🤖 A simulated HTTP server for testing client code with configurable responses.
https://cjstehno.github.io/ersatz
Apache License 2.0
47 stars 5 forks source link

Error executing "Getting Started" spec #78

Closed rmorrise closed 7 years ago

rmorrise commented 7 years ago

This tool looks awesome! But unfortunately, I can't get it to work at all.

In the Getting Started section of the guide, the sample Specification is full of typos. I corrected a few of them:

import com.stehno.ersatz.ErsatzServer
import spock.lang.Specification

class HelloSpec extends Specification {

    def 'say hello'(){
        setup:
        ErsatzServer ersatz = new ErsatzServer()

        ersatz.expectations {
            get('/say/hello'){
                called(1)
                query 'name','Ersatz'
                responder {
                    content 'Hello Ersatz','text/plain'
                }
            }
        }

        ersatz.start()

        when:
        String result = "${ersatz.serverUrl}/say/hello?name=Ersatz".toURL().text

        then:
        result == 'Hello Ersatz'

        and:
        ersatz.verify()

        cleanup:
        ersatz.stop()
    }
}

...but when I run this spec, I get an error:

groovy.lang.MissingPropertyException: No such property: serverUrl for class: com.stehno.ersatz.ErsatzServer

    at com.cscglobal.everest.kb.qna.HelloSpec.say hello(HelloSpec.groovy:25)

I tried changing the URLs to be hard-coded: get('http://test/say/hello') String result = "http://test/say/hello?name=Ersatz".toURL().text

...but then I get a socket error, which suggests to me that Ersatz is not running properly:

java.net.ConnectException: Connection timed out: connect

    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at com.cscglobal.everest.kb.qna.HelloSpec.say hello(HelloSpec.groovy:25)

Here is the page where I got this sample spec: http://stehno.com/ersatz/asciidoc/html5/#_getting_started

I am using Grails 3.3.0, following this guide: http://guides.grails.org/grails-mock-http-server/guide/index.html#ersatz

cjstehno commented 7 years ago

Erg... looks like a case of stale documentation examples. Sorry about that. The User Guide Getting Started example works now. Thanks for pointing out the bad code.

One thing to consider when using Grails is that if you are using the Undertow embedded server, you may have version collisions with the version used in Ersatz - if you run into that issue, use the "safe" (shadow) jar (see Shadow Jar in the User Guide).

I am going to close this issue. If you feel that there is still a problem, please feel free to re-open the issue.