Even though both @ContextConfiguration in addition to @SpringApplicationConfiguration annotations are used along amongst SpringJUnit4ClassRunner to specify how to charge the Spring application context, at that spot is a subtle departure betwixt them. Although @ContextConfiguration does a dandy chore inwards loading application context it doesn't lead keep amount payoff of Spring Boot features. Spring Boot applications are ultimately loaded yesteryear either SpringApplication ( inwards instance of the JAR) or SpringBootServletInitializer. This degree non entirely loads the application context but besides enables logging in addition to loading of external properties specified inwards application.properties or application.yml file, in addition to other features of Spring Boot framework, which is non loaded or enabled yesteryear the @ContextConfiguration annotation.
In short, it's improve to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration attempt for Spring kick application, including a attempt for spider web pages or front end terminate of the application.
Now that y'all know the essential difference betwixt @ContextConfiguration in addition to @SpringApplicationConfiguration annotations spell writing Spring kick tests, let's sympathise that inwards a piffling fleck to a greater extent than details amongst around code samples.
Spring Framework has first-class back upwards for writing Integration attempt since Spring 2.5 when it introduced SpringJUnit4ClassRunner in addition to the same goes for testing amongst Spring Boot application. During the Integration test, y'all besides bespeak to charge beans in addition to wire them upwards amongst dependency.
Of course, y'all tin give the sack produce that manually but it's improve if Spring handles them for you, which it does. It besides offers out-of-the-box features such every bit ingredient scanning, autowiring, annunciation transaction management, security, in addition to caching, which comes handy for testing inwards a to a greater extent than production-like environment. Btw, if y'all are non familiar amongst these essential Spring features in addition to then I advise y'all offset with Spring Framework 5: Beginner to Guru course of written report on Udemy, which covers them quite well.
As y'all tin give the sack see, the BookServiceTest degree is annotated amongst both the @RunWith JUnit annotation in addition to @ContextConfiguration annotations.
The @RunWith annotation is required to enable Spring integration testing in addition to that's why nosotros lead keep passed the SpringJUnit4ClassRunnner degree to it, spell @ContextConfiguration annotation specifies how to charge application context.
In this example, the Spring application context defined inwards the BookConfiguration degree volition survive loaded.
The SpringJUnit4ClassRunner is a powerful degree which non entirely loads the application context but besides autowire beans into the attempt every bit well.
For example, inwards this attempt class, nosotros needed a BookService to attempt its findByTitle() method in addition to that edible bean is automatically injected yesteryear SpringJUnit4ClassRunner degree hence nosotros only left amongst writing our testBookService() method.
If y'all are non familiar amongst auto-wiring You tin give the sack see Spring Boot Reference Guide
5 Free Spring Framework in addition to Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top v Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, in addition to Deployment
Thanks for reading this article hence far. If y'all similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation in addition to then delight portion amongst your friends in addition to colleagues. If y'all lead keep whatsoever questions or feedback in addition to then delight drib a note.
P. S. - If y'all desire to acquire Spring v in addition to Spring Boot two from scratch, inwards a guided, code-focused agency in addition to then I advise y'all lead keep a expect at the Learn Spring: The Master Class course of written report yesteryear Eugen Paraschive of Baeldung.
In short, it's improve to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration attempt for Spring kick application, including a attempt for spider web pages or front end terminate of the application.
Now that y'all know the essential difference betwixt @ContextConfiguration in addition to @SpringApplicationConfiguration annotations spell writing Spring kick tests, let's sympathise that inwards a piffling fleck to a greater extent than details amongst around code samples.
Spring Framework has first-class back upwards for writing Integration attempt since Spring 2.5 when it introduced SpringJUnit4ClassRunner in addition to the same goes for testing amongst Spring Boot application. During the Integration test, y'all besides bespeak to charge beans in addition to wire them upwards amongst dependency.
Of course, y'all tin give the sack produce that manually but it's improve if Spring handles them for you, which it does. It besides offers out-of-the-box features such every bit ingredient scanning, autowiring, annunciation transaction management, security, in addition to caching, which comes handy for testing inwards a to a greater extent than production-like environment. Btw, if y'all are non familiar amongst these essential Spring features in addition to then I advise y'all offset with Spring Framework 5: Beginner to Guru course of written report on Udemy, which covers them quite well.
Spring Boot - @ContextConfiguration example
Here is a uncomplicated Spring integration attempt amongst SpringJUnit4ClassRunner in addition to @ContextConfiguration annotation, 1 of the essential Spring kick annotations for loading application context:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=BookConfiguration.class) public class BookServiceTest { @Autowired private BookService bookService; @Test public void testBookService() { Book aBook = bookService.findByTitle("Spring Boot inwards Action"); assertEquals("Craig Walls", aBook.getAuthor()); assertEquals(40, aBook.getPrice()); } }
As y'all tin give the sack see, the BookServiceTest degree is annotated amongst both the @RunWith JUnit annotation in addition to @ContextConfiguration annotations.
The @RunWith annotation is required to enable Spring integration testing in addition to that's why nosotros lead keep passed the SpringJUnit4ClassRunnner degree to it, spell @ContextConfiguration annotation specifies how to charge application context.
In this example, the Spring application context defined inwards the BookConfiguration degree volition survive loaded.
The SpringJUnit4ClassRunner is a powerful degree which non entirely loads the application context but besides autowire beans into the attempt every bit well.
For example, inwards this attempt class, nosotros needed a BookService to attempt its findByTitle() method in addition to that edible bean is automatically injected yesteryear SpringJUnit4ClassRunner degree hence nosotros only left amongst writing our testBookService() method.
If y'all are non familiar amongst auto-wiring You tin give the sack see Spring Boot Reference Guide
5 Free Spring Framework in addition to Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top v Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, in addition to Deployment
Thanks for reading this article hence far. If y'all similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation in addition to then delight portion amongst your friends in addition to colleagues. If y'all lead keep whatsoever questions or feedback in addition to then delight drib a note.
P. S. - If y'all desire to acquire Spring v in addition to Spring Boot two from scratch, inwards a guided, code-focused agency in addition to then I advise y'all lead keep a expect at the Learn Spring: The Master Class course of written report yesteryear Eugen Paraschive of Baeldung.