74

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.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Person
  • 880
  • 1
  • 6
  • 12

5 Answers5

53

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 {
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
29

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 {
}
user1767316
  • 3,276
  • 3
  • 37
  • 46
  • 2
    how 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 – Nali Aug 04 '17 at 07:19
  • Good question, I do not know but it might be only one runing App at a time. – user1767316 Aug 22 '17 at 09:38
9

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.

satish chennupati
  • 2,602
  • 1
  • 18
  • 27
6

You should use this annotation:

@ContextConfiguration(classes = main_class)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Grigore Chis
  • 79
  • 1
  • 2
  • This doesn't work as during integration test my app is unable to get the database URL params from the properties file. – Neeraj Jain Nov 19 '17 at 20:38
  • 1
    @NeerajJain to be able to get them just a small tweak is necessary: @ContextConfiguration(classes = main_class, initializers = ConfigFileApplicationContextInitializer.class) – sowieso-fruehling Mar 12 '19 at 17:03
1

I have been using the following with no problem at all:

@ContextConfiguration(classes = Application.class)

Whereas Application is the name of my main class.