11

What is the Difference between @SpringApplicationConfiguration and @ContextConfiguration with respect to JUnit test cases?

abcd
  • 10,215
  • 15
  • 51
  • 85
Prince Varshney
  • 109
  • 1
  • 7

3 Answers3

7

@ContextConfiguration is an annotation from the Spring Test Framework, which is suitable for every Spring application, @SpringApplicationConfiguration is from Spring Boot and is actually a composite annotation, which includes ContextConfiguration with the custom SpringApplicationContextLoader as loader.

dunni
  • 43,386
  • 10
  • 104
  • 99
6

@ContextConfiguration and @SpringApplicationConfiguration both are doing same. Both load and configure an ApplicationContext for integration tests. But @ContextConfiguration has some lacking for support.

@ContextConfiguration Supported Resource Types

Prior to Spring 3.1, only path-based resource locations (typically XML configuration files) were supported. As of Spring 3.1, context loaders may choose to support either path-based or class-based resources.

As of Spring 4.0.4, context loaders may choose to support path-based and class-based resources simultaneously. Consequently @ContextConfiguration can be used to declare either path-based resource locations (via the locations() or value() attribute) or annotated classes (via the classes() attribute).

Note, however, that most implementations of SmartContextLoader only support a single resource type. As of Spring 4.1, path-based resource locations may be either XML configuration files or Groovy scripts (if Groovy is on the classpath). Of course, third-party frameworks may choose to support additional types of path-based resources.

@SpringApplicationConfiguration is similar to the standard @ContextConfiguration but uses Spring Boot's SpringApplicationContextLoader.

Resource Link:

  1. Annotation Type SpringApplicationConfiguration
  2. Annotation Type ContextConfiguration
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
2
  • @ContextConfiguration
    • Loads the Spring application context
    • it doesn’t load it with the full Spring Boot treatment.
  • @SpringApplicationConfiguration
    • largely identical to @ContextConfiguration.
    • Loads the Spring application context,
      • but also enables logging, the loading of external properties (application.properties or application.yml).
    • At most part @SpringApplicationConfiguration replaces @ContextConfiguration when writing tests for Spring Boot applications.
Dheeraj
  • 21
  • 2