Prince Varshney
Prince Varshney

Reputation: 119

Difference between @SpringApplicationConfiguration and @ContextConfiguration

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

Upvotes: 12

Views: 3842

Answers (3)

Dheeraj
Dheeraj

Reputation: 21

  • @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.

Upvotes: 2

SkyWalker
SkyWalker

Reputation: 29168

@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

Upvotes: 6

dunni
dunni

Reputation: 44555

@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.

Upvotes: 8

Related Questions