Grails

Integration Testing json response for controller , single test passes but same test in multiple tests fails

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.1.1
  • Fix Version/s: 1.2.1
  • Component/s: Testing
  • Labels:
    None
  • Testcase included:
    yes

Description

I have the following controller classes

class MyController {

def index = { }

def list = {
withFormat{
json{
render(contentType:"text/json"){ success(true) }
}
}
}

def info = {
withFormat{
json{
render(contentType:"text/json"){ success(true) } }
}
}

}
}

with the following integration test

package ssmithstone

import grails.test.GrailsUnitTestCase

class MyControllerIntegrationTests extends GrailsUnitTestCase {
MyController controller

protected void setUp() { super.setUp() controller = new MyController() }

protected void tearDown() { super.tearDown() }
void testJSONResponseForList() {

controller.request.contentType = "text/json"

controller.list()

String actualJSON = controller.response.contentAsString

assertEquals "{\"success\":true}" , actualJSON
}

void testJSONResponseForInfo(){

controller.request.contentType = "text/json"

controller.info()

String actualJSON = controller.response.contentAsString

assertEquals "{\"success\":true}" , actualJSON

}

}

now if you run

grails test-app -integration you get

Running tests of type 'integration'
-------------------------------------------------------
Running 2 integration tests...
Running test ssmithstone.MyControllerIntegrationTests...
testJSONResponseForInfo...FAILED
Tests Completed in 334ms ...
-------------------------------------------------------
Tests passed: 1
Tests failed: 1
-------------------------------------------------------

however if you run

grails test-app -integration ssmithstone.MyControllerIntegration.testJSONResponseForInfo

Starting integration tests ...
Running tests of type 'integration'
-------------------------------------------------------
Running 1 integration test...
Running test ssmithstone.MyControllerIntegrationTests...PASSED
Tests Completed in 245ms ...
-------------------------------------------------------
Tests passed: 1
Tests failed: 0
-------------------------------------------------------

so there is a bug there somewhere.

Issue Links

Activity

Hide
Benjamin R. Doerr added a comment -

This is not a issue. Please extend GroovyTestCase for your integration tests.

Show
Benjamin R. Doerr added a comment - This is not a issue. Please extend GroovyTestCase for your integration tests.
Hide
Stephen Smithstone added a comment - - edited

seems the grails 1.1.1 create-integration-test script generate the test class extending from GrailsUnitTestCase and not GroovyTestCase, would that be classed a bug or is desired feature ?

Show
Stephen Smithstone added a comment - - edited seems the grails 1.1.1 create-integration-test script generate the test class extending from GrailsUnitTestCase and not GroovyTestCase, would that be classed a bug or is desired feature ?
Hide
James Beamish-White added a comment - - edited

I also get this in 1.2.0, for classes that extends GroovyTestCase.
However, I realise that my implementation of the controller uses:

if (request.format == "json")

rather than withFormat

Show
James Beamish-White added a comment - - edited I also get this in 1.2.0, for classes that extends GroovyTestCase. However, I realise that my implementation of the controller uses: if (request.format == "json") rather than withFormat
Hide
James Beamish-White added a comment -

OK, I've switched to withFormat and it still occurs.

It seems that in integration tests, when run as an individual test, it correctly detects the format of the unit test as json. If it's run as part of the whole --integration suite, it's detected as a standard web (html) request - which is supported by logging:

controller withFormat code:
...
json {
log.debug "JSON request detected"
...
html {
log.debug "create() Web request detected"
...

test suite output:
...

  • create() Web request detected
    testCreateOrderWithJsonSucceeds...FAILED
    ...

individual test output:
...

  • JSON request detected
    ...
    PASSED
    ...
Show
James Beamish-White added a comment - OK, I've switched to withFormat and it still occurs. It seems that in integration tests, when run as an individual test, it correctly detects the format of the unit test as json. If it's run as part of the whole --integration suite, it's detected as a standard web (html) request - which is supported by logging: controller withFormat code: ... json { log.debug "JSON request detected" ... html { log.debug "create() Web request detected" ... test suite output: ...
  • create() Web request detected testCreateOrderWithJsonSucceeds...FAILED ...
individual test output: ...
  • JSON request detected ... PASSED ...
Hide
Stephen Smithstone added a comment -

james can you upload a test project please

Show
Stephen Smithstone added a comment - james can you upload a test project please
Hide
Luke Daley added a comment -

Closing this and moving to GRAILS-5757 for the create-integration-test issue.

Show
Luke Daley added a comment - Closing this and moving to GRAILS-5757 for the create-integration-test issue.
Hide
Luke Daley added a comment -

Scratch that, need to work out why this is happening.

Show
Luke Daley added a comment - Scratch that, need to work out why this is happening.

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: