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

Regression: SpringHandlerInstantiator breaks stand-alone support in the MVC Test framework [SPR-13375] #17957

Closed
spring-projects-issues opened this issue Aug 21, 2015 · 8 comments
Assignees
Labels
in: test in: web status: backported type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Sam Brannen opened SPR-13375 and commented

The SpringHandlerInstantiator introduced in #15394 always attempts to create new beans using its AutowireCapableBeanFactory. In the case of a StubWebApplicationContext, however, this results in an UnsupportedOperationException being thrown.

This behavior breaks support for JSON serialization in conjunction with the default configuration of the MockMvcBuilders.standaloneSetup() support in the Spring MVC Test framework since it internally uses a StubWebApplicationContext.

See the linked discussion on Stack Overflow for details.

Analysis

The following code in WebMvcConfigurationSupport.addDefaultHttpMessageConverters() is what causes this behavior:

if (jackson2Present) {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
	messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}

The fact that it supplies the ApplicationContext results in a SpringHandlerInstantiator being created in Jackson2ObjectMapperBuilder.configure(), even though a SpringHandlerInstantiator makes no sense for the stand-alone test setup in MVC Test.


Affects: 4.1.3

Reference URL: http://stackoverflow.com/questions/32101611/spring-mockmvc-unsupportedoperationexception-after-upgrading-to-new-spring-versi/32140898

Referenced from: commits spring-attic/spring-framework-issues@028ace9

Backported to: 4.1.8

0 votes, 6 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Juergen Hoeller commented

Hmm, we should consider a revision of StubWebApplicationContext here, simply creating instances without any actual autowiring... Not sure whether I want any AutowireCapableBeanFactory calls in production code to be defensive about UnsupportedOperationException.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Sam Brannen commented

FYI: I updated my response on Stack Overflow.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Sam Brannen commented

Daniel Lynch, yes, you should be able to work around this issue by registering your own MappingJackson2HttpMessageConverter via StandaloneMockMvcBuilder.setMessageConverters().

See my answer on the linked Stack Overflow discussion for details.

Something like the following should work:

ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();

mockMvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter(objectMapper)).build();

Please let us know if that works for you!

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Sam Brannen commented

Juergen Hoeller, agreed: a revision of StubWebApplicationContext would likely be the more robust solution.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Daniel Lynch commented

Sam Brannen, thank you, your suggestion did work.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2015

Sam Brannen commented

Daniel Lynch, great!

We appreciate the feedback.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2015

Juergen Hoeller commented

Sébastien Deleuze, I'll put this on my plate since it's really just about StubWebApplicationContext's implementation at this point.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 24, 2015

Sébastien Deleuze commented

Juergen Hoeller Feel free to have a look to this repro project and reuse this commit not yet merged in master if you think this is ok.

<T> T createBean(Class<T> beanClass) is the only one needed, but I also updated Object createBean(Class beanClass, int autowireMode, boolean dependencyCheck) to be consistent, even if we are just creating bean instances without any autowiring.

@spring-projects-issues spring-projects-issues added type: bug in: test status: backported in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.2.1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test in: web status: backported type: bug
Projects
None yet
Development

No branches or pull requests

2 participants