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.

3

5 Answers 5

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 {
0
30

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 {
}
2
  • 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
    Commented 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
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.

6

You should use this annotation:

@ContextConfiguration(classes = main_class)
2
  • 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
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.