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

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

Comments

@spring-projects-issues
Copy link
Collaborator

@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:

@Controller
public class SampleController {

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

And following enum constant:

public enum SampleEnum {

    VALUE1,
    VALUE2
}

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

@Override
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> {

    @SuppressWarnings("unchecked")
    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:

http://localhost:8080/test?sampleEnum=VALUE1
http://localhost:8080/test?sampleEnum=value1

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

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

    private MockMvc mockMvc;

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

    @Test
    public void shouldReturnSuccessfulResponseFromUpperCaseEnum() throws Exception {
        mockMvc.perform(get("/test?sampleEnum=VALUE1")).andExpect(status().isOk()).andExpect(content().string("VALUE1"));;
    }

    @Test
    public void shouldReturnSuccessfulResponseFromLowerCaseEnum() throws Exception {
        mockMvc.perform(get("/test?sampleEnum=value1")).andExpect(status().isOk()).andExpect(content().string("VALUE1"));
    }
}

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


Affects: 3.2 GA

@spring-projects-issues
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.

Loading

@spring-projects-issues
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.

Loading

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

Successfully merging a pull request may close this issue.

None yet
2 participants