Spock & Spock Reports how retrieve the MockHttpServletResponse (from Spring MVC Testing) for the where block?

414 Views Asked by At

I am working with

  • Gradle
  • Spring MVC Testing
  • Spock Core
  • Spock Spring
  • Spock Reports

In some Specification I have the following:

I was able to do the following:

def ResultActions resultActions // instance variables
def MockHttpServletResponse mockHttpServletResponse // instance variables

…
@Unroll(value="#methodNameReport Expected")    
def "something"(){

….
when: "GET"

    resultActions = mockMvc.perform(get(url, PersonaControllerHelper.ID)).andDo(print())
    mockHttpServletResponse = resultActions.andReturn().getResponse()
    println mockHttpServletResponse
    println mockHttpServletResponse.getStatus()
}

Until here, I am able to get in peace the MockHttpServletResponse class and print some data. Therefore MockHttpServletResponse is not null.

But later:

where: "."

    urlReport << [PersonaXmlFindOneWithProducesControllerTemplateFactory.findOneRequestParamById(PersonaControllerHelper.ID)]
    classNameReport << [PersonaXmlFindOneWithProducesController.class.getSimpleName()]
    methodNameReport << ["findOneRequestParamById"]
    statusReport << [mockHttpServletResponse.getStatus()]
    mediaTypeReport << [MediaType.APPLICATION_XML_VALUE]

I get

PersonaXmlFindOneWithProducesControllerTest.groovy: 90: 
Only @Shared and static fields may be accessed from here @ line 90, column 21.
            statusReport << [mockHttpServletResponse.getStatus()]

Even If I declare the variable static or with @Shared

I get

…..PersonaXmlFindOneWithProducesControllerTest > findOneRequestParamById Expected FAILED
    java.lang.NullPointerException at PersonaXmlFindOneWithProducesControllerTest.groovy:90

The wonderful NullPointerException.

What configuration settings is need it?

Alpha

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

def String url

def String urlReport
def String methodNameReport
def String statusReport

def ResultActions resultActions 
def @Shared MockHttpServletResponse mockHttpServletResponse

def setup(){
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Unroll(value="#methodNameReport Expected")
def "findOneRequestParamById Expected"(){

    given: "URL: #urlReport"

        url = PersonaXmlFindOneControllerURLURIFactory.findOneRequestParamById()

    when: "GET"

        resultActions = mockMvc.perform(get(url, PersonaControllerHelper.ID)).andDo(print())
        mockHttpServletResponse = resultActions.andReturn().getResponse()
        println mockHttpServletResponse //prints
        println mockHttpServletResponse.getStatus() //prints

    then: "Exists - Status: #statusReport"

        resultActions.andExpect(status().isOk())

    where: "."

        urlReport << [PersonaXmlFindOneControllerTemplateFactory.findOneRequestParamById(PersonaControllerHelper.ID)]
        methodNameReport << ["findOneRequestParamById"]
        statusReport << [mockHttpServletResponse.getStatus()] // Fails
}
0

There are 0 best solutions below