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

Spring MVC Test Framework ignores custom formatter [SPR-10167] #14800

spring-projects-issues opened this issue Jan 12, 2013 · 2 comments


Copy link

@spring-projects-issues spring-projects-issues commented Jan 12, 2013

Piotr Jagielski opened SPR-10167 and commented

Spring MVC Test Framework ignores custom formatter.

Given following controller:

public class SampleController {

    @RequestMapping(value = "/test", method = GET)
    public String test(SampleEnum sampleEnum) {
        return sampleEnum.toString();

And following enum constant:

public enum SampleEnum {


And following entry in Java Spring configuration file extending from WebMvcConfigurationSupport:

public void addFormatters(FormatterRegistry registry) {
    registry.removeConvertible(String.class, Enum.class);
    registry.addConverterFactory(new CustomEnumConverterFactory());

That uses custom enum formatter to allow using lowercase enum constants in requests parameters while continuing to use uppercase enum constants:

public class CustomEnumConverterFactory implements ConverterFactory<String, Enum> {

    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
        return new StringToEnum(targetType);

    private static class StringToEnum<T extends Enum> implements Converter<String, T> {

        private final Class<T> enumType;

        public StringToEnum(Class<T> enumType) {
            this.enumType = enumType;

        public T convert(String source) {
            if (source.length() == 0) {
                return null;
            return (T) Enum.valueOf(this.enumType, source.toUpperCase().trim());

Then when running the container both requests below result in printing enum constant:


But when trying to verify that using MVC Test Framework the first test fails:

@ContextConfiguration(classes = {WebConfiguration.class})
public class SampleControllerTest {

    private MockMvc mockMvc;

    public void setUp() throws Exception {
        this.mockMvc = MockMvcBuilders.standaloneSetup(new SampleController()).build();

    public void shouldReturnSuccessfulResponseFromUpperCaseEnum() throws Exception {

    public void shouldReturnSuccessfulResponseFromLowerCaseEnum() throws Exception {

The status returned in the first test when using MVC Test framework is 400 BAD REQUEST.

Affects: 3.2 GA

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 14, 2013

Rossen Stoyanchev commented

standaloneSetup is not the right builder if you want to load your Spring configuration. You'll want to use the webAppContextSetup instead. See the reference documentation.


Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 15, 2013

Piotr Jagielski commented

Thank you. Indeed the documentation makes it very clear. I don't know how I missed that part.


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

Successfully merging a pull request may close this issue.

None yet
2 participants