Spock Testing a POST Service in Grails

xiimoss

EDIT 2: Figured out how to pass an integer, however my now my test is failing because the value coming back is empty but the render is the correct value on the web page.

EDIT: I've figured out how to pass values by POST using the request.method, however i now can't figure out how to pass an int into the controller using request, as each addParameter expects a string.

I have a convert application in grails, where both the binary and hex service take a parameter via post before converting it to either binary or hex and returning the result. I'm now writing some Spock tests to test each service however i can't seem to figure out how to test the service if the parameter is required to be sent via POST rather than GET.

Binary Convert Service:

def binary() {

    if (request.method == 'POST') {

        if (session.user) {
            Integer number = params.getInt('number')
            String binary = Integer.toBinaryString(number)

            def r = new Results()

            r.customerID = session["id"]
            r.username = session["username"]
            r.ConvertService = "binary"
            r.number = number
            r.result = binary
            r.date = new Date()

            r.time = new Timestamp(System.currentTimeMillis())

            if(r.save()) {
                render binary
            } else {
                render r.getErrors()
            }


        } else {
            redirect(controller: 'main')
        }
    } else {
        response.sendError(405)
    }
}

Spock test:

void "Binary Service should return 1100"() {
    given:
        def convert = new ConvertController()
    when:
        def result = convert.binary(12)
    then:
        result == 1100

}

This is what my test returns right now:

    groovy.lang.MissingMethodException: No signature of method: eadassignment.ConvertController.binary() is applicable for argument types: (java.lang.Integer) values: [12]
Possible solutions: binary(), any(), <init>(), index(), every(), find()

    at org.codehaus.groovy.grails.plugins.web.api.ControllerTagLibraryApi.methodMissing(ControllerTagLibraryApi.java:97)
    at eadassignment.ConvertControllerSpec.Binary Service should return 1100(ConvertControllerSpec.groovy:22)
xiimoss

Figured out all my problems. Last one was because the function checked if a valid user existed which the test was failing for obvious reasons.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to test Grails service using Spock?

From Dev

Spock - Testing Exceptions with Data Tables

From Dev

Grails 2.2.3 Can't Find Spock on Classpath

From Dev

Testing Mock Bean in Spring with Spock

From Dev

Testing that beforeUpdate or beforeInsert are fired in Grails with Spock

From Dev

grails Unit test spock issue

From Dev

Grails: Testing Optimistic Locking

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

Unable to mock Grails Service method when unit testing Controller - MissingMethodException

From Dev

grails spock testing failing with 'java.lang.IllegalArgumentException: ServletContext must not be null'

From Dev

Grails Spock testing Controller and service

From Dev

how to mock a service method inside a controller for unit testing in grails using JUnit and when to use mockController

From Dev

Grails : Spock : Unit testing GORM domain class hooks

From Dev

Grails Spock Mocking an Object

From Dev

Grails Spock Mock Injected Service in Unit Test

From Dev

Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing

From Dev

Testing thread safety fails with Spock

From Dev

Asserting view with Grails Spock Unit Test for Controllers

From Dev

Spock - Testing Exceptions with Data Tables

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

Unable to mock Grails Service method when unit testing Controller - MissingMethodException

From Dev

How to use spock to test a service with real transactions in Grails application?

From Dev

Gradle Plugin Spock Testing

From Dev

Grails Spock Mocking an Object

From Dev

Spock - mocking external service

From Dev

Spock validation issues for integration tests in grails project

From Dev

Grails Spock GORM returns an empty list

From Dev

Grails Can't inject service to domain when test with Spock

From Dev

How to mock an interface parameter for a service method using Spock and Grails 2?

Related Related

  1. 1

    How to test Grails service using Spock?

  2. 2

    Spock - Testing Exceptions with Data Tables

  3. 3

    Grails 2.2.3 Can't Find Spock on Classpath

  4. 4

    Testing Mock Bean in Spring with Spock

  5. 5

    Testing that beforeUpdate or beforeInsert are fired in Grails with Spock

  6. 6

    grails Unit test spock issue

  7. 7

    Grails: Testing Optimistic Locking

  8. 8

    How to mock render in (Spock, Grails) unit testing Taglib?

  9. 9

    Unable to mock Grails Service method when unit testing Controller - MissingMethodException

  10. 10

    grails spock testing failing with 'java.lang.IllegalArgumentException: ServletContext must not be null'

  11. 11

    Grails Spock testing Controller and service

  12. 12

    how to mock a service method inside a controller for unit testing in grails using JUnit and when to use mockController

  13. 13

    Grails : Spock : Unit testing GORM domain class hooks

  14. 14

    Grails Spock Mocking an Object

  15. 15

    Grails Spock Mock Injected Service in Unit Test

  16. 16

    Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing

  17. 17

    Testing thread safety fails with Spock

  18. 18

    Asserting view with Grails Spock Unit Test for Controllers

  19. 19

    Spock - Testing Exceptions with Data Tables

  20. 20

    How to mock render in (Spock, Grails) unit testing Taglib?

  21. 21

    Unable to mock Grails Service method when unit testing Controller - MissingMethodException

  22. 22

    How to use spock to test a service with real transactions in Grails application?

  23. 23

    Gradle Plugin Spock Testing

  24. 24

    Grails Spock Mocking an Object

  25. 25

    Spock - mocking external service

  26. 26

    Spock validation issues for integration tests in grails project

  27. 27

    Grails Spock GORM returns an empty list

  28. 28

    Grails Can't inject service to domain when test with Spock

  29. 29

    How to mock an interface parameter for a service method using Spock and Grails 2?

HotTag

Archive