Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to hard code the port in integration tests in Grails 3.1.19 #10039

Closed
sdelamo opened this issue Jul 11, 2016 · 3 comments
Closed

How to hard code the port in integration tests in Grails 3.1.19 #10039

sdelamo opened this issue Jul 11, 2016 · 3 comments

Comments

@sdelamo
Copy link
Contributor

sdelamo commented Jul 11, 2016

From Grails 3.1.9 relase notes:

Integration Test Port Integration and functional tests now run on a random port instead of the same port as the application by default. This is to avoid port conflict problems when the application is already running. Integration tests that hard code the port will need to be altered to use the serverPort property of tests marked with @Integration

In particular I am asking about this part:

Integration tests that hard code the port will need to be altered to use the serverPort property of tests marked with @Integration`

I don't understand exactly how to use the serverPort which is mentioned.

I've tried editing application.yml

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    test:
        server: 
            port: 8090
        grails:
            serverUrl: http://localhost:8090

or

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    test:
        grails:
            serverUrl: http://localhost:8090
            serverPort: 8090

or

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    test:
        grails:
            serverUrl: http://localhost:8090
            server:
                port: 8090

Steps to Reproduce

  • Create a new grails app with 3.1.19
  • Create an integration test with the source mentioned in the Expected behaviour block of this issue.
  • Modify the application.yml file in any of the described ways.

Expected Behaviour

I expect this test to pass:

import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*
import org.springframework.beans.factory.annotation.Value

@Integration
@Rollback
class TestControllerSpec extends Specification {

    @Value('${local.server.port}')
    Integer serverPort

    void "test server port is not random"() {
        expect: 
        serverPort == 8090
    }
}

Actual Behaviour

grails test-app -integration fails

Environment Information

  • Operating System: OSX El Capitan
  • Grails Version: 3.1.9
  • JDK Version: 1.8.0_45
@graemerocher
Copy link
Member

As mentioned in the change... integration tests now run on a random port. Not the one configured in application.yml. This is a more sensible default.

If you don't want this behaviour you will have to specify @WebIntegrationTest(randomPort=false) at the top of your test.

@sdelamo
Copy link
Contributor Author

sdelamo commented Jul 11, 2016

Thanks @graemerocher

I read what you said in the change. Clicked in the @Integration annotation but did not see any parameter there. And I did not know about the @WebIntegrationTest(randomPort=false) syntax.

@tom-mi
Copy link

tom-mi commented Dec 19, 2016

Is there any way to configure this globally?
As far as I understand: before the change, I would have had to set server.port=0 in a single place to get a random port. Now I need to add the annotation mentioned above on every single test class to get a static port. Is that correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants