Skip to content

Commit 0aa0fd0

Browse files
committed
Ensure that Jackson and GSON are auto-configured by @AutoConfigureJsonTesters
Previously, @AutoConfigureJsonTesters only imported JsonTestersAutoConfiguration and relied on something else pulling in the Jackson and GSON auto-configuration upon which it depends. This worked with @jsontest which imported those auto-configurations. It did not work with @SpringBootTest which would use @EnableAutoConfiguration and the ordering was then wrong and JsonTestersAutoConfiguration would be processed before the Jackson and GSON auto-configurations had a chance to create the beans that JsonTestersAutoConfiguration needs. This commit updates the spring.factories configuration for JsonTestersAutoConfiguration so that it imports JacksonAutoConfiguration and GsonAutoConfiguration. Appropriate @AutoConfigureAfter has also been added to JsonTestersAutoConfiguration to ensure that it is considered after JacksonAutoConfiguration and GsonAutoConfiguration. Lastly, ExampleJsonApplication and associated classes have been moved into an app sub-package to prevent its component scanning from pulling in JsonTestersAutoConfiguration as if it were user configuration. Closes gh-9515
1 parent 5aa27be commit 0aa0fd0

File tree

10 files changed

+82
-13
lines changed

10 files changed

+82
-13
lines changed

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,9 +27,12 @@
2727
import org.springframework.beans.factory.FactoryBean;
2828
import org.springframework.beans.factory.config.BeanPostProcessor;
2929
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
30+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
3031
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3132
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3233
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
34+
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
35+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
3336
import org.springframework.boot.test.json.AbstractJsonMarshalTester;
3437
import org.springframework.boot.test.json.BasicJsonTester;
3538
import org.springframework.boot.test.json.GsonTester;
@@ -52,6 +55,7 @@
5255
@Configuration
5356
@ConditionalOnClass(name = "org.assertj.core.api.Assert")
5457
@ConditionalOnProperty("spring.test.jsontesters.enabled")
58+
@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class })
5559
public class JsonTestersAutoConfiguration {
5660

5761
@Bean

spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
4141

4242
# AutoConfigureJsonTesters auto-configuration imports
4343
org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters=\
44-
org.springframework.boot.test.autoconfigure.json.JsonTestersAutoConfiguration
44+
org.springframework.boot.test.autoconfigure.json.JsonTestersAutoConfiguration,\
45+
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
46+
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
4547

4648
# AutoConfigureMockMvc auto-configuration imports
4749
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\

spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestIntegrationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
import org.junit.runner.RunWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.autoconfigure.json.app.ExampleBasicObject;
24+
import org.springframework.boot.test.autoconfigure.json.app.ExampleCustomObject;
25+
import org.springframework.boot.test.autoconfigure.json.app.ExampleJsonApplication;
26+
import org.springframework.boot.test.autoconfigure.json.app.ExampleJsonObjectWithView;
2327
import org.springframework.boot.test.json.BasicJsonTester;
2428
import org.springframework.boot.test.json.GsonTester;
2529
import org.springframework.boot.test.json.JacksonTester;
2630
import org.springframework.boot.test.json.JsonContent;
31+
import org.springframework.test.context.ContextConfiguration;
2732
import org.springframework.test.context.junit4.SpringRunner;
2833

2934
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,6 +41,7 @@
3641
*/
3742
@RunWith(SpringRunner.class)
3843
@JsonTest
44+
@ContextConfiguration(classes = ExampleJsonApplication.class)
3945
public class JsonTestIntegrationTests {
4046

4147
@Autowired

spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestWithAutoConfigureJsonTestersTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import org.junit.runner.RunWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.autoconfigure.json.app.ExampleBasicObject;
2324
import org.springframework.boot.test.json.BasicJsonTester;
2425
import org.springframework.boot.test.json.GsonTester;
2526
import org.springframework.boot.test.json.JacksonTester;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.json;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.autoconfigure.json.app.ExampleBasicObject;
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.test.json.BasicJsonTester;
26+
import org.springframework.boot.test.json.GsonTester;
27+
import org.springframework.boot.test.json.JacksonTester;
28+
import org.springframework.test.context.junit4.SpringRunner;
29+
30+
/**
31+
* Integration tests for {@link SpringBootTest} with {@link AutoConfigureJsonTesters}.
32+
*
33+
* @author Andy Wilkinson
34+
*/
35+
@RunWith(SpringRunner.class)
36+
@SpringBootTest
37+
@AutoConfigureJsonTesters
38+
public class SpringBootTestWithAutoConfigureJsonTestersTests {
39+
40+
@Autowired
41+
BasicJsonTester basicJson;
42+
43+
@Autowired
44+
JacksonTester<ExampleBasicObject> jacksonTester;
45+
46+
@Autowired
47+
GsonTester<ExampleBasicObject> gsonTester;
48+
49+
@Test
50+
public void contextLoads() {
51+
52+
}
53+
54+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure.json;
17+
package org.springframework.boot.test.autoconfigure.json.app;
1818

1919
/**
2020
* Example object to read/write as JSON.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure.json;
17+
package org.springframework.boot.test.autoconfigure.json.app;
1818

1919
/**
2020
* Example object to read/write as JSON via {@link ExampleJsonComponent}.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure.json;
17+
package org.springframework.boot.test.autoconfigure.json.app;
1818

1919
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.test.autoconfigure.json.JsonTest;
2021

2122
/**
2223
* Example {@link SpringBootApplication @SpringBootApplication} for use with
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure.json;
17+
package org.springframework.boot.test.autoconfigure.json.app;
1818

1919
import java.io.IOException;
2020

@@ -28,6 +28,7 @@
2828
import org.springframework.boot.jackson.JsonComponent;
2929
import org.springframework.boot.jackson.JsonObjectDeserializer;
3030
import org.springframework.boot.jackson.JsonObjectSerializer;
31+
import org.springframework.boot.test.autoconfigure.json.JsonTest;
3132

3233
/**
3334
* Example {@link JsonComponent} for use with {@link JsonTest @JsonTest} tests.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure.json;
17+
package org.springframework.boot.test.autoconfigure.json.app;
1818

1919
import com.fasterxml.jackson.annotation.JsonView;
2020

@@ -68,7 +68,7 @@ public String toString() {
6868
return this.value + " " + this.id;
6969
}
7070

71-
static class TestView {
71+
public static class TestView {
7272

7373
}
7474

0 commit comments

Comments
 (0)