What is the proper annotation since @SpringApplicationConfiguration
and @WebIntegration
are deprecated as of Spring Boot Framework 1.4? I'm trying to play around with unit testing.
-
4@SpringBootTest(webEnvironment=WebEnvironment.MOCK)– UlisesCommented Sep 9, 2016 at 18:18
-
Take a look at spring.io/blog/2016/04/15/…– Ilya SerbisCommented Jan 26, 2017 at 12:49
-
It is not an answer to you question but, Spring boot is not a framework. But it is an opinionated view of Spring Framework. springhow.com/spring-boot-and-spring– Raja AnbazhaganCommented Aug 9, 2020 at 19:42
5 Answers
Take a look into JavaDocs of deprecated classes:
* @deprecated as of 1.4 in favor of
* {@link org.springframework.boot.test.context.SpringBootTest} with
* {@code webEnvironment=RANDOM_PORT} or {@code webEnvironment=DEFINED_PORT}.
*/
...
@Deprecated
public @interface WebIntegrationTest {
* @deprecated as of 1.4 in favor of {@link SpringBootTest} or direct use of
* {@link SpringBootContextLoader}.
*/
...
@Deprecated
public @interface SpringApplicationConfiguration {
Is there also a replacement for TestRestTemplate()?
Yes, here it is:
* @deprecated as of 1.4 in favor of
* {@link org.springframework.boot.test.web.client.TestRestTemplate}
*/
@Deprecated
public class TestRestTemplate extends RestTemplate {
A good place to start is now probably: Testing improvements in Spring Boot 1.4.
They describe a basic sample like the following:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {
}
as a replacement to, one of many:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@WebIntegrationTest
public class MyTest {
}
-
2how does the upper version of you know that the ApplicationConfiguration is in the class MyApp.class ? when I implement it your way it fails to load the applicationContext– NaliCommented Aug 4, 2017 at 7:19
-
Good question, I do not know but it might be only one runing App at a time. Commented Aug 22, 2017 at 9:38
you can use @EnableAutoConfiguration or @SpringBootApplication.
for testing purpose you can use @SpringBootTest(webEnvironment='your value') or simply @SpringBootTest
please refer :
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
for testing the REST, you can use @RestClientTest and configure a RestTemplateBuilder.
You should use this annotation:
@ContextConfiguration(classes = main_class)
-
This doesn't work as during integration test my app is unable to get the database URL params from the properties file. Commented Nov 19, 2017 at 20:38
-
1@NeerajJain to be able to get them just a small tweak is necessary: @ContextConfiguration(classes = main_class, initializers = ConfigFileApplicationContextInitializer.class) Commented Mar 12, 2019 at 17:03
I have been using the following with no problem at all:
@ContextConfiguration(classes = Application.class)
Whereas Application is the name of my main class.