Skip to content

Add status ResultMatcher to express status code ranges [SPR-11424] #16051

Closed
@spring-projects-issues

Description

@spring-projects-issues
Collaborator

Oliver Drotbohm opened SPR-11424 and commented

The status() method of MockMvcResultMatchers exposes a method per status code. This works fine if I want to express a particular code expected.

However, if I want to express a certain type of code (e.g. 1xx, 2xx etc.) I have to fall back to something like this:

….andExpect(status().is(both(greaterThanOrEqualTo(200)).and(lessThan(300))));

A short

–.andExpect(status().inRangeSuccess()); // expecting 2xx

would be easier to write and read. The method could also be named isSuccess() but I think it might make sense to clearly separate the range check methods from the ones checking for an individual status code.


Affects: 4.0.1

Activity

spring-projects-issues

spring-projects-issues commented on Feb 13, 2014

@spring-projects-issues
CollaboratorAuthor

Rossen Stoyanchev commented

Sounds good. HttpStatus has the Series enum along those lines. We could add a method for each of them. How about:

–.andExpect(status().is1xxInformational())
–.andExpect(status().is2xxSuccess())
–.andExpect(status().is3xxRedirection())
–.andExpect(status().is4xxClientError())
–.andExpect(status().is5xxServerError())
spring-projects-issues

spring-projects-issues commented on Mar 10, 2014

@spring-projects-issues
CollaboratorAuthor

Balakrishnan commented

In the newly added methods, the order of parameters in assert seems to be reversed.

Also, would it be possible to add these helper methods in org.springframework.http.HttpStatus also? That would be quite helpful.

spring-projects-issues

spring-projects-issues commented on Mar 12, 2014

@spring-projects-issues
CollaboratorAuthor

Rossen Stoyanchev commented

thanks see these two commits.

added this to the 4.0.2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rstoyanchev@spring-projects-issues

      Issue actions

        Add status ResultMatcher to express status code ranges [SPR-11424] · Issue #16051 · spring-projects/spring-framework