I have a unit test that tests one of my controllers. I had it set up to test a POST to my rest endpoint with json that included a field that is not specified in the domain model (unknown property). I'm expecting my api to fail and return an 400 error, but when I switch from Spring Boot 1.2.0.M1 to 1.2.0.M2, this unit test starts to fail returning a 201 instead of the expected 400 error. I read in the release notes that this property spring.jackson.deserialization.fail-on-unknown-properties is now set to FALSE by default, which is what's causing my issue. But setting spring.jackson.deserialization.fail-on-unknown-properties=true in my application.properties file seems to have no effect. I've even tried setting it on the domain class itself with the JsonIgnore annotation but that also seems to have no effect.
I will see if I can build a quick example application that illustrates the problem I'm running into but can't get to that right away.
A sample application would be very helpful.
I was able to create a simple Spring Boot application. I also narrowed it down to failing when you upgrade from 1.2.0.M1 to 1.2.0.M2. Not RC2. How do I add my sample app here? Should I add it to a github repo? Or can I zip it and attach it here?
You can send a pull resist request to
https://github.com/spring-projects/spring-boot-issues.
On Feb 3, 2015 6:21 PM, "kmandeville" [email protected] wrote:
I was able to create a simple Spring Boot application. I also narrowed it
down to failing when you upgrade from 1.2.0.M1 to 1.2.0.M2. Not RC2. How do
I add my sample app here? Should I add it to a github repo? Or can I zip it
and attach it here?โ
Reply to this email directly or view it on GitHub
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72778417
.
Sorry for my ignorance, but where does the gh1054 piece come from? Do I
create a directory there, add my app with packages like
org.springframework.boot.issues.gh1054?
I wrote up an application with com.example as the package structure.
On Tue, Feb 3, 2015 at 9:29 PM, Phil Webb [email protected] wrote:
You can send a pull resist request to
https://github.com/spring-projects/spring-boot-issues.
On Feb 3, 2015 6:21 PM, "kmandeville" [email protected] wrote:I was able to create a simple Spring Boot application. I also narrowed it
down to failing when you upgrade from 1.2.0.M1 to 1.2.0.M2. Not RC2. How
do
I add my sample app here? Should I add it to a github repo? Or can I zip
it
and attach it here?โ
Reply to this email directly or view it on GitHub
<
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72778417.
โ
Reply to this email directly or view it on GitHub
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72779200
.
Please use gh-2446 for the top folder. It doesn't matter about the package
names.
Thanks.
On Feb 3, 2015 6:35 PM, "kmandeville" [email protected] wrote:
Sorry for my ignorance, but where does the gh1054 piece come from? Do I
create a directory there, add my app with packages like
org.springframework.boot.issues.gh1054?
I wrote up an application with com.example as the package structure.On Tue, Feb 3, 2015 at 9:29 PM, Phil Webb [email protected]
wrote:You can send a pull resist request to
https://github.com/spring-projects/spring-boot-issues.
On Feb 3, 2015 6:21 PM, "kmandeville" [email protected] wrote:I was able to create a simple Spring Boot application. I also narrowed
it
down to failing when you upgrade from 1.2.0.M1 to 1.2.0.M2. Not RC2.
How
do
I add my sample app here? Should I add it to a github repo? Or can I
zip
it
and attach it here?โ
Reply to this email directly or view it on GitHub
<https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72778417
.
โ
Reply to this email directly or view it on GitHub
<
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72779200.
โ
Reply to this email directly or view it on GitHub
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72779692
.
Ah ok thank you!
On Tue, Feb 3, 2015 at 9:53 PM, Phil Webb [email protected] wrote:
Please use gh-2446 for the top folder. It doesn't matter about the package
names.Thanks.
On Feb 3, 2015 6:35 PM, "kmandeville" [email protected] wrote:
Sorry for my ignorance, but where does the gh1054 piece come from? Do I
create a directory there, add my app with packages like
org.springframework.boot.issues.gh1054?
I wrote up an application with com.example as the package structure.On Tue, Feb 3, 2015 at 9:29 PM, Phil Webb [email protected]
wrote:You can send a pull resist request to
https://github.com/spring-projects/spring-boot-issues.
On Feb 3, 2015 6:21 PM, "kmandeville" [email protected]
wrote:I was able to create a simple Spring Boot application. I also
narrowed
it
down to failing when you upgrade from 1.2.0.M1 to 1.2.0.M2. Not RC2.
How
do
I add my sample app here? Should I add it to a github repo? Or can I
zip
it
and attach it here?โ
Reply to this email directly or view it on GitHub
<https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72778417
.
โ
Reply to this email directly or view it on GitHub
<https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72779200
.
โ
Reply to this email directly or view it on GitHub
<
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72779692.
โ
Reply to this email directly or view it on GitHub
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72781331
.
Turns out that the property spring.jackson.deserialization.fail-on-unknown-properties fails with Spring Boot 1.2.0.M2 and 1.2.0.RC1, but works in RC2. Must be a the jackson dependency changes. The property still doesn't work for me. I'm going to do the pull request shortly.
You've configured the tests to use Spring MVC test in standalone mode with their own MappingJackson2HttpMessageConverter:
mockMvc = standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
This means that the application context that's being configured by Boot isn't being used in the test and is the reason for spring.jackson.deserialization.fail-on-unknown-properties having no effect. Instead of configuring Spring MVC test to use a standalone controller, you should configure it to use the web application context that's created and configured by Boot:
package com.example;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ControllerTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = webAppContextSetup(context).build();
}
// Test methods
}
Damn. Ok. Let me give that a try in my application. Thank you! Sorry to
waste your time.
On Wed, Feb 4, 2015 at 5:02 AM, Andy Wilkinson [email protected]
wrote:
Closed #2446 https://github.com/spring-projects/spring-boot/issues/2446.
โ
Reply to this email directly or view it on GitHub
https://github.com/spring-projects/spring-boot/issues/2446#event-229924585
.
I have a question about this. The main reason I was using standalone was
that I needed to mock out the service that is being called by the
controller. I was doing that using a @Mock service and @InjectMocks on a
controller instance in my test class. How would I do this using the
webAppContextSetup?
On Wed, Feb 4, 2015 at 5:02 AM, Andy Wilkinson [email protected]
wrote:
You've configured the tests to use Spring MVC test in standalone mode with
their own MappingJackson2HttpMessageConverter:mockMvc = standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
This means that the application context that's being configured by Boot
isn't being used in the test and is the reason for
spring.jackson.deserialization.fail-on-unknown-properties having no
effect. Instead of configuring Spring MVC test to use a standalone
controller, you should configure it to use the web application context
that's created and configured by Boot:package com.example;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.web.context.WebApplicationContext;@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfigurationpublic class ControllerTest {@Autowired private WebApplicationContext context; private MockMvc mockMvc; @Before public void setUp() throws Exception { mockMvc = webAppContextSetup(context).build(); } // Test methods}
โ
Reply to this email directly or view it on GitHub
https://github.com/spring-projects/spring-boot/issues/2446#issuecomment-72828566
.
Separate out the configuration that creates the services used by the controller and using a different configuration class in your test that exposes the mock services as Spring beans.
Most helpful comment
Separate out the configuration that creates the services used by the controller and using a different configuration class in your test that exposes the mock services as Spring beans.