Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @SpringApplicationConfiguration and @ContextConfiguration

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

like image 765
Prince Varshney Avatar asked Aug 18 '16 14:08

Prince Varshney


3 Answers

  • @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.
like image 103
Dheeraj Avatar answered Sep 20 '22 23:09

Dheeraj


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

like image 20
dunni Avatar answered Sep 24 '22 23:09

dunni


@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
like image 29
SkyWalker Avatar answered Sep 22 '22 23:09

SkyWalker